Ttl.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\PageCache\Model\System\Config\Backend;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Escaper;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. /**
  11. * Backend model for processing Public content cache lifetime settings
  12. *
  13. * Class Ttl
  14. */
  15. class Ttl extends \Magento\Framework\App\Config\Value
  16. {
  17. /**
  18. * @var Escaper
  19. */
  20. private $escaper;
  21. /**
  22. * Ttl constructor.
  23. * @param \Magento\Framework\Model\Context $context
  24. * @param \Magento\Framework\Registry $registry
  25. * @param ScopeConfigInterface $config
  26. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  27. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  28. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  29. * @param array $data
  30. * @param Escaper|null $escaper
  31. */
  32. public function __construct(
  33. \Magento\Framework\Model\Context $context,
  34. \Magento\Framework\Registry $registry,
  35. ScopeConfigInterface $config,
  36. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  37. ?\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  38. ?\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  39. array $data = [],
  40. ?Escaper $escaper = null
  41. ) {
  42. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  43. $this->escaper = $escaper ?: ObjectManager::getInstance()->create(Escaper::class);
  44. }
  45. /**
  46. * Throw exception if Ttl data is invalid or empty
  47. *
  48. * @return $this
  49. * @throws \Magento\Framework\Exception\LocalizedException
  50. */
  51. public function beforeSave()
  52. {
  53. $value = $this->getValue();
  54. if ($value < 0 || !preg_match('/^[0-9]+$/', $value)) {
  55. throw new \Magento\Framework\Exception\LocalizedException(
  56. __(
  57. 'Ttl value "%1" is not valid. Please use only numbers equal or greater than zero.',
  58. $this->escaper->escapeHtml($value)
  59. )
  60. );
  61. }
  62. return $this;
  63. }
  64. }