AbstractValidator::getConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Hryvinskyi\QuoteAddressValidator\Model\Validation;
4
5
use Hryvinskyi\QuoteAddressValidator\Model\ConfigInterface;
6
7
abstract class AbstractValidator implements \Hryvinskyi\QuoteAddressValidator\Model\ValidationInterface
8
{
9
    /**
10
     * @var ConfigInterface 
11
     */
12
    private $config;
13
14
    public function __construct(ConfigInterface $config)
15
    {
16
        $this->config = $config;
17
    }
18
19
    /**
20
     * @return ConfigInterface
21
     */
22
    public function getConfig(): ConfigInterface
23
    {
24
        return $this->config;
25
    }
26
27
    /**
28
     * Check string for stop words
29
     *
30
     * @param string $string
31
     * @param array $stopWords
32
     * @return bool True if the string does not contain stop words
33
     */
34
    public function checkStringForStopWords(string $string, array $stopWords): bool
35
    {
36
        foreach ($stopWords as $stopWord) {
37
            if (stripos($string, $stopWord) !== false) {
38
                return false;
39
            }
40
        }
41
42
        return true;
43
    }
44
45
    /**
46
     * Validate the value
47
     *
48
     * @param string $value
49
     * @param string $pattern
50
     * @param array $stopWords
51
     * @return bool True if the value is valid
52
     */
53
    public function validate(string $value, string $pattern, array $stopWords): bool
54
    {
55
        switch ($this->getConfig()->getValidationType()) {
56
            case 1: // Regex
57
                return preg_match($pattern, $value) === 1;
58
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
59
            case 2: // Stop words
60
                return $this->checkStringForStopWords($value, $stopWords);
61
                break;
62
            case 3: // Regex and stop words
63
                return preg_match($pattern, $value) === 1 && $this->checkStringForStopWords($value, $stopWords);
64
                break;
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    abstract public function execute(\Magento\Quote\Api\Data\AddressInterface $address): bool;
74
}
75