-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
362 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/AmoCRM/Collections/ShortLinks/ShortLinksCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.