Skip to content

Commit

Permalink
short links
Browse files Browse the repository at this point in the history
  • Loading branch information
bessudnov committed Jun 26, 2020
1 parent 23a4b82 commit 2d0e75e
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ $leadsService = $apiClient->leads();
| customersStatuses | Сегменты покупателя |
| calls | Звонки |
| products | Товары |
| shortLinks | Короткие ссылки |
| getOAuthClient | oAuth сервис |
| getRequest | Голый запросы |

Expand Down Expand Up @@ -422,6 +423,7 @@ $leadsService = $apiClient->leads();
```php
uninstall(WidgetModel $widgetModel);
```

#### Методы доступные в сервисе ```products```
1. settings
1. Результатом выполнения является модель ProductsSettingsModel
Expand Down
44 changes: 44 additions & 0 deletions examples/short_links_actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use AmoCRM\Exceptions\AmoCRMApiException;
use AmoCRM\Helpers\EntityTypesInterface;
use AmoCRM\Models\ShortLinks\ShortLinkModel;
use League\OAuth2\Client\Token\AccessTokenInterface;

include_once __DIR__ . '/bootstrap.php';

$accessToken = getToken();

$apiClient->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());
13 changes: 13 additions & 0 deletions src/AmoCRM/Client/AmoCRMApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
* Метод вернет объект причин отказа
*
Expand Down
25 changes: 25 additions & 0 deletions src/AmoCRM/Collections/ShortLinks/ShortLinksCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace AmoCRM\Collections\ShortLinks;

use AmoCRM\Collections\BaseApiCollection;
use AmoCRM\Models\ShortLinks\ShortLinkModel;

/**
* Class ShortLinksCollection
*
* @package AmoCRM\Collections\ShortLinks
*
* @method ShortLinkModel current() : ?BaseApiModel
* @method ShortLinkModel last() : ?BaseApiModel
* @method ShortLinkModel first() : ?BaseApiModel
* @method ShortLinkModel offsetGet($offset) : ?BaseApiModel
* @method self offsetSet($offset, ShortLinkModel $value) : BaseApiCollection
* @method self prepend(ShortLinkModel $value) : BaseApiCollection
* @method self add(ShortLinkModel $value) : BaseApiCollection
* @method ShortLinkModel getBy($key, $value) : ?BaseApiModel
*/
class ShortLinksCollection extends BaseApiCollection
{
public const ITEM_CLASS = ShortLinkModel::class;
}
140 changes: 140 additions & 0 deletions src/AmoCRM/EntitiesServices/ShortLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace AmoCRM\EntitiesServices;

use AmoCRM\Collections\ShortLinks\ShortLinksCollection;
use AmoCRM\Helpers\EntityTypesInterface;
use AmoCRM\Client\AmoCRMApiClient;
use AmoCRM\Client\AmoCRMApiRequest;
use AmoCRM\Collections\BaseApiCollection;
use AmoCRM\Exceptions\NotAvailableForActionException;
use AmoCRM\Models\BaseApiModel;
use AmoCRM\Models\ShortLinks\ShortLinkModel;

/**
* Class ShortLinks
*
* @package AmoCRM\EntitiesServices
*
* @method ShortLinksCollection add(ShortLinksCollection $collection) : BaseApiCollection
* @method ShortLinkModel addOne(ShortLinkModel $model) : BaseApiModel
*/
class ShortLinks extends BaseEntity
{
/**
* @var string
*/
protected $method = 'api/v' . AmoCRMApiClient::API_VERSION . '/' . EntityTypesInterface::SHORT_LINKS;

/**
* @var string
*/
protected $collectionClass = ShortLinksCollection::class;

/**
* @var string
*/
public const ITEM_CLASS = ShortLinkModel::class;

/**
* @param array $response
*
* @return array
*/
protected function getEntitiesFromResponse(array $response): array
{
$entities = [];

if (isset($response[AmoCRMApiRequest::EMBEDDED]) && isset($response[AmoCRMApiRequest::EMBEDDED][EntityTypesInterface::SHORT_LINKS])) {
$entities = $response[AmoCRMApiRequest::EMBEDDED][EntityTypesInterface::SHORT_LINKS];
}

return $entities;
}


/**
* @param BaseApiCollection|ShortLinksCollection $collection
* @param array $response
*
* @return BaseApiCollection
*/
protected function processAdd(BaseApiCollection $collection, array $response): BaseApiCollection
{
return $this->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');
}
}
1 change: 1 addition & 0 deletions src/AmoCRM/Helpers/EntityTypesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading

0 comments on commit 2d0e75e

Please sign in to comment.