Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add event types #574

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/AmoCRM/Client/AmoCRMApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use AmoCRM\EntitiesServices\CustomFields;
use AmoCRM\EntitiesServices\EntityFiles;
use AmoCRM\EntitiesServices\EntityNotes;
use AmoCRM\EntitiesServices\EventTypes;
use AmoCRM\EntitiesServices\Files;
use AmoCRM\EntitiesServices\Sources;
use AmoCRM\EntitiesServices\EntitySubscriptions;
Expand Down Expand Up @@ -565,6 +566,19 @@ public function events(): Events
return new Events($request);
}

/**
* Метод вернет объект событий
*
* @return EventTypes
* @throws AmoCRMMissedTokenException
*/
public function eventTypes(): EventTypes
{
$request = $this->buildRequest();

return new EventTypes($request);
}

/**
* Метод вернет объект хуков
*
Expand Down
28 changes: 28 additions & 0 deletions src/AmoCRM/Collections/EventTypesCollections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace AmoCRM\Collections;

use AmoCRM\Collections\Interfaces\HasPagesInterface;
use AmoCRM\Collections\Traits\PagesTrait;
use AmoCRM\Models\EventTypeModel;

/**
* Class EventTypesCollections
*
* @package AmoCRM\Collections
*
* @method null|EventTypeModel current()
* @method null|EventTypeModel last()
* @method null|EventTypeModel first()
* @method null|EventTypeModel offsetGet($offset)
* @method void offsetSet($offset, EventTypeModel $value)
* @method EventsCollections prepend(EventTypeModel $value)
* @method EventsCollections add(EventTypeModel $value)
* @method null|EventTypeModel getBy($key, $value)
*/
class EventTypesCollections extends BaseApiCollection implements HasPagesInterface
{
use PagesTrait;

public const ITEM_CLASS = EventTypeModel::class;
}
107 changes: 107 additions & 0 deletions src/AmoCRM/EntitiesServices/EventTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace AmoCRM\EntitiesServices;

use AmoCRM\Client\AmoCRMApiClient;
use AmoCRM\Client\AmoCRMApiRequest;
use AmoCRM\Collections\BaseApiCollection;
use AmoCRM\Collections\EventsCollections;
use AmoCRM\Collections\EventTypesCollections;
use AmoCRM\Exceptions\NotAvailableForActionException;
use AmoCRM\Filters\BaseEntityFilter;
use AmoCRM\Helpers\EntityTypesInterface;
use AmoCRM\Models\BaseApiModel;
use AmoCRM\Models\EventTypeModel;

/**
* Class EventTypes
*
* @package AmoCRM\EntitiesServices
*
* @method null|EventsCollections get(BaseEntityFilter $filter = null, array $with = [])
*/
class EventTypes extends BaseEntity
{
/**
* @var string
*/
protected $method = 'api/v' . AmoCRMApiClient::API_VERSION . '/' . EntityTypesInterface::EVENTS . '/types';

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

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

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

return $entities;
}

/**
* @param BaseApiModel $model
*
* @return BaseApiModel
* @throws NotAvailableForActionException
*/
public function addOne(BaseApiModel $model): BaseApiModel
{
throw new NotAvailableForActionException('Method not available for this entity');
}

/**
* @param BaseApiCollection $collection
*
* @return BaseApiCollection
* @throws NotAvailableForActionException
*/
public function add(BaseApiCollection $collection): BaseApiCollection
{
throw new NotAvailableForActionException('Method not available for this entity');
}

/**
* @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 @@ -20,6 +20,7 @@ interface EntityTypesInterface
public const CUSTOMERS = 'customers';
public const CUSTOMERS_TRANSACTIONS = 'transactions';
public const EVENTS = 'events';
public const EVENTS_TYPES = 'events_types';
public const NOTES = 'notes';
public const TAGS = 'tags';
public const TASKS = 'tasks';
Expand Down
83 changes: 83 additions & 0 deletions src/AmoCRM/Models/EventTypeModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace AmoCRM\Models;

use InvalidArgumentException;

class EventTypeModel extends BaseApiModel
{
/** @var string */
protected $key;
/** @var int */
protected $type;
/** @var string */
protected $lang;

public function getKey(): string
{
return $this->key;
}

public function setKey(string $key): EventTypeModel
{
$this->key = $key;

return $this;
}

public function getType(): int
{
return $this->type;
}

public function setType(int $type): EventTypeModel
{
$this->type = $type;

return $this;
}

public function getLang(): string
{
return $this->lang;
}

public function setLang(string $lang): EventTypeModel
{
$this->lang = $lang;

return $this;
}

public static function fromArray(array $eventType): EventTypeModel
{
if (empty($eventType['key'])) {
throw new InvalidArgumentException('EventType key is empty in ' . \json_encode($eventType, JSON_THROW_ON_ERROR));
}

$eventTypeModel = new static();

$eventTypeModel->setKey($eventType['key'])
->setType($eventType['type'])
->setLang($eventType['lang']);

return $eventTypeModel;
}

/**
* @inheritDoc
*/
public function toArray(): array
{
return [
'key' => $this->getKey(),
'type' => $this->getType(),
'lang' => $this->getLang(),
];
}

public function toApi(?string $requestId = "0"): array
{
return [];
}
}