Skip to content

Commit 2e2bf89

Browse files
committed
[UC-3] Add products and deals api client
1 parent d5f9cba commit 2e2bf89

5 files changed

Lines changed: 146 additions & 0 deletions

File tree

config/services/api.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,15 @@
77
<argument type="service" id="http_client"/>
88
<argument type="service" id="monolog.logger"/>
99
</service>
10+
11+
<service id="bit_bag.sylius_user_com_plugin.api.deal_api" class="BitBag\SyliusUserComPlugin\Api\DealApi">
12+
<argument type="service" id="http_client"/>
13+
<argument type="service" id="monolog.logger"/>
14+
</service>
15+
16+
<service id="bit_bag.sylius_user_com_plugin.api.product_api" class="BitBag\SyliusUserComPlugin\Api\ProductApi">
17+
<argument type="service" id="http_client"/>
18+
<argument type="service" id="monolog.logger"/>
19+
</service>
1020
</services>
1121
</container>

src/Api/DealApi.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Api;
13+
14+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
15+
use Symfony\Component\HttpFoundation\Request;
16+
17+
final class DealApi extends AbstractClient implements DealApiInterface
18+
{
19+
public function updateDealByCustomId(UserComApiAwareInterface $resource, string $customId, array $data): ?array
20+
{
21+
$url = $this->getApiEndpointUrl($resource, sprintf(self::UPDATE_DEAL_ENDPOINT, $customId));
22+
23+
return $this->request(
24+
$url,
25+
Request::METHOD_PUT,
26+
$this->buildOptions($resource, ['json' => $data]),
27+
);
28+
}
29+
30+
public function createDeal(UserComApiAwareInterface $resource, array $data): ?array
31+
{
32+
return $this->request(
33+
$this->getApiEndpointUrl($resource, self::CREATE_DEAL_ENDPOINT),
34+
Request::METHOD_POST,
35+
$this->buildOptions($resource, [
36+
'json' => $data,
37+
'headers' => [
38+
'Content-Type' => 'application/json',
39+
],
40+
]),
41+
);
42+
}
43+
44+
public function updateOrCreateDeal(UserComApiAwareInterface $resource, array $data): ?array
45+
{
46+
return $this->request(
47+
$this->getApiEndpointUrl($resource, self::CREATE_OR_UPDATE_DEAL_ENDPOINT),
48+
Request::METHOD_POST,
49+
$this->buildOptions($resource, [
50+
'json' => $data,
51+
'headers' => [
52+
'Content-Type' => 'application/json',
53+
],
54+
]),
55+
);
56+
}
57+
}

src/Api/DealApiInterface.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Api;
13+
14+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
15+
16+
interface DealApiInterface
17+
{
18+
public const CREATE_DEAL_ENDPOINT = '/deals/';
19+
20+
public const UPDATE_DEAL_ENDPOINT = '/deals-by-id/%s/';
21+
22+
public const CREATE_OR_UPDATE_DEAL_ENDPOINT = '/deals/update_or_create/';
23+
24+
public function updateDealByCustomId(UserComApiAwareInterface $resource, string $customId, array $data): ?array;
25+
26+
public function createDeal(UserComApiAwareInterface $resource, array $data): ?array;
27+
28+
public function updateOrCreateDeal(UserComApiAwareInterface $resource, array $data): ?array;
29+
}

src/Api/ProductApi.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Api;
13+
14+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
15+
use Symfony\Component\HttpFoundation\Request;
16+
17+
final class ProductApi extends AbstractClient implements ProductApiInterface
18+
{
19+
public const GET_PRODUCT_BY_CUSTOM_ID_ENDPOINT = '/products-by-id/%s/product_event/';
20+
21+
public function getProductByCustomId(UserComApiAwareInterface $resource, int $productId): ?array
22+
{
23+
$url = $this->getApiEndpointUrl($resource, sprintf(self::GET_PRODUCT_BY_CUSTOM_ID_ENDPOINT, $productId));
24+
25+
return $this->request(
26+
$url,
27+
Request::METHOD_GET,
28+
$this->buildOptions($resource),
29+
);
30+
}
31+
}

src/Api/ProductApiInterface.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Api;
13+
14+
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
15+
16+
interface ProductApiInterface
17+
{
18+
public function getProductByCustomId(UserComApiAwareInterface $resource, int $productId): ?array;
19+
}

0 commit comments

Comments
 (0)