ResolveEntityParamConverter::resolveTargetEntity()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the takeit/AmpHtmlBundle package.
5
 *
6
 * (c) Rafał Muszyński <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Takeit\Bundle\AmpHtmlBundle\Request\ParamConverter;
13
14
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
16
use Symfony\Component\HttpFoundation\Request;
17
use Doctrine\Common\Persistence\ManagerRegistry;
18
use Takeit\Bundle\AmpHtmlBundle\Model\AmpInterface;
19
20
/**
21
 * ResolveEntityParamConverter converts AmpInterface instances to a target entity.
22
 *
23
 * @author Rafał Muszyński <[email protected]>
24
 */
25
class ResolveEntityParamConverter extends DoctrineParamConverter
26
{
27
    /**
28
     * @var ManagerRegistry
29
     */
30
    protected $registry;
31
32
    /**
33
     * @var array
34
     */
35
    protected $mapping;
36
37
    /**
38
     * @param array                $mapping  Interface to entity mapping
39
     * @param ManagerRegistry|null $registry Registry manager
40
     */
41
    public function __construct(array $mapping, ManagerRegistry $registry = null)
42
    {
43
        $this->mapping = $mapping;
44
        $this->registry = $registry;
45
46
        parent::__construct($registry);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function apply(Request $request, ParamConverter $configuration)
53
    {
54
        return parent::apply($request, $this->resolveTargetEntity($configuration));
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function supports(ParamConverter $configuration)
61
    {
62
        if (AmpInterface::class !== $configuration->getClass()) {
63
            return false;
64
        }
65
66
        return parent::supports($this->resolveTargetEntity($configuration));
67
    }
68
69
    /**
70
     * Resolves the target entity.
71
     *
72
     * @param ParamConverter $configuration Contains the name, class and options of the object
73
     *
74
     * @return ParamConverter
75
     */
76
    protected function resolveTargetEntity(ParamConverter $configuration)
77
    {
78
        $class = $configuration->getClass();
79
        if (isset($this->mapping[$class])) {
80
            if ($this->mapping[$class] !== $class) {
81
                $configuration->setClass($this->mapping[$class]);
82
            }
83
        }
84
85
        return $configuration;
86
    }
87
}
88