Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CatalogRuleRepository.php
Go to the documentation of this file.
1 <?php
8 
14 
16 {
20  protected $ruleResource;
21 
25  protected $ruleFactory;
26 
30  private $rules = [];
31 
36  public function __construct(
37  \Magento\CatalogRule\Model\ResourceModel\Rule $ruleResource,
38  \Magento\CatalogRule\Model\RuleFactory $ruleFactory
39  ) {
40  $this->ruleResource = $ruleResource;
41  $this->ruleFactory = $ruleFactory;
42  }
43 
47  public function save(Data\RuleInterface $rule)
48  {
49  if ($rule->getRuleId()) {
50  $rule = $this->get($rule->getRuleId())->addData($rule->getData());
51  }
52 
53  try {
54  $this->ruleResource->save($rule);
55  unset($this->rules[$rule->getId()]);
56  } catch (ValidatorException $e) {
57  throw new CouldNotSaveException(__($e->getMessage()));
58  } catch (\Exception $e) {
59  throw new CouldNotSaveException(
60  __('The "%1" rule was unable to be saved. Please try again.', $rule->getRuleId())
61  );
62  }
63  return $rule;
64  }
65 
69  public function get($ruleId)
70  {
71  if (!isset($this->rules[$ruleId])) {
73  $rule = $this->ruleFactory->create();
74 
75  /* TODO: change to resource model after entity manager will be fixed */
76  $rule->load($ruleId);
77  if (!$rule->getRuleId()) {
78  throw new NoSuchEntityException(
79  __('The rule with the "%1" ID wasn\'t found. Verify the ID and try again.', $ruleId)
80  );
81  }
82  $this->rules[$ruleId] = $rule;
83  }
84  return $this->rules[$ruleId];
85  }
86 
90  public function delete(Data\RuleInterface $rule)
91  {
92  try {
93  $this->ruleResource->delete($rule);
94  unset($this->rules[$rule->getId()]);
95  } catch (ValidatorException $e) {
96  throw new CouldNotSaveException(__($e->getMessage()));
97  } catch (\Exception $e) {
98  throw new CouldNotDeleteException(__('The "%1" rule couldn\'t be removed.', $rule->getRuleId()));
99  }
100  return true;
101  }
102 
106  public function deleteById($ruleId)
107  {
108  $model = $this->get($ruleId);
109  $this->delete($model);
110  return true;
111  }
112 }
__construct(\Magento\CatalogRule\Model\ResourceModel\Rule $ruleResource, \Magento\CatalogRule\Model\RuleFactory $ruleFactory)
__()
Definition: __.php:13