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 7, 2023
1 parent bff07cc commit dc583b4
Show file tree
Hide file tree
Showing 21 changed files with 1,032 additions and 17 deletions.
61 changes: 61 additions & 0 deletions src/Adapter/ApiAccess/CommandHandler/AddApiAccessHandler.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\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\ValueObject\ApiClientId;
use PrestaShopBundle\Entity\ApiAccess;
use PrestaShopBundle\Entity\Repository\ApiAccessRepository;

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

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

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

$savedApiAccess = $this->repository->save($apiAccess);

return new ApiClientId($savedApiAccess);
}
}
65 changes: 65 additions & 0 deletions src/Adapter/ApiAccess/CommandHandler/EditApiAccessHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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 PrestaShopBundle\Entity\Repository\ApiAccessRepository;

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

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

public function handle(EditApiAccessCommand $command): void
{
$apiAccess = $this->repository->findOneBy(['clientId' => $command->getApiClientId()->getValue()]);

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

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

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

$this->repository->save($apiAccess);
}
}
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\ApiClientId;
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(['clientId' => $query->getApiAccessId()->getValue()]);

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

return new EditableApiAccess(
new ApiClientId((string) $apiAccess->getId()),
$apiAccess->getClientId(),
$apiAccess->getClientName(),
$apiAccess->isActive(),
$apiAccess->getDescription()
);
}
}
75 changes: 75 additions & 0 deletions src/Core/Domain/ApiAccess/Command/AddApiAccessCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\ApiClientId;

class AddApiAccessCommand
{
private string $clientName;

private ApiClientId $apiClientId;

private bool $enabled;

private string $description;

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

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

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

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

public function getDescription(): string
{
return $this->description;
}
}
96 changes: 96 additions & 0 deletions src/Core/Domain/ApiAccess/Command/EditApiAccessCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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\ApiClientId;

class EditApiAccessCommand
{
private ApiClientId $apiClientId;

private ?string $clientName = null;

private ?bool $enabled = null;

private ?string $description = null;

public function __construct($apiClientId)
{
$this->apiClientId = new ApiClientId($apiClientId);
}

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

public function setApiClientId(ApiClientId $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 dc583b4

Please sign in to comment.