DateValidatorTrait::mustBeDateTime()   C
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 8
Ratio 26.67 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 0
Metric Value
dl 8
loc 30
ccs 15
cts 15
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 3
nop 4
crap 7
1
<?php
2
3
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure;
4
5
use Imedia\Ammit\UI\Resolver\UIValidationEngine;
6
use Imedia\Ammit\UI\Resolver\Validator\InvalidArgumentException;
7
use Imedia\Ammit\Domain\DateValidation;
8
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface;
9
10
/**
11
 * @author Guillaume MOREL <[email protected]>
12
 */
13
trait DateValidatorTrait
14
{
15
    /** @var UIValidationEngine */
16
    protected $validationEngine;
17
18
    /**
19
     * Exceptions are caught in order to be processed later
20
     * @param mixed $value Date Y-m-d ?
21
     *
22
     * @return \DateTime
23
     */
24
    public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
25
    {
26 1
        $dateValidation = new DateValidation();
27 1
        $this->validationEngine->validateFieldValue(
28 1
            $parentValidator ?: $this,
29 1
            function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) {
30 1 View Code Duplication
                if (null === $value || ! $dateValidation->isDateValid($value)) {
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...
31 1
                    throw new InvalidArgumentException(
32 1
                        $exceptionMessage ?: sprintf('Date "%s" format invalid, must be Y-m-d.', $value),
33 1
                        0,
34
                        $propertyPath,
35
                        $value
36
                    );
37
                }
38 1
            }
39
        );
40
41 1
        if (null === $value) {
42 1
            return $this->createDefaultDateTime(); // Invalid
43
        }
44
45 1
        $date = $dateValidation->createDateFromString($value);
46 1
        if ($date instanceof \DateTime) {
47 1
            return $date->setTime(0, 0, 0); // Valid
48
        }
49
50 1
        return $this->createDefaultDateTime(); // Invalid
51
    }
52
53
    /**
54
     * Exceptions are caught in order to be processed later
55
     * @param mixed $value null|DateTime Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 or null ?
56
     *
57
     * @return \DateTime|null
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\DateTime|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
58
     */
59
    public function mustBeDateTimeOrEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
60
    {
61 1
        if (empty($value)) {
62 1
            return null;
63
        }
64
65 1
        return $this->mustBeDateTime($value, $propertyPath, $parentValidator, $exceptionMessage);
66
    }
67
68
    /**
69
     * Exceptions are caught in order to be processed later
70
     * @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ?
71
     *
72
     * @return \DateTime|false
73
     */
74
    public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
75
    {
76 1
        $dateValidation = new DateValidation();
77
78 1
        $this->validationEngine->validateFieldValue(
79 1
            $parentValidator ?: $this,
80 1
            function() use ($value, $propertyPath, $exceptionMessage, $dateValidation) {
81 1 View Code Duplication
                if (null === $value || ! $dateValidation->isDateTimeValid($value)) {
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...
82 1
                    throw new InvalidArgumentException(
83 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),
84 1
                        0,
85
                        $propertyPath,
86
                        $value
87
                    );
88
                }
89 1
            }
90
        );
91
92 1
        if (null === $value) {
93 1
            return $this->createDefaultDateTime(); // Invalid
94
        }
95
96 1
        $date = $dateValidation->createDateTimeFromString($value);
97
98 1
        if ($date instanceof \DateTime) {
99 1
            return $date; // Valid
100
        }
101
102 1
        return $this->createDefaultDateTime();  // Invalid
103
    }
104
105
    private function createDefaultDateTime(): \DateTime
106
    {
107 1
        $date = new \DateTime();
108
109 1
        return $date->setTime(0, 0, 0);
110
    }
111
}
112