123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\CatalogRule\Model;
- use Magento\CatalogRule\Api\Data;
- use Magento\Framework\Exception\CouldNotDeleteException;
- use Magento\Framework\Exception\CouldNotSaveException;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Exception\ValidatorException;
- class CatalogRuleRepository implements \Magento\CatalogRule\Api\CatalogRuleRepositoryInterface
- {
- /**
- * @var ResourceModel\Rule
- */
- protected $ruleResource;
- /**
- * @var RuleFactory
- */
- protected $ruleFactory;
- /**
- * @var array
- */
- private $rules = [];
- /**
- * @param ResourceModel\Rule $ruleResource
- * @param RuleFactory $ruleFactory
- */
- public function __construct(
- \Magento\CatalogRule\Model\ResourceModel\Rule $ruleResource,
- \Magento\CatalogRule\Model\RuleFactory $ruleFactory
- ) {
- $this->ruleResource = $ruleResource;
- $this->ruleFactory = $ruleFactory;
- }
- /**
- * {@inheritdoc}
- */
- public function save(Data\RuleInterface $rule)
- {
- if ($rule->getRuleId()) {
- $rule = $this->get($rule->getRuleId())->addData($rule->getData());
- }
- try {
- $this->ruleResource->save($rule);
- unset($this->rules[$rule->getId()]);
- } catch (ValidatorException $e) {
- throw new CouldNotSaveException(__($e->getMessage()));
- } catch (\Exception $e) {
- throw new CouldNotSaveException(
- __('The "%1" rule was unable to be saved. Please try again.', $rule->getRuleId())
- );
- }
- return $rule;
- }
- /**
- * {@inheritdoc}
- */
- public function get($ruleId)
- {
- if (!isset($this->rules[$ruleId])) {
- /** @var \Magento\CatalogRule\Model\Rule $rule */
- $rule = $this->ruleFactory->create();
- /* TODO: change to resource model after entity manager will be fixed */
- $rule->load($ruleId);
- if (!$rule->getRuleId()) {
- throw new NoSuchEntityException(
- __('The rule with the "%1" ID wasn\'t found. Verify the ID and try again.', $ruleId)
- );
- }
- $this->rules[$ruleId] = $rule;
- }
- return $this->rules[$ruleId];
- }
- /**
- * {@inheritdoc}
- */
- public function delete(Data\RuleInterface $rule)
- {
- try {
- $this->ruleResource->delete($rule);
- unset($this->rules[$rule->getId()]);
- } catch (ValidatorException $e) {
- throw new CouldNotSaveException(__($e->getMessage()));
- } catch (\Exception $e) {
- throw new CouldNotDeleteException(__('The "%1" rule couldn\'t be removed.', $rule->getRuleId()));
- }
- return true;
- }
- /**
- * {@inheritdoc}
- */
- public function deleteById($ruleId)
- {
- $model = $this->get($ruleId);
- $this->delete($model);
- return true;
- }
- }
|