Skip to content
Merged
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
44 changes: 44 additions & 0 deletions src/Model/Request/References/SubscriptionsEditRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* PHP version 7.3
*
* @category SubscriptionsEditRequest
* @package RetailCrm\Api\Model\Request\References
*/

namespace RetailCrm\Api\Model\Request\References;

use RetailCrm\Api\Component\FormData\Mapping as Form;
use RetailCrm\Api\Interfaces\RequestInterface;
use RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory;

/**
* Class SubscriptionsEditRequest
*
* @category SubscriptionsEditRequest
* @package RetailCrm\Api\Model\Request\References
*/
class SubscriptionsEditRequest implements RequestInterface
{
/**
* @var \RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory
*
* @Form\Type("RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory")
* @Form\SerializedName("subscription")
* @Form\JsonField()
*/
public $subscription;

/**
* SubscriptionsEditRequest constructor.
*
* @param \RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory|null $subscription
*/
public function __construct(?SubscriptionCategory $subscription = null)
{
if (null !== $subscription) {
$this->subscription = $subscription;
}
}
}
30 changes: 30 additions & 0 deletions src/Model/Response/References/SubscriptionsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* PHP version 7.3
*
* @category SubscriptionsResponse
* @package RetailCrm\Api\Model\Response\References
*/

namespace RetailCrm\Api\Model\Response\References;

use RetailCrm\Api\Component\Serializer\Annotation as JMS;
use RetailCrm\Api\Model\Response\SuccessResponse;

/**
* Class SubscriptionsResponse
*
* @category SubscriptionsResponse
* @package RetailCrm\Api\Model\Response\References
*/
class SubscriptionsResponse extends SuccessResponse
{
/**
* @var \RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory[]
*
* @JMS\Type("array<RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory>")
* @JMS\SerializedName("subscriptions")
*/
public $subscriptions;
}
131 changes: 131 additions & 0 deletions src/ResourceGroup/References.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use RetailCrm\Api\Model\Request\References\SitesEditRequest;
use RetailCrm\Api\Model\Request\References\StatusesEditRequest;
use RetailCrm\Api\Model\Request\References\StoresEditRequest;
use RetailCrm\Api\Model\Request\References\SubscriptionsEditRequest;
use RetailCrm\Api\Model\Request\References\UnitsEditRequest;
use RetailCrm\Api\Model\Response\IdResponse;
use RetailCrm\Api\Model\Response\References\CostGroupsResponse;
Expand All @@ -47,6 +48,7 @@
use RetailCrm\Api\Model\Response\References\StatusesResponse;
use RetailCrm\Api\Model\Response\References\StatusGroupsResponse;
use RetailCrm\Api\Model\Response\References\StoresResponse;
use RetailCrm\Api\Model\Response\References\SubscriptionsResponse;
use RetailCrm\Api\Model\Response\References\UnitsResponse;
use RetailCrm\Api\Model\Response\SuccessResponse;

Expand Down Expand Up @@ -2166,6 +2168,135 @@ public function storesEdit(string $code, StoresEditRequest $request): SuccessRes
return $response;
}

/**
* Makes GET "/api/v5/reference/subscriptions" request.
*
* Example:
* ```php
* use RetailCrm\Api\Factory\SimpleClientFactory;
* use RetailCrm\Api\Interfaces\ApiExceptionInterface;
*
* $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey');
*
* try {
* $response = $client->references->subscriptions();
* } catch (ApiExceptionInterface $exception) {
* echo sprintf(
* 'Error from RetailCRM API (status code: %d): %s',
* $exception->getStatusCode(),
* $exception->getMessage()
* );
*
* if (count($exception->getErrorResponse()->errors) > 0) {
* echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors);
* }
*
* return;
* }
*
* echo 'Subscriptions: ' . print_r($response->subscriptions, true);
* ```
*
* @return \RetailCrm\Api\Model\Response\References\SubscriptionsResponse
* @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface
* @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface
* @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException
* @throws \RetailCrm\Api\Exception\Api\ApiErrorException
* @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException
* @throws \RetailCrm\Api\Exception\Api\MissingParameterException
* @throws \RetailCrm\Api\Exception\Api\ValidationException
* @throws \RetailCrm\Api\Exception\Client\HandlerException
* @throws \RetailCrm\Api\Exception\Client\HttpClientException
*/
public function subscriptions(): SubscriptionsResponse
{
/** @var SubscriptionsResponse $response */
$response = $this->sendRequest(
RequestMethod::GET,
'reference/subscriptions',
null,
SubscriptionsResponse::class
);
return $response;
}

/**
* Makes POST "/api/v5/reference/subscriptions/{channel}/{code}/edit" request.
*
* Example:
* ```php
* use RetailCrm\Api\Factory\SimpleClientFactory;
* use RetailCrm\Api\Interfaces\ApiExceptionInterface;
* use RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory;
* use RetailCrm\Api\Model\Request\References\SubscriptionsEditRequest;
*
* $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey');
*
* $entity = new SubscriptionCategory();
* $entity->channel = 'email';
* $entity->name = 'Новости';
* $entity->active = true;
* $entity->autoSubscribe = false;
* $entity->ordering = 10;
*
* try {
* $response = $client->references->subscriptionsEdit(
* 'email',
* 'news',
* new SubscriptionsEditRequest($entity)
* );
* } catch (ApiExceptionInterface $exception) {
* echo sprintf(
* 'Error from RetailCRM API (status code: %d): %s',
* $exception->getStatusCode(),
* $exception->getMessage()
* );
*
* if (count($exception->getErrorResponse()->errors) > 0) {
* echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors);
* }
*
* return;
* }
*
* echo 'Created subscription category with ID: ' . $response->id;
* ```
*
* @param string $channel
* @param string $code
* @param \RetailCrm\Api\Model\Request\References\SubscriptionsEditRequest $request
*
* @return \RetailCrm\Api\Model\Response\IdResponse
* @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface
* @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface
* @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException
* @throws \RetailCrm\Api\Exception\Api\ApiErrorException
* @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException
* @throws \RetailCrm\Api\Exception\Api\MissingParameterException
* @throws \RetailCrm\Api\Exception\Api\ValidationException
* @throws \RetailCrm\Api\Exception\Client\HandlerException
* @throws \RetailCrm\Api\Exception\Client\HttpClientException
*/
public function subscriptionsEdit(string $channel, string $code, SubscriptionsEditRequest $request): IdResponse
{
$sendRequest = $request;

if (null !== $request->subscription) {
$subscription = clone $request->subscription;
$subscription->channel = $channel;
$sendRequest = new SubscriptionsEditRequest($subscription);
}

/** @var IdResponse $response */
$response = $this->sendRequest(
RequestMethod::POST,
'reference/subscriptions/' . $channel . '/' . $code . '/edit',
$sendRequest,
IdResponse::class
);
return $response;
}

/**
* Makes GET "/api/v5/reference/units" request.
*
Expand Down
78 changes: 78 additions & 0 deletions tests/src/ResourceGroup/ReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use RetailCrm\Api\Enum\RequestMethod;
use RetailCrm\Api\Model\Callback\Entity\Delivery\SerializedStoreWeekOpeningHours;
use RetailCrm\Api\Model\Callback\Entity\Delivery\StoreWorkTime;
use RetailCrm\Api\Model\Entity\Customers\SubscriptionCategory;
use RetailCrm\Api\Model\Entity\Orders\Delivery\CourierPhone;
use RetailCrm\Api\Model\Entity\References\CostGroup;
use RetailCrm\Api\Model\Entity\References\CostItem;
Expand Down Expand Up @@ -51,6 +52,7 @@
use RetailCrm\Api\Model\Request\References\SitesEditRequest;
use RetailCrm\Api\Model\Request\References\StatusesEditRequest;
use RetailCrm\Api\Model\Request\References\StoresEditRequest;
use RetailCrm\Api\Model\Request\References\SubscriptionsEditRequest;
use RetailCrm\Api\Model\Request\References\UnitsEditRequest;
use RetailCrm\TestUtils\Factory\TestClientFactory;
use RetailCrm\TestUtils\TestCase\AbstractApiResourceGroupTestCase;
Expand Down Expand Up @@ -3712,6 +3714,82 @@ public function testStoresEdit(): void
self::assertModelEqualsToResponse($json, $response);
}

public function testSubscriptions(): void
{
$json = <<<'EOF'
{
"success": true,
"subscriptions": [
{
"id": 2,
"channel": "email",
"name": "Без тематики",
"code": "default_marketing",
"active": true,
"autoSubscribe": true,
"ordering": 1
},
{
"id": 4,
"channel": "waba",
"name": "Новости",
"code": "news",
"active": true,
"autoSubscribe": false,
"ordering": 10
}
]
}
EOF;

$mock = static::createApiMockBuilder('reference/subscriptions');
$mock->matchMethod(RequestMethod::GET)
->reply(200)
->withBody($json);

$client = TestClientFactory::createClient($mock->getClient());
$response = $client->references->subscriptions();

self::assertModelEqualsToResponse($json, $response);
}

public function testSubscriptionsEdit(): void
{
$json = <<<'EOF'
{
"success": true,
"id": 18
}
EOF;

$entity = new SubscriptionCategory();
$entity->name = 'Новости';
$entity->active = true;
$entity->autoSubscribe = false;
$entity->ordering = 10;

$request = new SubscriptionsEditRequest($entity);
$expectedEntity = new SubscriptionCategory();
$expectedEntity->channel = 'email';
$expectedEntity->name = 'Новости';
$expectedEntity->active = true;
$expectedEntity->autoSubscribe = false;
$expectedEntity->ordering = 10;
$expectedRequest = new SubscriptionsEditRequest($expectedEntity);

$mock = static::createApiMockBuilder('reference/subscriptions/email/news/edit');
$mock->matchMethod(RequestMethod::POST)
->matchBody(self::encodeForm($expectedRequest))
->reply(201)
->withBody($json);

$client = TestClientFactory::createClient($mock->getClient());
$response = $client->references->subscriptionsEdit('email', 'news', $request);

self::assertModelEqualsToResponse($json, $response);
self::assertNull($request->subscription->channel);
}

public function testUnits(): void
{
$json = <<<'EOF'
Expand Down
Loading