. */ namespace SP\Domain\Plugin\Services; use SP\Core\Application; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\ItemData; use SP\DataModel\ItemSearchData; use SP\Domain\Common\Services\Service; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Plugin\In\PluginRepositoryInterface; use SP\Domain\Plugin\PluginServiceInterface; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\QueryResult; use SP\Infrastructure\Plugin\Repositories\PluginModel; use SP\Infrastructure\Plugin\Repositories\PluginRepository; /** * Class PluginService * * @package SP\Domain\Plugin\Services */ final class PluginService extends Service implements PluginServiceInterface { private PluginRepository $pluginRepository; public function __construct(Application $application, PluginRepositoryInterface $pluginRepository) { parent::__construct($application); $this->pluginRepository = $pluginRepository; } /** * Creates an item * * @throws ConstraintException * @throws QueryException */ public function create(PluginModel $itemData): int { return $this->pluginRepository->create($itemData)->getLastId(); } /** * Updates an item * * @throws ConstraintException * @throws QueryException */ public function update(PluginModel $itemData): int { return $this->pluginRepository->update($itemData); } /** * Returns the item for given id * * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ public function getById(int $id): PluginModel { $result = $this->pluginRepository->getById($id); if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('Plugin not found'), SPException::INFO); } return $result->getData(); } /** * Returns all the items * * @return PluginModel[] * @throws ConstraintException * @throws QueryException */ public function getAll(): array { return $this->pluginRepository->getAll()->getDataAsArray(); } /** * Returns all the items for given ids * * @param int[] $ids * * @return PluginModel[] * @throws ConstraintException * @throws QueryException */ public function getByIdBatch(array $ids): array { return $this->pluginRepository->getByIdBatch($ids)->getDataAsArray(); } /** * Deletes all the items for given ids * * @param int[] $ids * * @throws SPException * @throws ConstraintException * @throws QueryException * @throws SPException */ public function deleteByIdBatch(array $ids): void { if ($this->pluginRepository->deleteByIdBatch($ids) !== count($ids)) { throw new ServiceException(__u('Error while deleting the plugins')); } } /** * Deletes an item * * @throws SPException * @throws ConstraintException * @throws QueryException */ public function delete(int $id): void { if ($this->pluginRepository->delete($id) === 0) { throw new NoSuchItemException(__u('Plugin not found'), SPException::INFO); } } /** * Searches for items by a given filter * * @throws ConstraintException * @throws QueryException */ public function search(ItemSearchData $itemSearchData): QueryResult { return $this->pluginRepository->search($itemSearchData); } /** * Devuelve los datos de un plugin por su nombre * * @throws NoSuchItemException * @throws ConstraintException * @throws QueryException */ public function getByName(string $name): PluginModel { $result = $this->pluginRepository->getByName($name); if ($result->getNumRows() === 0) { throw new NoSuchItemException(__u('Plugin not found'), SPException::INFO); } return $result->getData(); } /** * Cambiar el estado del plugin * * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Infrastructure\Common\Repositories\NoSuchItemException */ public function toggleEnabled(int $id, bool $enabled): void { if ($this->pluginRepository->toggleEnabled($id, $enabled) === 0) { throw new NoSuchItemException(__u('Plugin not found'), SPException::INFO); } } /** * Cambiar el estado del plugin * * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Infrastructure\Common\Repositories\NoSuchItemException */ public function toggleEnabledByName(string $name, bool $enabled): void { if ($this->pluginRepository->toggleEnabledByName($name, $enabled) === 0) { throw new NoSuchItemException(__u('Plugin not found'), SPException::INFO); } } /** * Cambiar el estado del plugin * * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Infrastructure\Common\Repositories\NoSuchItemException */ public function toggleAvailable(int $id, bool $available): void { if ($this->pluginRepository->toggleAvailable($id, $available) === 0) { throw new NoSuchItemException(__u('Plugin not found'), SPException::INFO); } } /** * Cambiar el estado del plugin * * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Infrastructure\Common\Repositories\NoSuchItemException */ public function toggleAvailableByName(string $name, bool $available): void { if ($this->pluginRepository->toggleAvailableByName($name, $available) === 0) { throw new NoSuchItemException(__u('Plugin not found'), SPException::INFO); } } /** * Restablecer los datos de un plugin * * @throws NoSuchItemException * @throws ConstraintException * @throws QueryException */ public function resetById(int $id): bool { if (($count = $this->pluginRepository->resetById($id)) === 0) { throw new NoSuchItemException(__u('Plugin not found'), SPException::INFO); } return $count; } /** * Devolver los plugins activados * * @return ItemData[] * @throws ConstraintException * @throws QueryException */ public function getEnabled(): array { return $this->pluginRepository->getEnabled()->getDataAsArray(); } }