Completed
Push — master ( 1765ed...c6ff3e )
by Fabian
29s queued 10s
created

FetchTagLimit::beforeSave()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 10
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 Fetch Tag Limit settings
19
 *
20
 * Class HeaderLength
21
 */
22
class FetchTagLimit 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 empty
56
     *
57
     * @return $this
58
     * @throws LocalizedException
59
     */
60
    public function beforeSave()
61
    {
62
        $value = $this->getValue();
63
        if ($value < 0 || !preg_match('/^[0-9]+$/', $value)) {
64
            throw new LocalizedException(
65
                __(
66
                    'Fetch tag limit value "%1" is not valid. Please use only numbers equal or greater than 0.',
67
                    $this->escaper->escapeHtml($value)
68
                )
69
            );
70
        }
71
        return $this;
72
    }
73
}
74