Completed
Pull Request — 3.1 (#348)
by Piotr
09:36 queued 07:40
created

AdminElementParamConverter::supports()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Request\ParamConverter;
13
14
use FSi\Bundle\AdminBundle\Admin\ManagerInterface;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
16
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
use FSi\Bundle\AdminBundle\Admin\Element;
20
21
class AdminElementParamConverter implements ParamConverterInterface
22
{
23
    /**
24
     * @var ManagerInterface
25
     */
26
    private $manager;
27
28
    public function __construct(ManagerInterface $manager)
29
    {
30
        $this->manager = $manager;
31
    }
32
33
    public function apply(Request $request, ParamConverter $configuration): bool
34
    {
35
        $param = $configuration->getName();
36
        $id = $request->attributes->get($param, '');
37
38
        if (!$this->manager->hasElement($id)) {
39
            throw new NotFoundHttpException(sprintf('Admin element with id %s does not exist.', $id));
40
        }
41
42
        $request->attributes->set($param, $this->manager->getElement($id));
43
44
        return true;
45
    }
46
47
    public function supports(ParamConverter $configuration): bool
48
    {
49
        if (!$configuration instanceof ParamConverter) {
0 ignored issues
show
Bug introduced by
The class Sensio\Bundle\FrameworkE...guration\ParamConverter does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
50
            return false;
51
        }
52
53
        if (!$configuration->getClass()) {
54
            return false;
55
        }
56
57
        if (!class_exists($configuration->getClass()) && !interface_exists($configuration->getClass())) {
58
            return false;
59
        }
60
61
        $implements = class_implements($configuration->getClass());
62
63
        if (in_array(Element::class, $implements, true)) {
64
            return true;
65
        }
66
67
        return false;
68
    }
69
}
70