Completed
Pull Request — master (#47)
by Guillaume
02:34
created

RawValueValidator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 38
loc 76
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mustBeDate() 19 19 4
A mustBeDateTime() 19 19 4
A createUIValidationException() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\UI\Resolver\Validator;
5
6
use Assert\InvalidArgumentException;
7
use Imedia\Ammit\Domain\DateValidation;
8
use Imedia\Ammit\UI\Resolver\Exception\UIValidationException;
9
use Imedia\Ammit\UI\Resolver\UIValidationEngine;
10
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure\BooleanValidatorTrait;
11
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure\FloatValidatorTrait;
12
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure\IntegerValidatorTrait;
13
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure\StringValidatorTrait;
14
use Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure\ArrayValidatorTrait;
15
16
/**
17
 * @author Guillaume MOREL <[email protected]>
18
 */
19
class RawValueValidator implements UIValidatorInterface
20
{
21
    use BooleanValidatorTrait;
22
    use IntegerValidatorTrait;
23
    use FloatValidatorTrait;
24
    use StringValidatorTrait;
25
    use ArrayValidatorTrait;
26
27
    /** @var UIValidationEngine */
28
    protected $validationEngine;
29
30
    public function __construct(UIValidationEngine $validationEngine)
31
    {
32 1
        $this->validationEngine = $validationEngine;
33 1
    }
34
35
    /**
36
     * Exceptions are caught in order to be processed later
37
     * @param mixed $value Date Y-m-d ?
38
     *
39
     * @return mixed Untouched value
40
     */
41 View Code Duplication
    public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
42
    {
43 1
        $this->validationEngine->validateFieldValue(
44 1
            $parentValidator ?: $this,
45 1
            function() use ($value, $propertyPath, $exceptionMessage) {
46 1
                $dateValidation = new DateValidation();
47 1
                if (! $dateValidation->isDateValid($value)) {
48 1
                    throw new InvalidArgumentException(
49 1
                        $exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value),
50 1
                        0,
51
                        $propertyPath,
52
                        $value
53
                    );
54
                }
55 1
            }
56
        );
57
58 1
        return $value;
59
    }
60
61
    /**
62
     * Exceptions are caught in order to be processed later
63
     * @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ?
64
     *
65
     * @return mixed Untouched value
66
     */
67 View Code Duplication
    public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
68
    {
69 1
        $this->validationEngine->validateFieldValue(
70 1
            $parentValidator ?: $this,
71 1
            function() use ($value, $propertyPath, $exceptionMessage) {
72 1
                $dateValidation = new DateValidation();
73 1
                if (! $dateValidation->isDateTimeValid($value)) {
74 1
                    throw new InvalidArgumentException(
75 1
                        $exceptionMessage ?: sprintf('Datetime "%s" format invalid, must be Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00.', $value),
76 1
                        0,
77
                        $propertyPath,
78
                        $value
79
                    );
80
                }
81 1
            }
82
        );
83
84 1
        return $value;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
91
    {
92 1
        return UIValidationException::fromRaw($message, $propertyPath);
93
    }
94
}
95