Skip to content

Commit 8bccec9

Browse files
authored
Merge pull request PrestaShop#65 from Progi1984/webserviceKey
Added endpoints for domain "WebserviceKey"
2 parents fbc3508 + 7690d4d commit 8bccec9

12 files changed

+635
-9
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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 Academic Free License version 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/AFL-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+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\WebserviceKey;
24+
25+
use ApiPlatform\Metadata\ApiProperty;
26+
use ApiPlatform\Metadata\ApiResource;
27+
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\AddWebserviceKeyCommand;
28+
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\EditWebserviceKeyCommand;
29+
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceConstraintException;
30+
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceKeyNotFoundException;
31+
use PrestaShop\PrestaShop\Core\Domain\Webservice\Query\GetWebserviceKeyForEditing;
32+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate;
33+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
34+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate;
35+
use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType;
36+
use Symfony\Component\HttpFoundation\Response;
37+
use Symfony\Component\Validator\Constraints as Assert;
38+
39+
#[ApiResource(
40+
operations: [
41+
new CQRSCreate(
42+
uriTemplate: '/webservice-key',
43+
validationContext: ['groups' => ['Default', 'Create']],
44+
CQRSCommand: AddWebserviceKeyCommand::class,
45+
scopes: ['webservice_key_write'],
46+
CQRSCommandMapping: [
47+
'[shopIds]' => '[associatedShops]',
48+
'[enabled]' => '[status]',
49+
],
50+
),
51+
new CQRSGet(
52+
uriTemplate: '/webservice-key/{webserviceKeyId}',
53+
requirements: ['webserviceKeyId' => '\d+'],
54+
CQRSQuery: GetWebserviceKeyForEditing::class,
55+
scopes: ['webservice_key_read'],
56+
CQRSQueryMapping: self::QUERY_MAPPING,
57+
),
58+
new CQRSPartialUpdate(
59+
uriTemplate: '/webservice-key/{webserviceKeyId}',
60+
read: false,
61+
CQRSCommand: EditWebserviceKeyCommand::class,
62+
CQRSQuery: GetWebserviceKeyForEditing::class,
63+
scopes: ['webservice_key_write'],
64+
CQRSCommandMapping: [
65+
'[shopIds]' => '[shopAssociation]',
66+
'[enabled]' => '[status]',
67+
],
68+
CQRSQueryMapping: self::QUERY_MAPPING,
69+
),
70+
],
71+
normalizationContext: ['skip_null_values' => false],
72+
exceptionToStatus: [
73+
WebserviceKeyNotFoundException::class => Response::HTTP_NOT_FOUND,
74+
WebserviceConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
75+
],
76+
)]
77+
class WebserviceKey
78+
{
79+
#[ApiProperty(identifier: true)]
80+
public int $webserviceKeyId;
81+
82+
#[Assert\NotBlank(groups: ['Create'])]
83+
#[Assert\Length(min: 32, max: 32)]
84+
public string $key;
85+
86+
#[Assert\NotBlank(groups: ['Create'])]
87+
#[Assert\Length(min: 1, max: FormattedTextareaType::LIMIT_MEDIUMTEXT_UTF8_MB4)]
88+
public string $description;
89+
90+
#[Assert\NotNull(groups: ['Create'])]
91+
public bool $enabled;
92+
93+
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'array']])]
94+
#[Assert\Collection(
95+
fields: [
96+
'DELETE' => new Assert\All(
97+
constraints: [
98+
new Assert\Type(type: 'string'),
99+
],
100+
groups: ['Create']
101+
),
102+
'GET' => new Assert\All(
103+
constraints: [
104+
new Assert\Type(type: 'string'),
105+
],
106+
groups: ['Create']
107+
),
108+
'HEAD' => new Assert\All(
109+
constraints: [
110+
new Assert\Type(type: 'string'),
111+
],
112+
groups: ['Create']
113+
),
114+
'PATCH' => new Assert\All(
115+
constraints: [
116+
new Assert\Type(type: 'string'),
117+
],
118+
groups: ['Create']
119+
),
120+
'PUT' => new Assert\All(
121+
constraints: [
122+
new Assert\Type(type: 'string'),
123+
],
124+
groups: ['Create']
125+
),
126+
'POST' => new Assert\All(
127+
constraints: [
128+
new Assert\Type(type: 'string'),
129+
],
130+
groups: ['Create']
131+
),
132+
],
133+
allowMissingFields: true,
134+
groups: ['Create'])]
135+
public array $permissions;
136+
137+
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
138+
#[Assert\NotBlank(allowNull: true)]
139+
public array $shopIds;
140+
141+
public const QUERY_MAPPING = [
142+
'[status]' => '[enabled]',
143+
'[resourcePermissions]' => '[permissions]',
144+
'[associatedShops]' => '[shopIds]',
145+
];
146+
147+
public function setEnabled(string|bool $enabled): self
148+
{
149+
$this->enabled = (bool) $enabled;
150+
151+
return $this;
152+
}
153+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 Academic Free License version 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/AFL-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+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\WebserviceKey;
24+
25+
use ApiPlatform\Metadata\ApiProperty;
26+
use ApiPlatform\Metadata\ApiResource;
27+
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceKeyNotFoundException;
28+
use PrestaShop\PrestaShop\Core\Search\Filters\WebserviceKeyFilters;
29+
use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList;
30+
use PrestaShopBundle\ApiPlatform\Provider\QueryListProvider;
31+
use Symfony\Component\HttpFoundation\Response;
32+
33+
#[ApiResource(
34+
operations: [
35+
new PaginatedList(
36+
uriTemplate: '/webservice-keys',
37+
provider: QueryListProvider::class,
38+
scopes: ['webservice_key_read'],
39+
ApiResourceMapping: [
40+
'[id_webservice_account]' => '[webserviceKeyId]',
41+
'[active]' => '[enabled]',
42+
],
43+
gridDataFactory: 'prestashop.core.grid.data_factory.webservice_key',
44+
filtersClass: WebserviceKeyFilters::class,
45+
),
46+
],
47+
exceptionToStatus: [
48+
WebserviceKeyNotFoundException::class => Response::HTTP_NOT_FOUND,
49+
],
50+
)]
51+
class WebserviceKeyList
52+
{
53+
#[ApiProperty(identifier: true)]
54+
public int $webserviceKeyId;
55+
56+
public string $key;
57+
58+
public string $description;
59+
60+
public bool $enabled;
61+
}

tests/Integration/ApiPlatform/ApiClientEndpointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static function setUpBeforeClass(): void
3535
self::createApiClient(['api_client_write', 'api_client_read']);
3636
}
3737

38-
public function getProtectedEndpoints(): iterable
38+
public static function getProtectedEndpoints(): iterable
3939
{
4040
yield 'get endpoint' => [
4141
'GET',

tests/Integration/ApiPlatform/ApiTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function testProtectedEndpoints(string $method, string $uri, string $cont
120120
*
121121
* @return iterable
122122
*/
123-
abstract public function getProtectedEndpoints(): iterable;
123+
abstract public static function getProtectedEndpoints(): iterable;
124124

125125
protected static function createClient(array $kernelOptions = [], array $defaultOptions = []): Client
126126
{

tests/Integration/ApiPlatform/AttributeGroupEndpointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected static function resetTables(): void
5454
]);
5555
}
5656

57-
public function getProtectedEndpoints(): iterable
57+
public static function getProtectedEndpoints(): iterable
5858
{
5959
yield 'get endpoint' => [
6060
'GET',

tests/Integration/ApiPlatform/CustomerGroupEndpointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function tearDownAfterClass(): void
4444
DatabaseDump::restoreTables(['group', 'group_lang', 'group_reduction', 'group_shop', 'category_group']);
4545
}
4646

47-
public function getProtectedEndpoints(): iterable
47+
public static function getProtectedEndpoints(): iterable
4848
{
4949
yield 'get endpoint' => [
5050
'GET',

tests/Integration/ApiPlatform/DiscountEndpointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function testDeleteDiscount(): void
211211
self::assertResponseStatusCodeSame(204);
212212
}
213213

214-
public function getProtectedEndpoints(): iterable
214+
public static function getProtectedEndpoints(): iterable
215215
{
216216
yield 'get endpoint' => [
217217
'GET',

tests/Integration/ApiPlatform/HookEndpointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function tearDownAfterClass(): void
4343
DatabaseDump::restoreTables(['hook']);
4444
}
4545

46-
public function getProtectedEndpoints(): iterable
46+
public static function getProtectedEndpoints(): iterable
4747
{
4848
yield 'get endpoint' => [
4949
'GET',

tests/Integration/ApiPlatform/ModuleEndpointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function tearDownAfterClass(): void
5353
(new ResourceResetter())->resetTestModules();
5454
}
5555

56-
public function getProtectedEndpoints(): iterable
56+
public static function getProtectedEndpoints(): iterable
5757
{
5858
yield 'get endpoint' => [
5959
'GET',

tests/Integration/ApiPlatform/ProductEndpointTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public static function tearDownAfterClass(): void
145145
(new ResourceResetter())->resetTestModules();
146146
}
147147

148-
public function getProtectedEndpoints(): iterable
148+
public static function getProtectedEndpoints(): iterable
149149
{
150150
yield 'get endpoint' => [
151151
'GET',

0 commit comments

Comments
 (0)