|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace IntegerNet\AsyncVarnish\Model\System\Config\Backend; |
| 5 | + |
| 6 | +use Magento\Framework\App\Cache\TypeListInterface; |
| 7 | +use Magento\Framework\App\Config\Value; |
| 8 | +use Magento\Framework\App\ObjectManager; |
| 9 | +use Magento\Framework\Data\Collection\AbstractDb; |
| 10 | +use Magento\Framework\Escaper; |
| 11 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 12 | +use Magento\Framework\Exception\LocalizedException; |
| 13 | +use Magento\Framework\Model\Context; |
| 14 | +use Magento\Framework\Model\ResourceModel\AbstractResource; |
| 15 | +use Magento\Framework\Registry; |
| 16 | + |
| 17 | +/** |
| 18 | + * Backend model for processing Varnish Max header length settings |
| 19 | + * |
| 20 | + * Class HeaderLength |
| 21 | + */ |
| 22 | +class HeaderLength extends Value |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var Escaper |
| 26 | + */ |
| 27 | + private $escaper; |
| 28 | + |
| 29 | + /** |
| 30 | + * Ttl constructor. |
| 31 | + * @param Context $context |
| 32 | + * @param Registry $registry |
| 33 | + * @param ScopeConfigInterface $config |
| 34 | + * @param TypeListInterface $cacheTypeList |
| 35 | + * @param Escaper $escaper |
| 36 | + * @param AbstractResource|null $resource |
| 37 | + * @param AbstractDb|null $resourceCollection |
| 38 | + * @param array $data |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + Context $context, |
| 42 | + Registry $registry, |
| 43 | + ScopeConfigInterface $config, |
| 44 | + TypeListInterface $cacheTypeList, |
| 45 | + Escaper $escaper, |
| 46 | + ?AbstractResource $resource = null, |
| 47 | + ?AbstractDb $resourceCollection = null, |
| 48 | + array $data = [] |
| 49 | + ) { |
| 50 | + parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); |
| 51 | + $this->escaper = $escaper; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Throw exception if HeaderLength data is invalid or lower than 40 bytes |
| 56 | + * |
| 57 | + * @return $this |
| 58 | + * @throws LocalizedException |
| 59 | + */ |
| 60 | + public function beforeSave() |
| 61 | + { |
| 62 | + $value = $this->getValue(); |
| 63 | + if ($value < 40 || !preg_match('/^[0-9]+$/', $value)) { |
| 64 | + throw new LocalizedException( |
| 65 | + __( |
| 66 | + 'Header Length value "%1" is not valid. Please use only numbers equal or greater than 40.', |
| 67 | + $this->escaper->escapeHtml($value) |
| 68 | + ) |
| 69 | + ); |
| 70 | + } |
| 71 | + return $this; |
| 72 | + } |
| 73 | +} |
0 commit comments