Completed
Push — 8.x-1.x ( 99c4c7...dece3c )
by
unknown
29:31
created

EntityParamConverter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 19.19 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 7
dl 19
loc 99
ccs 30
cts 31
cp 0.9677
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 19 19 4
A getNode() 0 7 1
B assertValidNode() 0 18 7
A supports() 0 14 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 Drupal\Core\Entity\ContentEntityBase;
7
use Drupal\Core\Entity\ContentEntityInterface;
8
use Drupal\Core\Entity\Entity;
9
use Drupal\Core\Entity\EntityInterface;
10
use Drupal\Core\Entity\EntityTypeManagerInterface;
11
use Drupal\node\Entity\Node;
12
use Drupal\node\NodeInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
16
class EntityParamConverter implements ParamConverterInterface
17
{
18
19
    /**
20
     * @var EntityTypeManagerInterface
21
     */
22
    private $entityTypeManager;
23
24
    /**
25
     * @param EntityTypeManagerInterface $entityTypeManager
26
     */
27 13
    public function __construct(EntityTypeManagerInterface $entityTypeManager)
28
    {
29 13
        $this->entityTypeManager = $entityTypeManager;
30 13
    }
31
32
    /**
33
     * @param Request $request
34
     * @param ParamConverter $configuration
35
     *
36
     * @return bool
37
     */
38 5 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...
39
    {
40 5
        $param = $configuration->getName();
41 5
        if (!$request->attributes->has($param)) {
42 1
            return false;
43
        }
44
45 4
        $value = $request->attributes->get($param);
46 4
        if (!$value && $configuration->isOptional()) {
47
            return false;
48
        }
49
50 4
        $request->attributes->set(
51
          $param,
52 4
          $this->getNode($value, $configuration)
53
        );
54
55 3
        return true;
56
    }
57
58
    /**
59
     * @param string $value
60
     * @param ParamConverter $configuration
61
     *
62
     * @return EntityInterface|null
63
     */
64 4
    protected function getNode($value, ParamConverter $configuration)
65
    {
66 4
        $node = $this->entityTypeManager->getStorage('node')->load($value);
67 4
        $this->assertValidNode($configuration, $node);
68
69 3
        return $node;
70
    }
71
72
    /**
73
     * @param ParamConverter $configuration
74
     * @param EntityInterface $node
75
     */
76 4
    protected function assertValidNode(
77
      ParamConverter $configuration,
78
      EntityInterface $node = null
79
    ) {
80 4
        $options = $configuration->getOptions();
81
82
        if (
83 4
          (is_null($node) && false === $configuration->isOptional())
84 4
          || (!is_null($node) && isset($options['bundle']) && $node->bundle(
85 4
            ) !== $options['bundle'])
86
        ) {
87 1
            $class = 'node';
88 1
            if (isset($options['bundle'])) {
89 1
                $class = $options['bundle'];
90
            }
91 1
            throw new NotFoundHttpException(sprintf('%s not found.', $class));
92
        }
93 3
    }
94
95
    /**
96
     * @param ParamConverter $configuration
97
     *
98
     * @return bool
99
     */
100 7
    public function supports(ParamConverter $configuration)
101
    {
102 7
        return in_array(
103 7
          $configuration->getClass(),
104
          [
105 7
            NodeInterface::class,
106
            Node::class,
107
            EntityInterface::class,
108
            Entity::class,
109
            ContentEntityInterface::class,
110
            ContentEntityBase::class,
111
          ]
112
        );
113
    }
114
}
115