Completed
Push — 8.x-1.x ( 2c0805...e1bdd0 )
by
unknown
26:50
created

EntityParamConverter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 99
ccs 32
cts 32
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getNode() 0 7 1
A supports() 0 14 1
A apply() 0 19 4
B assertValidNode() 0 18 6
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 17
    public function __construct(EntityTypeManagerInterface $entityTypeManager)
28
    {
29 17
        $this->entityTypeManager = $entityTypeManager;
30 17
    }
31
32
    /**
33
     * @param Request $request
34
     * @param ParamConverter $configuration
35
     *
36
     * @return bool
37
     */
38 8
    public function apply(Request $request, ParamConverter $configuration)
39
    {
40 8
        $param = $configuration->getName();
41 8
        if (!$request->attributes->has($param)) {
42 2
            return false;
43
        }
44
45 6
        $value = $request->attributes->get($param);
46 6
        if (!$value && $configuration->isOptional()) {
47 1
            return false;
48
        }
49
50 5
        $request->attributes->set(
51
          $param,
52 5
          $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 5
    protected function getNode($value, ParamConverter $configuration)
65
    {
66 5
        $node = $this->entityTypeManager->getStorage('node')->load($value);
67 5
        $this->assertValidNode($configuration, $node);
68
69 3
        return $node;
70
    }
71
72
    /**
73
     * @param ParamConverter $configuration
74
     * @param EntityInterface $node
75
     */
76 5
    protected function assertValidNode(
77
      ParamConverter $configuration,
78
      EntityInterface $node = null
79
    ) {
80 5
        if (is_null($node) && $configuration->isOptional()) {
81 1
            return;
82
        }
83 4
        if (is_null($node)) {
84 1
            throw new NotFoundHttpException('entity not found.');
85
        }
86 3
        $options = $configuration->getOptions();
87 3
        if (isset($options['bundle']) && $node->bundle(
88 3
          ) !== $options['bundle']) {
89 1
            throw new NotFoundHttpException(
90 1
              sprintf('%s not found.', $options['bundle'])
91
            );
92
        }
93 2
    }
94
95
    /**
96
     * @param ParamConverter $configuration
97
     *
98
     * @return bool
99
     */
100 9
    public function supports(ParamConverter $configuration)
101
    {
102 9
        return in_array(
103 9
          $configuration->getClass(),
104
          [
105 9
            NodeInterface::class,
106
            Node::class,
107
            EntityInterface::class,
108
            Entity::class,
109
            ContentEntityInterface::class,
110
            ContentEntityBase::class,
111
          ]
112
        );
113
    }
114
}
115