Completed
Push — 8.x-1.x ( 4cc04a...006414 )
by
unknown
27:53
created

EntityParamConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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