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

HeaderLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 8
dl 0
loc 12
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
}
74