Skip to content

Commit 30220ba

Browse files
authored
Merge pull request #228 from PrestaShop/add-provider-processor-examples
Added example for Provider / Processor endpoint
2 parents 24e044e + 5578c3b commit 30220ba

5 files changed

Lines changed: 233 additions & 16 deletions

File tree

api_module/config/services.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
services:
2-
32
PrestaShop\Module\ApiModule\Repository\HorseRepository:
43
arguments:
54
- '@doctrine.dbal.default_connection'
@@ -11,4 +10,9 @@ services:
1110

1211
PrestaShop\Module\ApiModule\ApiPlatform\State\HorseProcessor:
1312
autowire: true
13+
14+
PrestaShop\Module\ApiModule\ApiPlatform\State\CatProvider:
15+
tags: [ 'api_platform.state_provider' ]
16+
17+
PrestaShop\Module\ApiModule\ApiPlatform\State\CatProcessor:
1418
tags: [ 'api_platform.state_processor' ]

api_module/src/ApiPlatform/Resources/Cat.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,25 @@
3030

3131
use ApiPlatform\Metadata\ApiProperty;
3232
use ApiPlatform\Metadata\ApiResource;
33-
use ApiPlatform\Metadata\Get;
33+
use PrestaShop\Module\ApiModule\ApiPlatform\State\CatProcessor;
34+
use PrestaShop\Module\ApiModule\ApiPlatform\State\CatProvider;
35+
use Symfony\Component\Validator\Constraints as Assert;
3436

3537
#[ApiResource(
36-
operations: [
37-
new Get(
38-
uriTemplate: '/cat/{id}',
39-
requirements: ['id' => '\d+'],
40-
),
41-
]
38+
provider: CatProvider::class,
39+
processor: CatProcessor::class
4240
)]
4341
class Cat
4442
{
45-
/**
46-
* @var int
47-
*/
4843
#[ApiProperty(identifier: true)]
49-
private int $id;
44+
public ?string $uuid = null;
5045

51-
/**
52-
* @var string
53-
*/
54-
private string $name;
46+
public function __construct(
47+
#[Assert\NotBlank]
48+
#[Assert\Type('string')]
49+
public string $name,
50+
)
51+
{
52+
53+
}
5554
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/OSL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19+
* versions in the future. If you wish to customize PrestaShop for your
20+
* needs please refer to https://devdocs.prestashop.com/ for more information.
21+
*
22+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
23+
* @copyright Since 2007 PrestaShop SA and Contributors
24+
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25+
*/
26+
27+
declare(strict_types=1);
28+
29+
namespace PrestaShop\Module\ApiModule\ApiPlatform\State;
30+
31+
use ApiPlatform\Metadata\DeleteOperationInterface;
32+
use ApiPlatform\Metadata\Operation;
33+
use ApiPlatform\State\ProcessorInterface;
34+
use PrestaShop\Module\ApiModule\ApiPlatform\Resources\Cat;
35+
36+
/**
37+
* @implements ProcessorInterface<Cat, Cat|void>
38+
*/
39+
final class CatProcessor implements ProcessorInterface
40+
{
41+
private string $jsonStorageFile;
42+
private array $data;
43+
44+
public function __construct() {
45+
$this->jsonStorageFile = CatProvider::STORAGE_FILE;
46+
$jsonData = file_get_contents($this->jsonStorageFile);
47+
$this->data = json_decode($jsonData, true);
48+
}
49+
50+
/**
51+
* @return Cat|void
52+
*/
53+
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
54+
{
55+
if (!$data instanceof Cat) {
56+
return null;
57+
}
58+
59+
if ($operation instanceof DeleteOperationInterface) {
60+
return $this->remove($data, $operation, $uriVariables, $context);
61+
}
62+
63+
return $this->persist($data, $operation, $uriVariables, $context);
64+
}
65+
66+
private function remove(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
67+
{
68+
foreach ($this->data as $key => $cat) {
69+
if ($cat['uuid'] === $data->uuid) {
70+
unset($this->data[$key]);
71+
}
72+
}
73+
74+
// reindex array
75+
$this->data = array_values($this->data);
76+
file_put_contents($this->jsonStorageFile, json_encode($this->data, JSON_PRETTY_PRINT));
77+
78+
return $data;
79+
}
80+
81+
private function persist(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
82+
{
83+
$foundKey = null;
84+
// check if the cat is not already in the file
85+
foreach ($this->data as $key => $cat) {
86+
if ($cat['uuid'] === $data->uuid) {
87+
$foundKey = $key;
88+
}
89+
}
90+
91+
// if $foundKey is null, then we add a new cat
92+
// is not, we update the existing cat
93+
if ($foundKey === null) {
94+
$data->uuid = uniqid();
95+
}
96+
97+
$this->data[$foundKey] = [
98+
'uuid' => $data->uuid,
99+
'name' => $data->name,
100+
];
101+
102+
// reindex array
103+
$this->data = array_values($this->data);
104+
file_put_contents($this->jsonStorageFile, json_encode($this->data, JSON_PRETTY_PRINT));
105+
106+
return $data;
107+
}
108+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Open Software License (OSL 3.0)
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/OSL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* DISCLAIMER
17+
*
18+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19+
* versions in the future. If you wish to customize PrestaShop for your
20+
* needs please refer to https://devdocs.prestashop.com/ for more information.
21+
*
22+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
23+
* @copyright Since 2007 PrestaShop SA and Contributors
24+
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25+
*/
26+
27+
declare(strict_types=1);
28+
29+
namespace PrestaShop\Module\ApiModule\ApiPlatform\State;
30+
31+
use ApiPlatform\Metadata\Operation;
32+
use ApiPlatform\State\ProviderInterface;
33+
use ApiPlatform\Metadata\CollectionOperationInterface;
34+
use PrestaShop\Module\ApiModule\ApiPlatform\Resources\Cat;
35+
36+
/**
37+
* @implements ProviderInterface<Cat|null>
38+
*/
39+
final class CatProvider implements ProviderInterface
40+
{
41+
public const STORAGE_FILE = __DIR__ . '/../../Resources/data/data.json';
42+
private array $data;
43+
44+
public function __construct() {
45+
$jsonStorageFile = self::STORAGE_FILE;
46+
$jsonData = file_get_contents($jsonStorageFile);
47+
$this->data = json_decode($jsonData, true);
48+
}
49+
50+
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array|Cat|null
51+
{
52+
if ($operation instanceof CollectionOperationInterface) {
53+
$results = [];
54+
55+
foreach ($this->data as $cat) {
56+
$newCat = new Cat($cat['name']);
57+
$newCat->uuid = $cat['uuid'];
58+
$results[] = $newCat;
59+
}
60+
61+
return $results;
62+
}
63+
64+
return $this->findCatByUuid($uriVariables['uuid']);
65+
}
66+
67+
private function findCatByUuid(string $uuid): ?Cat
68+
{
69+
foreach ($this->data as $cat) {
70+
if ($cat['uuid'] === $uuid) {
71+
$newCat = new Cat($cat['name']);
72+
$newCat->uuid = $uuid;
73+
74+
return $newCat;
75+
}
76+
}
77+
78+
return null;
79+
}
80+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"uuid": "9284ca1e-9403-42c1-9a3f-81e5384a6925",
4+
"name": "Grumpy Cat"
5+
},
6+
{
7+
"uuid": "7f0be8a6-8d7c-4404-9733-8e81106d2874",
8+
"name": "Garfield"
9+
},
10+
{
11+
"uuid": "9232bd16-ce13-4dcc-afa3-aa4190f861e6",
12+
"name": "Hello Kitty"
13+
},
14+
{
15+
"uuid": "02bc0196-5e15-4dd1-94a1-6f6f7f12d25c",
16+
"name": "Grominet"
17+
},
18+
{
19+
"uuid": "a1399187-668a-45ef-9262-b5e1dc7b927f",
20+
"name": "Tom"
21+
},
22+
{
23+
"uuid": "8ad8dbb2-5c2c-46bd-b490-3a6d294c91bd",
24+
"name": "Lucifer"
25+
}
26+
]

0 commit comments

Comments
 (0)