From 3de717d40f68ad94e399a910aa922e9eb4bf2070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sat, 11 Jun 2022 10:13:48 +0200 Subject: [PATCH] refactor: [WIP] Migrate items controller. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- .../Items/AccountUserController.php | 89 ++++++++ .../Items/CategoriesController.php | 74 +++++++ .../Controllers/Items/ClientsController.php | 71 +++++++ .../web/Controllers/Items/ItemsController.php | 201 ------------------ .../Items/NotificationsController.php | 97 +++++++++ .../web/Controllers/Items/TagsController.php | 74 +++++++ .../web/Controllers/SimpleControllerBase.php | 6 +- 7 files changed, 408 insertions(+), 204 deletions(-) create mode 100644 app/modules/web/Controllers/Items/AccountUserController.php create mode 100644 app/modules/web/Controllers/Items/CategoriesController.php create mode 100644 app/modules/web/Controllers/Items/ClientsController.php delete mode 100644 app/modules/web/Controllers/Items/ItemsController.php create mode 100644 app/modules/web/Controllers/Items/NotificationsController.php create mode 100644 app/modules/web/Controllers/Items/TagsController.php diff --git a/app/modules/web/Controllers/Items/AccountUserController.php b/app/modules/web/Controllers/Items/AccountUserController.php new file mode 100644 index 00000000..0824c2a1 --- /dev/null +++ b/app/modules/web/Controllers/Items/AccountUserController.php @@ -0,0 +1,89 @@ +. + */ + +namespace SP\Modules\Web\Controllers\Items; + +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Application; +use SP\Core\PhpExtensionChecker; +use SP\Core\UI\ThemeInterface; +use SP\Domain\Account\AccountServiceInterface; +use SP\Http\Json; +use SP\Http\JsonResponse; +use SP\Http\RequestInterface; +use SP\Modules\Web\Controllers\SimpleControllerBase; +use stdClass; + +/** + * Class AccountUserController + */ +final class AccountUserController extends SimpleControllerBase +{ + private AccountServiceInterface $accountService; + + public function __construct( + Application $application, + ThemeInterface $theme, + Klein $router, + Acl $acl, + RequestInterface $request, + PhpExtensionChecker $extensionChecker, + AccountServiceInterface $accountService + ) { + parent::__construct($application, $theme, $router, $acl, $request, $extensionChecker); + + $this->checks(); + + $this->accountService = $accountService; + } + + /** + * Devolver las cuentas visibles por el usuario + * + * @param int|null $accountId + * + * @throws \JsonException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function accountsUserAction(?int $accountId = null): void + { + $outItems = []; + + foreach ($this->accountService->getForUser($accountId) as $account) { + $obj = new stdClass(); + $obj->id = $account->id; + $obj->name = $account->clientName.' - '.$account->name; + + $outItems[] = $obj; + } + + $jsonResponse = new JsonResponse(); + $jsonResponse->setStatus(0); + $jsonResponse->setData($outItems); + + Json::factory($this->router->response())->returnJson($jsonResponse); + } +} \ No newline at end of file diff --git a/app/modules/web/Controllers/Items/CategoriesController.php b/app/modules/web/Controllers/Items/CategoriesController.php new file mode 100644 index 00000000..a8de3c55 --- /dev/null +++ b/app/modules/web/Controllers/Items/CategoriesController.php @@ -0,0 +1,74 @@ +. + */ + +namespace SP\Modules\Web\Controllers\Items; + +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Application; +use SP\Core\Exceptions\ConstraintException; +use SP\Core\Exceptions\QueryException; +use SP\Core\Exceptions\SPException; +use SP\Core\PhpExtensionChecker; +use SP\Core\UI\ThemeInterface; +use SP\Domain\Category\CategoryServiceInterface; +use SP\Http\Json; +use SP\Http\RequestInterface; +use SP\Modules\Web\Controllers\SimpleControllerBase; +use SP\Mvc\View\Components\SelectItemAdapter; + +/** + * Class CategoriesController + */ +final class CategoriesController extends SimpleControllerBase +{ + private CategoryServiceInterface $categoryService; + + public function __construct( + Application $application, + ThemeInterface $theme, + Klein $router, + Acl $acl, + RequestInterface $request, + PhpExtensionChecker $extensionChecker, + CategoryServiceInterface $categoryService + ) { + parent::__construct($application, $theme, $router, $acl, $request, $extensionChecker); + + $this->checks(); + + $this->categoryService = $categoryService; + } + + /** + * @throws ConstraintException + * @throws QueryException + * @throws SPException + */ + public function categoriesAction(): void + { + Json::factory($this->router->response()) + ->returnRawJson(SelectItemAdapter::factory($this->categoryService->getAllBasic())->getJsonItemsFromModel()); + } +} \ No newline at end of file diff --git a/app/modules/web/Controllers/Items/ClientsController.php b/app/modules/web/Controllers/Items/ClientsController.php new file mode 100644 index 00000000..7b7859dd --- /dev/null +++ b/app/modules/web/Controllers/Items/ClientsController.php @@ -0,0 +1,71 @@ +. + */ + +namespace SP\Modules\Web\Controllers\Items; + +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Application; +use SP\Core\PhpExtensionChecker; +use SP\Core\UI\ThemeInterface; +use SP\Domain\Client\ClientServiceInterface; +use SP\Http\Json; +use SP\Http\RequestInterface; +use SP\Modules\Web\Controllers\SimpleControllerBase; +use SP\Mvc\View\Components\SelectItemAdapter; + +/** + * Class ClientsController + */ +final class ClientsController extends SimpleControllerBase +{ + private ClientServiceInterface $clientService; + + public function __construct( + Application $application, + ThemeInterface $theme, + Klein $router, + Acl $acl, + RequestInterface $request, + PhpExtensionChecker $extensionChecker, + ClientServiceInterface $clientService + ) { + parent::__construct($application, $theme, $router, $acl, $request, $extensionChecker); + + $this->checks(); + + $this->clientService = $clientService; + } + + /** + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + */ + public function clientsAction(): void + { + Json::factory($this->router->response()) + ->returnRawJson(SelectItemAdapter::factory($this->clientService->getAllForUser())->getJsonItemsFromModel()); + } +} \ No newline at end of file diff --git a/app/modules/web/Controllers/Items/ItemsController.php b/app/modules/web/Controllers/Items/ItemsController.php deleted file mode 100644 index 94c39122..00000000 --- a/app/modules/web/Controllers/Items/ItemsController.php +++ /dev/null @@ -1,201 +0,0 @@ -. - */ - -namespace SP\Modules\Web\Controllers\Items; - -use DI\DependencyException; -use DI\NotFoundException; -use SP\Core\Exceptions\ConstraintException; -use SP\Core\Exceptions\QueryException; -use SP\Core\Exceptions\SessionTimeout; -use SP\Core\Exceptions\SPException; -use SP\DataModel\DataModelInterface; -use SP\DataModel\NotificationData; -use SP\Domain\Account\Services\AccountService; -use SP\Domain\Category\Services\CategoryService; -use SP\Domain\Client\Services\ClientService; -use SP\Domain\Notification\Services\NotificationService; -use SP\Domain\Tag\Services\TagService; -use SP\Html\Html; -use SP\Http\Json; -use SP\Http\JsonResponse; -use SP\Modules\Web\Controllers\SimpleControllerBase; -use SP\Mvc\View\Components\SelectItemAdapter; -use stdClass; - -/** - * Class ItemsController - * - * @package SP\Modules\Web\Controllers - */ -final class ItemsController extends SimpleControllerBase -{ - /** - * Devolver las cuentas visibles por el usuario - * - * @param int|null $accountId - * - * @throws \DI\DependencyException - * @throws \DI\NotFoundException - * @throws \JsonException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException - */ - public function accountsUserAction(?int $accountId = null): void - { - $outItems = []; - - foreach ($this->dic->get(AccountService::class)->getForUser($accountId) as $account) { - $obj = new stdClass(); - $obj->id = $account->id; - $obj->name = $account->clientName . ' - ' . $account->name; - - $outItems[] = $obj; - } - - $jsonResponse = new JsonResponse(); - $jsonResponse->setStatus(0); - $jsonResponse->setData($outItems); - - Json::fromDic()->returnJson($jsonResponse); - } - - /** - * @throws DependencyException - * @throws NotFoundException - * @throws ConstraintException - * @throws QueryException - * @throws SPException - */ - public function clientsAction(): void - { - Json::factory($this->router->response()) - ->returnRawJson( - SelectItemAdapter::factory( - $this->dic->get(ClientService::class) - ->getAllForUser())->getJsonItemsFromModel()); - } - - /** - * @throws DependencyException - * @throws NotFoundException - * @throws ConstraintException - * @throws QueryException - * @throws SPException - */ - public function categoriesAction(): void - { - Json::factory($this->router->response()) - ->returnRawJson( - SelectItemAdapter::factory( - $this->dic->get(CategoryService::class) - ->getAllBasic())->getJsonItemsFromModel()); - } - - /** - * @throws \DI\DependencyException - * @throws \DI\NotFoundException - * @throws \JsonException - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException - */ - public function notificationsAction(): void - { - $notifications = array_map( - static function ($notification) { - /** @@var $notification NotificationData */ - return sprintf( - '(%s) - %s', - $notification->getComponent(), - Html::truncate(Html::stripTags($notification->getDescription()), 30) - ); - }, $this->dic - ->get(NotificationService::class) - ->getAllActiveForUserId($this->session->getUserData()->getId())); - - $count = count($notifications); - - $jsonResponse = new JsonResponse(); - $jsonResponse->setStatus(0); - $jsonResponse->setData([ - 'message' => __('There aren\'t any pending notifications'), - 'message_has' => sprintf(__('There are pending notifications: %d'), $count), - 'count' => $count, - 'notifications' => $notifications, - 'hash' => sha1(implode('', $notifications)) - ]); - - Json::factory($this->router->response()) - ->returnJson($jsonResponse); - } - - /** - * @throws DependencyException - * @throws NotFoundException - * @throws ConstraintException - * @throws QueryException - * @throws SPException - */ - public function tagsAction(): void - { - Json::factory($this->router->response()) - ->returnRawJson( - SelectItemAdapter::factory( - $this->dic->get(TagService::class) - ->getAllBasic())->getJsonItemsFromModel()); - } - - /** - * ItemsController constructor. - * - * @throws SessionTimeout - */ - protected function initialize(): void - { - $this->checks(); - } - - /** - * Preparar los elementos para devolverlos - * - * @param array $items - * - * @return array - */ - private function prepareItems(array $items): array - { - $outItems = []; - - /** @var DataModelInterface $item */ - foreach ($items as $item) { - $obj = new stdClass(); - $obj->id = $item->getId(); - $obj->name = $item->getName(); - - $outItems[] = $obj; - } - - return $outItems; - } -} \ No newline at end of file diff --git a/app/modules/web/Controllers/Items/NotificationsController.php b/app/modules/web/Controllers/Items/NotificationsController.php new file mode 100644 index 00000000..f668d442 --- /dev/null +++ b/app/modules/web/Controllers/Items/NotificationsController.php @@ -0,0 +1,97 @@ +. + */ + +namespace SP\Modules\Web\Controllers\Items; + +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Application; +use SP\Core\PhpExtensionChecker; +use SP\Core\UI\ThemeInterface; +use SP\DataModel\NotificationData; +use SP\Domain\Notification\NotificationServiceInterface; +use SP\Html\Html; +use SP\Http\Json; +use SP\Http\JsonResponse; +use SP\Http\RequestInterface; +use SP\Modules\Web\Controllers\SimpleControllerBase; + +/** + * Class NotificationsController + */ +final class NotificationsController extends SimpleControllerBase +{ + private NotificationServiceInterface $notificationService; + + public function __construct( + Application $application, + ThemeInterface $theme, + Klein $router, + Acl $acl, + RequestInterface $request, + PhpExtensionChecker $extensionChecker, + NotificationServiceInterface $notificationService + ) { + parent::__construct($application, $theme, $router, $acl, $request, $extensionChecker); + + $this->checks(); + + $this->notificationService = $notificationService; + } + + + /** + * @throws \JsonException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function notificationsAction(): void + { + $notifications = array_map( + static function ($notification) { + /** @@var $notification NotificationData */ + return sprintf( + '(%s) - %s', + $notification->getComponent(), + Html::truncate(Html::stripTags($notification->getDescription()), 30) + ); + }, + $this->notificationService->getAllActiveForUserId($this->session->getUserData()->getId()) + ); + + $count = count($notifications); + + $jsonResponse = new JsonResponse(); + $jsonResponse->setStatus(0); + $jsonResponse->setData([ + 'message' => __('There aren\'t any pending notifications'), + 'message_has' => sprintf(__('There are pending notifications: %d'), $count), + 'count' => $count, + 'notifications' => $notifications, + 'hash' => sha1(implode('', $notifications)), + ]); + + Json::factory($this->router->response())->returnJson($jsonResponse); + } +} \ No newline at end of file diff --git a/app/modules/web/Controllers/Items/TagsController.php b/app/modules/web/Controllers/Items/TagsController.php new file mode 100644 index 00000000..f0ebd212 --- /dev/null +++ b/app/modules/web/Controllers/Items/TagsController.php @@ -0,0 +1,74 @@ +. + */ + +namespace SP\Modules\Web\Controllers\Items; + +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Application; +use SP\Core\Exceptions\ConstraintException; +use SP\Core\Exceptions\QueryException; +use SP\Core\Exceptions\SPException; +use SP\Core\PhpExtensionChecker; +use SP\Core\UI\ThemeInterface; +use SP\Domain\Tag\TagServiceInterface; +use SP\Http\Json; +use SP\Http\RequestInterface; +use SP\Modules\Web\Controllers\SimpleControllerBase; +use SP\Mvc\View\Components\SelectItemAdapter; + +/** + * Class TagsController + */ +final class TagsController extends SimpleControllerBase +{ + private TagServiceInterface $tagService; + + public function __construct( + Application $application, + ThemeInterface $theme, + Klein $router, + Acl $acl, + RequestInterface $request, + PhpExtensionChecker $extensionChecker, + TagServiceInterface $tagService + ) { + parent::__construct($application, $theme, $router, $acl, $request, $extensionChecker); + + $this->checks(); + + $this->tagService = $tagService; + } + + /** + * @throws ConstraintException + * @throws QueryException + * @throws SPException + */ + public function tagsAction(): void + { + Json::factory($this->router->response()) + ->returnRawJson(SelectItemAdapter::factory($this->tagService->getAllBasic())->getJsonItemsFromModel()); + } +} \ No newline at end of file diff --git a/app/modules/web/Controllers/SimpleControllerBase.php b/app/modules/web/Controllers/SimpleControllerBase.php index 483b9a11..eb6c4239 100644 --- a/app/modules/web/Controllers/SimpleControllerBase.php +++ b/app/modules/web/Controllers/SimpleControllerBase.php @@ -53,9 +53,9 @@ abstract class SimpleControllerBase // TODO: remove when controllers are ready protected ContainerInterface $dic; - protected EventDispatcher $eventDispatcher; - protected ConfigFileService $config; - protected ContextInterface $session; + protected EventDispatcher $eventDispatcher; + protected ConfigFileService $config; + protected ContextInterface $session; protected ThemeInterface $theme; protected Klein $router; protected Acl $acl;