DateTimeParamConverter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 27.87 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 17 19 4
A getDateTime() 0 18 4
A supports() 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
3
namespace Drupal\controller_annotations\Request\ParamConverter;
4
5
use Drupal\controller_annotations\Configuration\ParamConverter;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
use DateTime;
9
10
/**
11
 * Convert DateTime instances from request attribute variable.
12
 *
13
 * @author Benjamin Eberlei <[email protected]>
14
 */
15
class DateTimeParamConverter implements ParamConverterInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     *
20
     * @throws NotFoundHttpException When invalid date given
21
     */
22 6 View Code Duplication
    public function apply(Request $request, ParamConverter $configuration)
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...
23
    {
24 6
        $param = $configuration->getName();
25 6
        if (!$request->attributes->has($param)) {
26 1
            return false;
27
        }
28
29 5
        $value = $request->attributes->get($param);
30 5
        if (!$value && $configuration->isOptional()) {
31 2
            return false;
32
        }
33
34 4
        $request->attributes->set(
35 4
            $param,
36 4
            $this->getDateTime($configuration, $value, $param)
37
        );
38
39 2
        return true;
40
    }
41
42
    /**
43
     * @param ParamConverter $configuration
44
     * @param $value
45
     * @param $param
46
     * @return bool|DateTime
47
     * @throws \Exception
48
     */
49 4
    protected function getDateTime(ParamConverter $configuration, $value, $param)
50
    {
51 4
        $options = $configuration->getOptions();
52
53 4
        if (isset($options['format'])) {
54 2
            $date = DateTime::createFromFormat($options['format'], $value);
55 3
        } elseif (false !== strtotime($value)) {
56 2
            $date = new DateTime($value);
57
        }
58
59 4
        if (empty($date)) {
60 2
            throw new NotFoundHttpException(
61 2
                sprintf('Invalid date given for parameter "%s".', $param)
62
            );
63
        }
64
65 2
        return $date;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 2
    public function supports(ParamConverter $configuration)
72
    {
73 2
        return \DateTime::class === $configuration->getClass();
74
    }
75
}
76