From 2d0e75e373a7b23ce9208b959dc2faa920bce8a5 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 26 Jun 2020 20:13:20 +0300 Subject: [PATCH] short links --- README.md | 2 + examples/short_links_actions.php | 44 ++++++ src/AmoCRM/Client/AmoCRMApiClient.php | 13 ++ .../ShortLinks/ShortLinksCollection.php | 25 ++++ src/AmoCRM/EntitiesServices/ShortLinks.php | 140 ++++++++++++++++++ src/AmoCRM/Helpers/EntityTypesInterface.php | 1 + .../Models/ShortLinks/ShortLinkModel.php | 137 +++++++++++++++++ 7 files changed, 362 insertions(+) create mode 100644 examples/short_links_actions.php create mode 100644 src/AmoCRM/Collections/ShortLinks/ShortLinksCollection.php create mode 100644 src/AmoCRM/EntitiesServices/ShortLinks.php create mode 100644 src/AmoCRM/Models/ShortLinks/ShortLinkModel.php diff --git a/README.md b/README.md index e3c921ad..92c90e0f 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,7 @@ $leadsService = $apiClient->leads(); | customersStatuses | Сегменты покупателя | | calls | Звонки | | products | Товары | +| shortLinks | Короткие ссылки | | getOAuthClient | oAuth сервис | | getRequest | Голый запросы | @@ -422,6 +423,7 @@ $leadsService = $apiClient->leads(); ```php uninstall(WidgetModel $widgetModel); ``` + #### Методы доступные в сервисе ```products``` 1. settings 1. Результатом выполнения является модель ProductsSettingsModel diff --git a/examples/short_links_actions.php b/examples/short_links_actions.php new file mode 100644 index 00000000..17256ac6 --- /dev/null +++ b/examples/short_links_actions.php @@ -0,0 +1,44 @@ +setAccessToken($accessToken) + ->setAccountBaseDomain($accessToken->getValues()['baseDomain']) + ->onAccessTokenRefresh( + function (AccessTokenInterface $accessToken, string $baseDomain) { + saveToken( + [ + 'accessToken' => $accessToken->getToken(), + 'refreshToken' => $accessToken->getRefreshToken(), + 'expires' => $accessToken->getExpires(), + 'baseDomain' => $baseDomain, + ] + ); + } + ); + +//Сервис коротких ссылок +$shortLinksService = $apiClient->shortLinks(); + +//Создадим ссылку +$shortLink = new ShortLinkModel(); +$shortLink + ->setUrl('https://example.com') + ->setEntityType(EntityTypesInterface::CONTACTS) + ->setEntityId(11070881); + +try { + $shortLink = $shortLinksService->addOne($shortLink); +} catch (AmoCRMApiException $e) { + printError($e); + die; +} + +var_dump($shortLink->toArray()); diff --git a/src/AmoCRM/Client/AmoCRMApiClient.php b/src/AmoCRM/Client/AmoCRMApiClient.php index b6bdc08a..6fc177de 100644 --- a/src/AmoCRM/Client/AmoCRMApiClient.php +++ b/src/AmoCRM/Client/AmoCRMApiClient.php @@ -22,6 +22,7 @@ use AmoCRM\EntitiesServices\Leads; use AmoCRM\EntitiesServices\Roles; use AmoCRM\EntitiesServices\Segments; +use AmoCRM\EntitiesServices\ShortLinks; use AmoCRM\EntitiesServices\Tasks; use AmoCRM\EntitiesServices\Unsorted; use AmoCRM\EntitiesServices\Users; @@ -426,6 +427,18 @@ public function widgets(): Widgets return new Widgets($request); } + /** + * Метод вернет объект коротких ссылок + * + * @return ShortLinks + */ + public function shortLinks(): ShortLinks + { + $request = $this->buildRequest(); + + return new ShortLinks($request); + } + /** * Метод вернет объект причин отказа * diff --git a/src/AmoCRM/Collections/ShortLinks/ShortLinksCollection.php b/src/AmoCRM/Collections/ShortLinks/ShortLinksCollection.php new file mode 100644 index 00000000..1994788f --- /dev/null +++ b/src/AmoCRM/Collections/ShortLinks/ShortLinksCollection.php @@ -0,0 +1,25 @@ +processAction($collection, $response); + } + + /** + * @param BaseApiCollection|ShortLinksCollection $collection + * @param array $response + * + * @return BaseApiCollection + */ + protected function processAction(BaseApiCollection $collection, array $response): BaseApiCollection + { + $entities = $this->getEntitiesFromResponse($response); + foreach ($entities as $entity) { + if (array_key_exists('url', $entity)) { + $initialEntity = $collection->getBy('entity_id', $entity['metadata']['entity_id']); + if (!empty($initialEntity)) { + $this->processModelAction($initialEntity, $entity); + } + } + } + + return $collection; + } + + /** + * @param BaseApiModel|ShortLinkModel $apiModel + * @param array $entity + */ + protected function processModelAction(BaseApiModel $apiModel, array $entity): void + { + if (isset($entity['url'])) { + $apiModel->setUrl($entity['url']); + } + + if (isset($entity['metadata']['entity_id'])) { + $apiModel->setEntityId($entity['metadata']['entity_id']); + } + + if (isset($entity['metadata']['entity_type'])) { + $apiModel->setEntityType($entity['metadata']['entity_type']); + } + } + + /** + * @param BaseApiCollection $collection + * + * @return BaseApiCollection + * @throws NotAvailableForActionException + */ + public function update(BaseApiCollection $collection): BaseApiCollection + { + throw new NotAvailableForActionException('Method not available for this entity'); + } + + /** + * @param BaseApiModel $apiModel + * + * @return BaseApiModel + * @throws NotAvailableForActionException + */ + public function updateOne(BaseApiModel $apiModel): BaseApiModel + { + throw new NotAvailableForActionException('Method not available for this entity'); + } + + /** + * @param BaseApiModel $apiModel + * @param array $with + * + * @return BaseApiModel + * @throws NotAvailableForActionException + */ + public function syncOne(BaseApiModel $apiModel, $with = []): BaseApiModel + { + throw new NotAvailableForActionException('Method not available for this entity'); + } +} diff --git a/src/AmoCRM/Helpers/EntityTypesInterface.php b/src/AmoCRM/Helpers/EntityTypesInterface.php index 739f15be..9327b2d6 100644 --- a/src/AmoCRM/Helpers/EntityTypesInterface.php +++ b/src/AmoCRM/Helpers/EntityTypesInterface.php @@ -30,6 +30,7 @@ interface EntityTypesInterface public const CALLS = 'calls'; public const PRODUCTS = 'products'; public const SETTINGS = 'settings'; + public const SHORT_LINKS = 'short_links'; public const CUSTOM_FIELDS = 'custom_fields'; diff --git a/src/AmoCRM/Models/ShortLinks/ShortLinkModel.php b/src/AmoCRM/Models/ShortLinks/ShortLinkModel.php new file mode 100644 index 00000000..22455c3f --- /dev/null +++ b/src/AmoCRM/Models/ShortLinks/ShortLinkModel.php @@ -0,0 +1,137 @@ +setUrl($shortLink['url']) + ->setEntityId($shortLink['metadata']['entity_id']) + ->setEntityType($shortLink['metadata']['entity_type']); + + return $model; + } + + /** + * @inheritDoc + */ + public function toArray(): array + { + return [ + 'url' => $this->getUrl(), + 'entity_id' => $this->getEntityId(), + 'entity_type' => $this->getEntityType(), + ]; + } + + /** + * @return string + */ + public function getUrl(): string + { + return $this->url; + } + + /** + * @param string $url + * + * @return ShortLinkModel + */ + public function setUrl(string $url): ShortLinkModel + { + $this->url = $url; + + return $this; + } + + /** + * @return string|null + */ + public function getEntityType(): ?string + { + return $this->entityType; + } + + /** + * @param string|null $entityType + * + * @return ShortLinkModel + */ + public function setEntityType(?string $entityType): ShortLinkModel + { + $this->entityType = $entityType; + + return $this; + } + + /** + * @return int|null + */ + public function getEntityId(): ?int + { + return $this->entityId; + } + + /** + * @param int|null $entityId + * + * @return ShortLinkModel + */ + public function setEntityId(?int $entityId): ShortLinkModel + { + $this->entityId = $entityId; + + return $this; + } + + /** + * @param string|null $requestId + * @return array + */ + public function toApi(?string $requestId = "0"): array + { + return [ + 'url' => $this->getUrl(), + 'metadata' => [ + 'entity_id' => $this->getEntityId(), + 'entity_type' => $this->getEntityType(), + ], + ]; + } +}