Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API access Command #33833

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/Adapter/ApiAccess/CommandHandler/AddApiAccessHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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)->getMessage(),
$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);
}
}
92 changes: 92 additions & 0 deletions src/Adapter/ApiAccess/CommandHandler/EditApiAccessHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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
tleon marked this conversation as resolved.
Show resolved Hide resolved
{
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)->getMessage(),
$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;
}
}