Completed
Push — 8.x-1.x ( 0d95ba...2c0805 )
by
unknown
26:58
created

EntityParamConverter::assertValidNode()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 8.8571
cc 6
eloc 12
nc 4
nop 2
crap 6.027
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 14
    public function __construct(EntityTypeManagerInterface $entityTypeManager)
28
    {
29 14
        $this->entityTypeManager = $entityTypeManager;
30 14
    }
31
32
    /**
33
     * @param Request $request
34
     * @param ParamConverter $configuration
35
     *
36
     * @return bool
37
     */
38 6
    public function apply(Request $request, ParamConverter $configuration)
39
    {
40 6
        $param = $configuration->getName();
41 6
        if (!$request->attributes->has($param)) {
42 2
            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
        if (is_null($node) && $configuration->isOptional()) {
81 1
            return;
82
        }
83 3
        if (is_null($node)) {
84
            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 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