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 28, 2023
1 parent dd87f6c commit 2ca8686
Show file tree
Hide file tree
Showing 25 changed files with 1,545 additions and 54 deletions.
74 changes: 74 additions & 0 deletions src/Adapter/ApiAccess/CommandHandler/AddApiAccessHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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 Doctrine\ORM\Exception\ORMException;
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\CannotAddApiAccessException;
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
{
public function __construct(
private readonly ApiAccessRepository $repository,
private readonly ValidatorInterface $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) {
throw ApiAccessConstraintException::buildFromPropertyPath($errors->get(0)->getPropertyPath(), $errors->get(0)->getMessageTemplate());
}

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

return new ApiAccessId($savedApiAccess);
}
}
88 changes: 88 additions & 0 deletions src/Adapter/ApiAccess/CommandHandler/EditApiAccessHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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 Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\NoResultException;
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\ApiAccessNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Exception\CannotUpdateApiAccessException;
use PrestaShopBundle\Entity\Repository\ApiAccessRepository;
use Symfony\Component\Validator\Validator\ValidatorInterface;

#[AsCommandHandler]
class EditApiAccessHandler implements EditApiAccessCommandHandlerInterface
{
public function __construct(
private readonly ApiAccessRepository $repository,
private readonly ValidatorInterface $validator
) {
}

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

if (!is_null($command->getApiClientId())) {
$apiAccess->setClientId($command->getApiClientId());
}

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

if (!is_null($command->isEnabled())) {
$apiAccess->setEnabled($command->isEnabled());
}

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

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

if (count($errors) > 0) {
throw ApiAccessConstraintException::buildFromPropertyPath($errors->get(0)->getPropertyPath(), $errors->get(0)->getMessageTemplate());
}

try {
$this->repository->save($apiAccess);
} catch (ORMException $e) {
throw new CannotUpdateApiAccessException('Could not update Api access', 0, $e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 Doctrine\ORM\NoResultException;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsQueryHandler;
use PrestaShop\PrestaShop\Core\Domain\ApiAccess\Exception\ApiAccessNotFoundException;
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 PrestaShopBundle\Entity\Repository\ApiAccessRepository;

#[AsQueryHandler]
class GetApiAccessForEditingHandler implements GetApiAccessForEditingHandlerInterface
{
public function __construct(private readonly ApiAccessRepository $repository)
{
}

public function handle(GetApiAccessForEditing $query): EditableApiAccess
{
try {
$apiAccess = $this->repository->findOneBy(['id' => $query->getApiAccessId()->getValue()]);
} catch (NoResultException $e) {
throw new ApiAccessNotFoundException(sprintf('Could not find Api access %s', $query->getApiAccessId()->getValue()), 0, $e);
}

return new EditableApiAccess(
$apiAccess->getId(),
$apiAccess->getClientId(),
$apiAccess->getClientName(),
$apiAccess->isEnabled(),
$apiAccess->getDescription()
);
}
}
61 changes: 61 additions & 0 deletions src/Core/Domain/ApiAccess/Command/AddApiAccessCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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
{
public function __construct(
private readonly string $clientName,
private readonly string $apiClientId,
private readonly bool $enabled,
private readonly string $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;
}
}
103 changes: 103 additions & 0 deletions src/Core/Domain/ApiAccess/Command/EditApiAccessCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?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;

use PrestaShop\PrestaShop\Core\Domain\ApiAccess\ValueObject\ApiAccessId;

class EditApiAccessCommand
{
private ApiAccessId $apiAccessId;

private ?string $apiClientId = null;

private ?string $clientName = null;

private ?bool $enabled = null;

private ?string $description = null;

public function __construct(int $apiAccessId)
{
$this->apiAccessId = new ApiAccessId($apiAccessId);
}

public function getApiAccessId(): ApiAccessId
{
return $this->apiAccessId;
}

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

public function setApiClientId(string $apiClientId): self
{
$this->apiClientId = $apiClientId;

return $this;
}

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

public function setClientName(string $clientName): self
{
$this->clientName = $clientName;

return $this;
}

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

public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;

return $this;
}

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

public function setDescription(string $description): self
{
$this->description = $description;

return $this;
}
}

0 comments on commit 2ca8686

Please sign in to comment.