Job.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * CatalogRule Rule Job model
  4. *
  5. * Uses for encapsulate some logic of rule model and for having ability change behavior (for example, in controller)
  6. *
  7. * Copyright © Magento, Inc. All rights reserved.
  8. * See COPYING.txt for license details.
  9. */
  10. namespace Magento\CatalogRule\Model\Rule;
  11. use Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor;
  12. /**
  13. * Catalog Rule job model
  14. *
  15. * @method \Magento\CatalogRule\Model\Rule\Job setSuccess(string $errorMessage)
  16. * @method \Magento\CatalogRule\Model\Rule\Job setError(string $errorMessage)
  17. * @method string getSuccess()
  18. * @method string getError()
  19. * @method bool hasSuccess()
  20. * @method bool hasError()
  21. *
  22. * @author Magento Core Team <core@magentocommerce.com>
  23. *
  24. * @api
  25. * @since 100.0.2
  26. */
  27. class Job extends \Magento\Framework\DataObject
  28. {
  29. /**
  30. * @var RuleProductProcessor
  31. */
  32. protected $ruleProcessor;
  33. /**
  34. * Basic object initialization
  35. *
  36. * @param RuleProductProcessor $ruleProcessor
  37. * @param array $data
  38. */
  39. public function __construct(
  40. RuleProductProcessor $ruleProcessor,
  41. array $data = []
  42. ) {
  43. $this->ruleProcessor = $ruleProcessor;
  44. parent::__construct($data);
  45. }
  46. /**
  47. * Dispatch event "catalogrule_apply_all" and set success or error message depends on result
  48. *
  49. * @return \Magento\CatalogRule\Model\Rule\Job
  50. * @api
  51. */
  52. public function applyAll()
  53. {
  54. try {
  55. $this->ruleProcessor->markIndexerAsInvalid();
  56. $this->setSuccess(__('Updated rules applied.'));
  57. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  58. $this->setError($e->getMessage());
  59. }
  60. return $this;
  61. }
  62. }