. */ namespace SP\Plugin; use Defuse\Crypto\Exception\CryptoException; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\NoSuchPropertyException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Plugin\Ports\PluginDataServiceInterface; use SP\Domain\Plugin\Services\PluginDataService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Plugin\Repositories\PluginDataModel; /** * Class PluginOperation * * @package SP\Plugin */ final class PluginOperation { private PluginDataService $pluginDataService; private string $pluginName; /** * PluginOperation constructor. * * @param PluginDataServiceInterface $pluginDataService * @param string $pluginName */ public function __construct( PluginDataServiceInterface $pluginDataService, string $pluginName ) { $this->pluginDataService = $pluginDataService; $this->pluginName = $pluginName; } /** * @param int $itemId * @param mixed $data * * @return int * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function create(int $itemId, $data): int { $itemData = new PluginDataModel(); $itemData->setName($this->pluginName); $itemData->setItemId($itemId); $itemData->setData(serialize($data)); return $this->pluginDataService->create($itemData)->getLastId(); } /** * @param int $itemId * @param mixed $data * * @return int * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function update(int $itemId, $data): int { $itemData = new PluginDataModel(); $itemData->setName($this->pluginName); $itemData->setItemId($itemId); $itemData->setData(serialize($data)); return $this->pluginDataService->update($itemData); } /** * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ public function delete(int $itemId): void { $this->pluginDataService->deleteByItemId($this->pluginName, $itemId); } /** * @throws ConstraintException * @throws CryptoException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function get(int $itemId, ?string $class = null) { try { return $this->pluginDataService ->getByItemId($this->pluginName, $itemId) ->hydrate($class); } catch (NoSuchItemException $e) { return null; } } }