Skip to content

Commit

Permalink
feat(api-access): add api access command
Browse files Browse the repository at this point in the history
  • Loading branch information
tleon committed Sep 20, 2023
1 parent bff07cc commit f1c706a
Show file tree
Hide file tree
Showing 23 changed files with 1,196 additions and 48 deletions.
91 changes: 91 additions & 0 deletions src/Adapter/ApiAccess/CommandHandler/AddApiAccessHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Adapter\ApiAccess\CommandHandler;

use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Command\AddApiAccessCommand;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\CommandHandler\AddApiAccessCommandHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Exception\ApiAccessConstraintException;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Exception\ApiAccessException;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\ValueObject\ApiAccessId;
use PrestaShopBundle\Entity\ApiAccess;
use PrestaShopBundle\Entity\Repository\ApiAccessRepository;
use Symfony\Component\Validator\Validator\ValidatorInterface;

#[AsCommandHandler]
class AddApiAccessHandler implements AddApiAccessCommandHandlerInterface
{
private ApiAccessRepository $repository;
private ValidatorInterface $validator;

public function __construct(ApiAccessRepository $repository, ValidatorInterface $validator)
{
$this->repository = $repository;
$this->validator = $validator;
}

public function handle(AddApiAccessCommand $command): ApiAccessId
{
$apiAccess = new ApiAccess();
$apiAccess->setClientId($command->getApiClientId());
$apiAccess->setClientName($command->getClientName());
$apiAccess->setEnabled($command->isEnabled());
$apiAccess->setDescription($command->getDescription());

$errors = $this->validator->validate($apiAccess);

if (count($errors) > 0) {
/**
* We ignore the next line because this is the way symfony recommend to cast the errors array.
*
* @phpstan-ignore-next-line
*/
$errorsString = (string) $errors;

foreach ($errors as $error) {
switch ($error->getMessage()) {
case 'This value is already used.':
/* @phpstan-ignore-next-line */
throw new ApiAccessConstraintException((string) $error, ApiAccessConstraintException::VALUE_ALREADY_USED);
default:
throw new ApiAccessConstraintException($errorsString);
}
}
}

try {
$savedApiAccess = $this->repository->save($apiAccess);
} catch (\Exception $e) {
throw new ApiAccessException('Could not add Api access', 0, $e);
}

return new ApiAccessId($savedApiAccess);
}
}
100 changes: 100 additions & 0 deletions src/Adapter/ApiAccess/CommandHandler/EditApiAccessHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Adapter\ApiAccess\CommandHandler;

use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Command\EditApiAccessCommand;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\CommandHandler\EditApiAccessCommandHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Exception\ApiAccessConstraintException;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Exception\ApiAccessException;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Exception\ApiAccessNotFoundException;
use PrestaShopBundle\Entity\Repository\ApiAccessRepository;
use Symfony\Component\Validator\Validator\ValidatorInterface;

#[AsCommandHandler]
class EditApiAccessHandler implements EditApiAccessCommandHandlerInterface
{
private ApiAccessRepository $repository;
private ValidatorInterface $validator;

public function __construct(ApiAccessRepository $repository, ValidatorInterface $validator)
{
$this->repository = $repository;
$this->validator = $validator;
}

public function handle(EditApiAccessCommand $command): void
{
try {
$apiAccess = $this->repository->findOneBy(['id' => $command->getApiAccessId()->getValue()]);
} catch (\Exception $e) {
throw new ApiAccessNotFoundException(sprintf('Could not find Api access %s', $command->getApiClientId()), 0, $e);
}

if (!empty($command->getClientName())) {
$apiAccess->setClientName($command->getClientName());
}

if (!empty($command->isEnabled())) {
$apiAccess->setActive($command->isEnabled());
}

if (!empty($command->getDescription())) {
$apiAccess->setDescription($command->getDescription());
}

$errors = $this->validator->validate($apiAccess);

if (count($errors) > 0) {
/**
* We ignore the next line because this is the way symfony recommend to cast the errors array.
*
* @phpstan-ignore-next-line
*/
$errorsString = (string) $errors;

foreach ($errors as $error) {
switch ($error->getMessage()) {
case 'This value is already used.':
/* @phpstan-ignore-next-line */
throw new ApiAccessConstraintException((string) $error, ApiAccessConstraintException::VALUE_ALREADY_USED);
default:
throw new ApiAccessConstraintException($errorsString);
}
}
}

try {
$this->repository->save($apiAccess);
} catch (\Exception $e) {
throw new ApiAccessException('Could not add Api access', 0, $e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Adapter\ApiAccess\QueryHandler;

use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsQueryHandler;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Exception\ApiAccessException;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Query\GetApiAccessForEditing;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\QueryHandler\GetApiAccessForEditingHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\QueryResult\EditableApiAccess;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\ValueObject\ApiAccessId;
use PrestaShopBundle\Entity\Repository\ApiAccessRepository;

#[AsQueryHandler]
class GetApiAccessForEditingHandler implements GetApiAccessForEditingHandlerInterface
{
private ApiAccessRepository $repository;

public function __construct(ApiAccessRepository $repository)
{
$this->repository = $repository;
}

public function handle(GetApiAccessForEditing $query): EditableApiAccess
{
$apiAccess = $this->repository->findOneBy(['id' => $query->getApiAccessId()->getValue()]);

if (empty($apiAccess->getId())) {
throw new ApiAccessException('ApiAccess Could not be found.');
}

return new EditableApiAccess(
new ApiAccessId($apiAccess->getId()),
$apiAccess->getClientId(),
$apiAccess->getClientName(),
$apiAccess->isEnabled(),
$apiAccess->getDescription()
);
}
}
73 changes: 73 additions & 0 deletions src/Core/Domain/ApiAccess/Command/AddApiAccessCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Core\Domain\ApiAccess\Command;

class AddApiAccessCommand
{
private string $clientName;

private string $apiClientId;

private bool $enabled;

private string $description;

public function __construct(
string $clientName,
string $apiClientId,
bool $enabled,
string $description
) {
$this->clientName = $clientName;
$this->apiClientId = $apiClientId;
$this->enabled = $enabled;
$this->description = $description;
}

public function getClientName(): string
{
return $this->clientName;
}

public function getApiClientId(): string
{
return $this->apiClientId;
}

public function isEnabled(): bool
{
return $this->enabled;
}

public function getDescription(): string
{
return $this->description;
}
}

0 comments on commit f1c706a

Please sign in to comment.