Validator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\GS1Parser\Validator;
6
7
use Lamoda\GS1Parser\Exception\InvalidBarcodeException;
8
use Lamoda\GS1Parser\Parser\ParserInterface;
9
10
final class Validator implements ValidatorInterface
11
{
12
    /**
13
     * @var ParserInterface
14
     */
15
    private $parser;
16
    /**
17
     * @var ValidatorConfig
18
     */
19
    private $config;
20
21 16
    public function __construct(ParserInterface $parser, ValidatorConfig $config)
22
    {
23 16
        $this->parser = $parser;
24 16
        $this->config = $config;
25 16
    }
26
27 16
    public function validate($value): Resolution
28
    {
29 16
        if ($value === null && $this->config->isAllowEmpty()) {
30 1
            return Resolution::createValid();
31
        }
32
33 15
        if (!is_string($value)) {
34 2
            return Resolution::createInvalid([
35 2
                ErrorCodes::VALUE_IS_NOT_STRING => 'Value is not a string',
36
            ]);
37
        }
38
39 13
        $trimmedValue = trim($value);
40
41 13
        if ($trimmedValue === '') {
42 2
            return $this->config->isAllowEmpty() ?
43 1
                Resolution::createValid() :
44 1
                Resolution::createInvalid([
45 2
                    ErrorCodes::VALUE_EMPTY => 'Value is empty',
46
                ]);
47
        }
48
49
        try {
50 11
            $barcode = $this->parser->parse($trimmedValue);
51 8
        } catch (InvalidBarcodeException $exception) {
52 8
            return Resolution::createInvalid([
53 8
                ErrorCodes::INVALID_VALUE => sprintf(
54 8
                    'Value is invalid: %s',
55 8
                    $exception->getCode()
56
                ),
57
            ]);
58
        }
59
60 3
        $ais = array_keys($barcode->ais());
61 3
        $requiredAis = array_diff($this->config->getRequiredAIs(), $ais);
62 3
        $forbiddenAIs = array_intersect($this->config->getForbiddenAIs(), $ais);
63
64 3 View Code Duplication
        if (count($requiredAis) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65 1
            return Resolution::createInvalid([
66 1
                ErrorCodes::MISSING_AIS => sprintf(
67 1
                    'AIs are missing: "%s"',
68 1
                    implode('", "', $requiredAis)
69
                ),
70
            ]);
71
        }
72
73 2 View Code Duplication
        if (count($forbiddenAIs) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74 1
            return Resolution::createInvalid([
75 1
                ErrorCodes::FORBIDDEN_AIS => sprintf(
76 1
                    'AIs are forbidden: "%s"',
77 1
                    implode('", "', $forbiddenAIs)
78
                ),
79
            ]);
80
        }
81
82 1
        return Resolution::createValid();
83
    }
84
85
}