LogFrequency.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Config\Backend;
  7. use Magento\Cron\Model\Config\Source\Frequency;
  8. use Magento\Framework\App\Cache\TypeListInterface;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\App\Config\Storage\WriterInterface;
  11. use Magento\Framework\App\Config\Value;
  12. use Magento\Framework\Data\Collection\AbstractDb;
  13. use Magento\Framework\Exception\TemporaryState\CouldNotSaveException;
  14. use Magento\Framework\Model\Context;
  15. use Magento\Framework\Model\ResourceModel\AbstractResource;
  16. use Magento\Framework\Registry;
  17. use Vertex\Tax\Model\Config;
  18. use Vertex\Tax\Model\ConfigFactory;
  19. /**
  20. * Backend for frequency of log rotation
  21. */
  22. class LogFrequency extends Value
  23. {
  24. /** @var Config */
  25. private $configReader;
  26. /** @var WriterInterface */
  27. private $configWriter;
  28. /**
  29. * @param Context $context
  30. * @param Registry $registry
  31. * @param ScopeConfigInterface $config
  32. * @param WriterInterface $configWriter
  33. * @param ConfigFactory $vertexConfigFactory
  34. * @param TypeListInterface $cacheTypeList
  35. * @param AbstractResource|null $resource
  36. * @param AbstractDb|null $resourceCollection
  37. * @param array $data
  38. */
  39. public function __construct(
  40. Context $context,
  41. Registry $registry,
  42. ScopeConfigInterface $config,
  43. WriterInterface $configWriter,
  44. ConfigFactory $vertexConfigFactory,
  45. TypeListInterface $cacheTypeList,
  46. AbstractResource $resource = null,
  47. AbstractDb $resourceCollection = null,
  48. array $data = []
  49. ) {
  50. $this->configWriter = $configWriter;
  51. $this->configReader = $vertexConfigFactory->create(['scopeConfig' => $config]); // Support snapshot config
  52. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  53. }
  54. /**
  55. * Convert the saved configuration values into a valid cron schedule.
  56. *
  57. * @return Value
  58. * @throws CouldNotSaveException
  59. */
  60. public function afterSave()
  61. {
  62. $time = explode(',', $this->configReader->getCronRotationTime());
  63. $frequency = $this->configReader->getCronRotationFrequency();
  64. $cronExprString = $this->createCronExpression($time, $frequency);
  65. try {
  66. $this->configWriter->save(Config::CRON_STRING_PATH, $cronExprString);
  67. } catch (\Exception $e) {
  68. throw new CouldNotSaveException(__('Failed to write crontab expression.'), $e);
  69. }
  70. return parent::afterSave();
  71. }
  72. /**
  73. * Generate a crontab expression from the given data.
  74. *
  75. * @param array $timeComponents
  76. * @param string $frequency
  77. * @return string
  78. */
  79. private function createCronExpression(array $timeComponents = [], $frequency = null)
  80. {
  81. $timeComponents = array_pad($timeComponents, 3, 0);
  82. $expression = [
  83. (int)$timeComponents[1],
  84. (int)$timeComponents[0],
  85. $frequency === Frequency::CRON_MONTHLY ? '1' : '*',
  86. '*',
  87. $frequency === Frequency::CRON_WEEKLY ? '1' : '*',
  88. ];
  89. return \implode(' ', $expression);
  90. }
  91. }