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; } }