Completed
Push — master ( 31c245...6903f3 )
by Javier
03:11
created

CarbonParamConverter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFormat() 0 9 3
A apply() 0 15 2
A getCarbon() 0 10 3
A isValidInput() 0 7 3
A supports() 0 7 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Xgc\CarbonBundle\Request\ParamConverter;
5
6
use Carbon\Carbon;
7
use Exception;
8
use InvalidArgumentException;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
10
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
14
/**
15
 * Class CarbonParamConverter
16
 * @package Xgc\CarbonBundle\Request
17
 */
18
class CarbonParamConverter implements ParamConverterInterface
19
{
20
21
    /**
22
     * Stores the object in the request.
23
     *
24
     * @param Request        $request The request
25
     * @param ParamConverter $configuration Contains the name, class and options of the object
26
     *
27
     * @return bool True if the object has been successfully set, else false
28
     *
29
     * @throws NotFoundHttpException
30
     * @throws InvalidArgumentException
31
     */
32 22
    public function apply(Request $request, ParamConverter $configuration): bool
33
    {
34 22
        if (!$this->isValidInput($request, $configuration)) {
35 2
            return false;
36
        }
37
38 20
        $param   = $configuration->getName();
39 20
        $options = $configuration->getOptions();
40 20
        $value   = $request->attributes->get($param);
41 20
        $format  = $this->loadFormat($options, $value);
42 20
        $carbon  = $this->getCarbon($format, $value, $param);
43
44 18
        $request->attributes->set($param, $carbon);
45
46 18
        return true;
47
    }
48
49
    /**
50
     * Checks if the object is supported.
51
     *
52
     * @param ParamConverter $configuration Should be an instance of ParamConverter
53
     *
54
     * @return bool True if the object is supported, else false
55
     */
56 2
    public function supports(ParamConverter $configuration): bool
57
    {
58 2
        if (null === $configuration->getClass()) {
59 1
            return false;
60
        }
61
62 2
        return Carbon::class === $configuration->getClass();
63
    }
64
65
    /**
66
     * @param Request        $request
67
     * @param ParamConverter $configuration
68
     *
69
     * @return bool
70
     */
71 22
    private function isValidInput(Request $request, ParamConverter $configuration): bool
72
    {
73 22
        $param = $configuration->getName();
74
75
        return !(
76 22
            (!$request->attributes->has($param)) ||
77 22
            (!$request->attributes->get($param) && $configuration->isOptional())
78
        );
79
    }
80
81
    /**
82
     * @param array  $options
83
     *
84
     * @param string $value
85
     *
86
     * @return null|string ;
87
     */
88 20
    private function loadFormat(array $options, string $value): ?string
89
    {
90 20
        $format = $options['format'] ?? null;
91
92 20
        if (!$format && \filter_var($value, \FILTER_VALIDATE_INT) !== false) {
93 1
            $format = 'U';
94
        }
95
96 20
        return $format;
97
    }
98
99
    /**
100
     * @param null|string $format
101
     * @param string      $value
102
     * @param string      $param
103
     *
104
     * @return Carbon
105
     *
106
     * @throws NotFoundHttpException
107
     */
108 20
    private function getCarbon(?string $format, string $value, string $param): Carbon
109
    {
110
        try {
111 20
            if ($format) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $format of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
112 3
                return Carbon::createFromFormat($format, $value);
113
            }
114
115 17
            return new Carbon($value);
116 2
        } catch (Exception $e) {
117 2
            throw new NotFoundHttpException(\sprintf('Invalid date given for parameter "%s".', $param));
118
        }
119
    }
120
}
121