Completed
Push — master ( f77682...c45988 )
by Guillaume
02:29
created

DateValidatorTrait::createDefaultDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure;
4
5
use Assert\InvalidArgumentException;
6
use Imedia\Ammit\Domain\DateValidation;
7
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface;
8
9
/**
10
 * @author Guillaume MOREL <[email protected]>
11
 */
12
trait DateValidatorTrait
13
{
14
    /**
15
     * Exceptions are caught in order to be processed later
16
     * @param mixed $value Date Y-m-d ?
17
     *
18
     * @return \DateTime
19
     */
20
    public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
21
    {
22 1
        $dateValidation = new DateValidation();
23 1
        $this->validationEngine->validateFieldValue(
0 ignored issues
show
Bug introduced by
The property validationEngine does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24 1
            $parentValidator ?: $this,
25 1 View Code Duplication
            function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) {
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...
26 1
                if (null === $value || ! $dateValidation->isDateValid($value)) {
27 1
                    throw new InvalidArgumentException(
28 1
                        $exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value),
29 1
                        0,
30
                        $propertyPath,
31
                        $value
32
                    );
33
                }
34 1
            }
35
        );
36
37 1
        if (null === $value) {
38 1
            return $this->createDefaultDateTime(); // Invalid
39
        }
40
41 1
        $date = $dateValidation->createDateFromString($value);
42 1
        if ($date instanceof \DateTime) {
43 1
            return $date->setTime(0, 0, 0); // Valid
44
        }
45
46 1
        return $this->createDefaultDateTime(); // Invalid
47
    }
48
49
    /**
50
     * Exceptions are caught in order to be processed later
51
     * @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ?
52
     *
53
     * @return \DateTime|false
54
     */
55
    public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
56
    {
57 1
        $dateValidation = new DateValidation();
58
59 1
        $this->validationEngine->validateFieldValue(
60 1
            $parentValidator ?: $this,
61 1 View Code Duplication
            function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) {
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...
62 1
                if (null === $value || ! $dateValidation->isDateTimeValid($value)) {
63 1
                    throw new InvalidArgumentException(
64 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),
65 1
                        0,
66
                        $propertyPath,
67
                        $value
68
                    );
69
                }
70 1
            }
71
        );
72
73 1
        if (null === $value) {
74 1
            return $this->createDefaultDateTime(); // Invalid
75
        }
76
77 1
        $date = $dateValidation->createDateTimeFromString($value);
78
79 1
        if ($date instanceof \DateTime) {
80 1
            return $date; // Valid
81
        }
82
83 1
        return $this->createDefaultDateTime();  // Invalid
84
    }
85
86
    private function createDefaultDateTime(): \DateTime
87
    {
88 1
        $date = new \DateTime();
89
90 1
        return $date->setTime(0, 0, 0);
91
    }
92
}
93