|
| 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 | +} |
0 commit comments