diff --git a/app/modules/api/Controllers/AccountController.php b/app/modules/api/Controllers/AccountController.php index 2c7c60be..01979099 100644 --- a/app/modules/api/Controllers/AccountController.php +++ b/app/modules/api/Controllers/AccountController.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -27,7 +27,6 @@ namespace SP\Modules\Api\Controllers; use Exception; use Klein\Klein; use League\Fractal\Resource\Item; -use SP\Adapters\AccountAdapter; use SP\Core\Acl\Acl; use SP\Core\Acl\ActionsInterface; use SP\Core\Application; @@ -37,10 +36,12 @@ use SP\Core\Events\EventMessage; use SP\DataModel\Dto\AccountDetailsResponse; use SP\Domain\Account\AccountPresetServiceInterface; use SP\Domain\Account\AccountServiceInterface; +use SP\Domain\Account\Out\AccountAdapter; use SP\Domain\Account\Services\AccountRequest; use SP\Domain\Account\Services\AccountSearchFilter; use SP\Domain\Api\ApiServiceInterface; use SP\Domain\Api\Services\ApiResponse; +use SP\Domain\CustomField\CustomFieldServiceInterface; use SP\Modules\Api\Controllers\Help\AccountHelp; use SP\Mvc\Controller\ItemTrait; use SP\Mvc\Model\QueryCondition; @@ -57,19 +58,25 @@ final class AccountController extends ControllerBase private AccountPresetServiceInterface $accountPresetService; private AccountServiceInterface $accountService; + private CustomFieldServiceInterface $customFieldService; + /** + * @throws \SP\Core\Exceptions\InvalidClassException + */ public function __construct( Application $application, Klein $router, ApiServiceInterface $apiService, Acl $acl, AccountPresetServiceInterface $accountPresetService, - AccountServiceInterface $accountService + AccountServiceInterface $accountService, + CustomFieldServiceInterface $customFieldService ) { + parent::__construct($application, $router, $apiService, $acl); + $this->accountPresetService = $accountPresetService; $this->accountService = $accountService; - - parent::__construct($application, $router, $apiService, $acl); + $this->customFieldService = $customFieldService; $this->apiService->setHelpClass(AccountHelp::class); } @@ -113,18 +120,15 @@ final class AccountController extends ControllerBase ) ); - $adapter = new AccountAdapter($this->configData); + $adapter = new AccountAdapter($this->configData, $this->customFieldService); - $out = $this->fractal - ->createData(new Item($accountResponse, $adapter)); + $out = $this->fractal->createData(new Item($accountResponse, $adapter)); if ($customFields) { $this->fractal->parseIncludes(['customFields']); } - $this->returnResponse( - ApiResponse::makeSuccess($out->toArray(), $id) - ); + $this->returnResponse(ApiResponse::makeSuccess($out->toArray(), $id)); } catch (Exception $e) { $this->returnResponseException($e); diff --git a/app/modules/api/Controllers/CategoryController.php b/app/modules/api/Controllers/CategoryController.php index a6b91190..86ae41f6 100644 --- a/app/modules/api/Controllers/CategoryController.php +++ b/app/modules/api/Controllers/CategoryController.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -26,13 +26,13 @@ namespace SP\Modules\Api\Controllers; use Exception; use League\Fractal\Resource\Item; -use SP\Adapters\CategoryAdapter; use SP\Core\Acl\ActionsInterface; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\DataModel\CategoryData; use SP\DataModel\ItemSearchData; use SP\Domain\Api\Services\ApiResponse; +use SP\Domain\Category\Out\CategoryAdapter; use SP\Domain\Category\Services\CategoryService; use SP\Modules\Api\Controllers\Help\CategoryHelp; use SP\Mvc\Controller\ItemTrait; diff --git a/app/modules/api/Controllers/ClientController.php b/app/modules/api/Controllers/ClientController.php index 55357f63..08f77e2d 100644 --- a/app/modules/api/Controllers/ClientController.php +++ b/app/modules/api/Controllers/ClientController.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -26,13 +26,13 @@ namespace SP\Modules\Api\Controllers; use Exception; use League\Fractal\Resource\Item; -use SP\Adapters\ClientAdapter; use SP\Core\Acl\ActionsInterface; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\DataModel\ClientData; use SP\DataModel\ItemSearchData; use SP\Domain\Api\Services\ApiResponse; +use SP\Domain\Client\Out\ClientAdapter; use SP\Domain\Client\Services\ClientService; use SP\Modules\Api\Controllers\Help\ClientHelp; use SP\Mvc\Controller\ItemTrait; diff --git a/lib/SP/Adapters/AccountAdapter.php b/lib/SP/Domain/Account/Out/AccountAdapter.php similarity index 87% rename from lib/SP/Adapters/AccountAdapter.php rename to lib/SP/Domain/Account/Out/AccountAdapter.php index 148aeaa8..3a5eff59 100644 --- a/lib/SP/Adapters/AccountAdapter.php +++ b/lib/SP/Domain/Account/Out/AccountAdapter.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,11 +22,14 @@ * along with sysPass. If not, see . */ -namespace SP\Adapters; +namespace SP\Domain\Account\Out; use League\Fractal\Resource\Collection; use SP\Core\Acl\ActionsInterface; use SP\DataModel\Dto\AccountDetailsResponse; +use SP\Domain\Common\Out\AdapterBase; +use SP\Domain\CustomField\CustomFieldServiceInterface; +use SP\Domain\CustomField\Out\CustomFieldAdapter; use SP\Mvc\Controller\ItemTrait; use SP\Mvc\View\Components\SelectItemAdapter; use SP\Util\Link; @@ -36,13 +39,11 @@ use SP\Util\Link; * * @package SP\Adapters */ -final class AccountAdapter extends AdapterBase +final class AccountAdapter extends AdapterBase implements AccountAdapterInterface { use ItemTrait; - protected $availableIncludes = [ - 'customFields' - ]; + protected $availableIncludes = ['customFields']; /** * @throws \SP\Core\Exceptions\ConstraintException @@ -50,10 +51,12 @@ final class AccountAdapter extends AdapterBase * @throws \SP\Core\Exceptions\SPException * @throws \SP\Domain\Common\Services\ServiceException */ - public function includeCustomFields(AccountDetailsResponse $data): Collection - { + public function includeCustomFields( + AccountDetailsResponse $data, + CustomFieldServiceInterface $customFieldService + ): Collection { return $this->collection( - $this->getCustomFieldsForItem(ActionsInterface::ACCOUNT, $data->getId()), + $this->getCustomFieldsForItem(ActionsInterface::ACCOUNT, $data->getId(), $customFieldService), new CustomFieldAdapter($this->configData) ); } diff --git a/lib/SP/Domain/Account/Out/AccountAdapterInterface.php b/lib/SP/Domain/Account/Out/AccountAdapterInterface.php new file mode 100644 index 00000000..3323f844 --- /dev/null +++ b/lib/SP/Domain/Account/Out/AccountAdapterInterface.php @@ -0,0 +1,51 @@ +. + */ + +namespace SP\Domain\Account\Out; + + +use League\Fractal\Resource\Collection; +use SP\DataModel\Dto\AccountDetailsResponse; +use SP\Domain\CustomField\CustomFieldServiceInterface; + +/** + * Class AccountAdapter + * + * @package SP\Adapters + */ +interface AccountAdapterInterface +{ + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + * @throws \SP\Domain\Common\Services\ServiceException + */ + public function includeCustomFields( + AccountDetailsResponse $data, + CustomFieldServiceInterface $customFieldService + ): Collection; + + public function transform(AccountDetailsResponse $data): array; +} \ No newline at end of file diff --git a/lib/SP/Adapters/CategoryAdapter.php b/lib/SP/Domain/Category/Out/CategoryAdapter.php similarity index 78% rename from lib/SP/Adapters/CategoryAdapter.php rename to lib/SP/Domain/Category/Out/CategoryAdapter.php index 03db94cd..46d7f15f 100644 --- a/lib/SP/Adapters/CategoryAdapter.php +++ b/lib/SP/Domain/Category/Out/CategoryAdapter.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,11 +22,14 @@ * along with sysPass. If not, see . */ -namespace SP\Adapters; +namespace SP\Domain\Category\Out; use League\Fractal\Resource\Collection; use SP\Core\Acl\ActionsInterface; use SP\DataModel\CategoryData; +use SP\Domain\Common\Out\AdapterBase; +use SP\Domain\CustomField\CustomFieldServiceInterface; +use SP\Domain\CustomField\Out\CustomFieldAdapter; use SP\Mvc\Controller\ItemTrait; use SP\Util\Link; @@ -35,13 +38,11 @@ use SP\Util\Link; * * @package SP\Adapters */ -final class CategoryAdapter extends AdapterBase +final class CategoryAdapter extends AdapterBase implements CategoryAdapterInterface { use ItemTrait; - protected $availableIncludes = [ - 'customFields' - ]; + protected $availableIncludes = ['customFields']; /** * @throws \SP\Core\Exceptions\ConstraintException @@ -49,10 +50,10 @@ final class CategoryAdapter extends AdapterBase * @throws \SP\Core\Exceptions\SPException * @throws \SP\Domain\Common\Services\ServiceException */ - public function includeCustomFields(CategoryData $data): Collection + public function includeCustomFields(CategoryData $data, CustomFieldServiceInterface $customFieldService): Collection { return $this->collection( - $this->getCustomFieldsForItem(ActionsInterface::CATEGORY, $data->id), + $this->getCustomFieldsForItem(ActionsInterface::CATEGORY, $data->id, $customFieldService), new CustomFieldAdapter($this->configData) ); } diff --git a/lib/SP/Domain/Category/Out/CategoryAdapterInterface.php b/lib/SP/Domain/Category/Out/CategoryAdapterInterface.php new file mode 100644 index 00000000..78dd6264 --- /dev/null +++ b/lib/SP/Domain/Category/Out/CategoryAdapterInterface.php @@ -0,0 +1,51 @@ +. + */ + +namespace SP\Domain\Category\Out; + + +use League\Fractal\Resource\Collection; +use SP\DataModel\CategoryData; +use SP\Domain\CustomField\CustomFieldServiceInterface; + +/** + * Class CategoryAdapter + * + * @package SP\Adapters + */ +interface CategoryAdapterInterface +{ + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + * @throws \SP\Domain\Common\Services\ServiceException + */ + public function includeCustomFields( + CategoryData $data, + CustomFieldServiceInterface $customFieldService + ): Collection; + + public function transform(CategoryData $data): array; +} \ No newline at end of file diff --git a/lib/SP/Adapters/ClientAdapter.php b/lib/SP/Domain/Client/Out/ClientAdapter.php similarity index 79% rename from lib/SP/Adapters/ClientAdapter.php rename to lib/SP/Domain/Client/Out/ClientAdapter.php index cae3c795..12884c4e 100644 --- a/lib/SP/Adapters/ClientAdapter.php +++ b/lib/SP/Domain/Client/Out/ClientAdapter.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,11 +22,14 @@ * along with sysPass. If not, see . */ -namespace SP\Adapters; +namespace SP\Domain\Client\Out; use League\Fractal\Resource\Collection; use SP\Core\Acl\ActionsInterface; use SP\DataModel\ClientData; +use SP\Domain\Common\Out\AdapterBase; +use SP\Domain\CustomField\CustomFieldServiceInterface; +use SP\Domain\CustomField\Out\CustomFieldAdapter; use SP\Mvc\Controller\ItemTrait; use SP\Util\Link; @@ -35,13 +38,11 @@ use SP\Util\Link; * * @package SP\Adapters */ -final class ClientAdapter extends AdapterBase +final class ClientAdapter extends AdapterBase implements ClientAdapterInterface { use ItemTrait; - protected $availableIncludes = [ - 'customFields' - ]; + protected $availableIncludes = ['customFields']; /** * @throws \SP\Core\Exceptions\ConstraintException @@ -49,10 +50,10 @@ final class ClientAdapter extends AdapterBase * @throws \SP\Core\Exceptions\SPException * @throws \SP\Domain\Common\Services\ServiceException */ - public function includeCustomFields(ClientData $data): Collection + public function includeCustomFields(ClientData $data, CustomFieldServiceInterface $customFieldService): Collection { return $this->collection( - $this->getCustomFieldsForItem(ActionsInterface::CLIENT, $data->id), + $this->getCustomFieldsForItem(ActionsInterface::CLIENT, $data->id, $customFieldService), new CustomFieldAdapter($this->configData) ); } diff --git a/lib/SP/Domain/Client/Out/ClientAdapterInterface.php b/lib/SP/Domain/Client/Out/ClientAdapterInterface.php new file mode 100644 index 00000000..6bea8a55 --- /dev/null +++ b/lib/SP/Domain/Client/Out/ClientAdapterInterface.php @@ -0,0 +1,48 @@ +. + */ + +namespace SP\Domain\Client\Out; + + +use League\Fractal\Resource\Collection; +use SP\DataModel\ClientData; +use SP\Domain\CustomField\CustomFieldServiceInterface; + +/** + * Class ClientAdapter + * + * @package SP\Adapters + */ +interface ClientAdapterInterface +{ + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + * @throws \SP\Domain\Common\Services\ServiceException + */ + public function includeCustomFields(ClientData $data, CustomFieldServiceInterface $customFieldService): Collection; + + public function transform(ClientData $data): array; +} \ No newline at end of file diff --git a/lib/SP/Adapters/AdapterBase.php b/lib/SP/Domain/Common/Out/AdapterBase.php similarity index 92% rename from lib/SP/Adapters/AdapterBase.php rename to lib/SP/Domain/Common/Out/AdapterBase.php index 1c02c4e6..9f15351a 100644 --- a/lib/SP/Adapters/AdapterBase.php +++ b/lib/SP/Domain/Common/Out/AdapterBase.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\Adapters; +namespace SP\Domain\Common\Out; use League\Fractal\TransformerAbstract; diff --git a/lib/SP/Adapters/CustomFieldAdapter.php b/lib/SP/Domain/CustomField/Out/CustomFieldAdapter.php similarity index 85% rename from lib/SP/Adapters/CustomFieldAdapter.php rename to lib/SP/Domain/CustomField/Out/CustomFieldAdapter.php index 9b0898f9..1e561cf9 100644 --- a/lib/SP/Adapters/CustomFieldAdapter.php +++ b/lib/SP/Domain/CustomField/Out/CustomFieldAdapter.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,9 +22,10 @@ * along with sysPass. If not, see . */ -namespace SP\Adapters; +namespace SP\Domain\CustomField\Out; +use SP\Domain\Common\Out\AdapterBase; use SP\Domain\CustomField\Services\CustomFieldItem; /** @@ -32,7 +33,7 @@ use SP\Domain\CustomField\Services\CustomFieldItem; * * @package SP\Adapters */ -final class CustomFieldAdapter extends AdapterBase +final class CustomFieldAdapter extends AdapterBase implements CustomFieldAdapterInterface { public function transform(CustomFieldItem $data): array { diff --git a/lib/SP/Domain/CustomField/Out/CustomFieldAdapterInterface.php b/lib/SP/Domain/CustomField/Out/CustomFieldAdapterInterface.php new file mode 100644 index 00000000..476f4389 --- /dev/null +++ b/lib/SP/Domain/CustomField/Out/CustomFieldAdapterInterface.php @@ -0,0 +1,38 @@ +. + */ + +namespace SP\Domain\CustomField\Out; + + +use SP\Domain\CustomField\Services\CustomFieldItem; + +/** + * Class CustomFieldAdapter + * + * @package SP\Adapters + */ +interface CustomFieldAdapterInterface +{ + public function transform(CustomFieldItem $data): array; +} \ No newline at end of file diff --git a/lib/SP/Mvc/Controller/ItemTrait.php b/lib/SP/Mvc/Controller/ItemTrait.php index 59d7708b..d0c8baef 100644 --- a/lib/SP/Mvc/Controller/ItemTrait.php +++ b/lib/SP/Mvc/Controller/ItemTrait.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -96,12 +96,13 @@ trait ItemTrait * @param int $moduleId * @param int|int[] $itemId * @param RequestInterface $request + * @param \SP\Domain\CustomField\CustomFieldServiceInterface $customFieldService * * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Core\Exceptions\SPException - * @throws \SP\Infrastructure\Common\Repositories\NoSuchItemException * @throws \SP\Domain\Common\Services\ServiceException + * @throws \SP\Infrastructure\Common\Repositories\NoSuchItemException */ protected function addCustomFieldsForItem( int $moduleId, @@ -109,17 +110,7 @@ trait ItemTrait RequestInterface $request, CustomFieldServiceInterface $customFieldService ): void { - $customFields = $request->analyzeArray( - 'customfield', - function ($values) { - return array_map( - static function ($value) { - return Filter::getString($value); - }, - $values - ); - } - ); + $customFields = $this->getCustomFieldsFromRequest($request); if (!empty($customFields)) { try { @@ -179,17 +170,7 @@ trait ItemTrait RequestInterface $request, CustomFieldServiceInterface $customFieldService ): void { - $customFields = $request->analyzeArray( - 'customfield', - function ($values) { - return array_map( - static function ($value) { - return Filter::getString($value); - }, - $values - ); - } - ); + $customFields = $this->getCustomFieldsFromRequest($request); if (!empty($customFields)) { try { @@ -227,4 +208,17 @@ trait ItemTrait { return $request->analyzeArray('items'); } + + /** + * @param \SP\Http\RequestInterface $request + * + * @return array|null + */ + private static function getCustomFieldsFromRequest(RequestInterface $request): ?array + { + return $request->analyzeArray( + 'customfield', + fn($values) => array_map(static fn($value) => Filter::getString($value), $values) + ); + } } \ No newline at end of file