Completed
Pull Request — 3.x (#6212)
by Jordi Sala
03:21
created

AdminListBlockService::__construct()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.5341
c 0
b 0
f 0
cc 6
nc 7
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Block;
15
16
use Sonata\AdminBundle\Admin\Pool;
17
use Sonata\AdminBundle\Templating\TemplateRegistry;
18
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
19
use Sonata\BlockBundle\Block\BlockContextInterface;
20
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
21
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
use Twig\Environment;
25
26
/**
27
 * @final since sonata-project/admin-bundle 3.52
28
 *
29
 * @author Thomas Rabaix <[email protected]>
30
 */
31
class AdminListBlockService extends AbstractBlockService
32
{
33
    /**
34
     * @var Pool
35
     */
36
    protected $pool;
37
38
    /**
39
     * @var TemplateRegistryInterface
40
     */
41
    private $templateRegistry;
42
43
    /**
44
     * NEXT_MAJOR: Remove `$deprecatedTemplatingOrNull` argument.
45
     *
46
     * @param Environment|string $twigOrDeprecatedName
47
     */
48
    public function __construct(
49
        $twigOrDeprecatedName,
50
        ?EngineInterface $deprecatedTemplatingOrNull,
51
        Pool $pool,
52
        ?TemplateRegistryInterface $templateRegistry = null
53
    ) {
54
        // NEXT_MAJOR: Remove conditions, only pass the Environment
55
        if ($deprecatedTemplatingOrNull instanceof EngineInterface) {
56
            @trigger_error(sprintf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
57
                'Passing %s as argument 2 to %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x'
58
                . ' and will throw a \TypeError in version 4.0. You must pass null instead.',
59
                EngineInterface::class,
60
                __METHOD__
61
            ), E_USER_DEPRECATED);
62
63
            if (!\is_string($twigOrDeprecatedName)) {
64
                throw new \TypeError(sprintf(
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Passing other t...EngineInterface::class).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
65
                    'Passing other than string as argument 1 to %s() is not allowed when %s is passed as argument 2.'
66
                    . ' You must pass an string instead.',
67
                    __METHOD__,
68
                    EngineInterface::class
69
                ));
70
            }
71
72
            parent::__construct($twigOrDeprecatedName, $deprecatedTemplatingOrNull);
0 ignored issues
show
Documentation introduced by
$twigOrDeprecatedName is of type string, but the function expects a object<Twig\Environment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to AbstractBlockService::__construct() has too many arguments starting with $deprecatedTemplatingOrNull.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
        } elseif ($twigOrDeprecatedName instanceof Environment) {
74
            if (!\is_null($deprecatedTemplatingOrNull)) {
75
                throw new \TypeError(sprintf(
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Argument 2 pass...EngineInterface::class).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
76
                    'Argument 2 passed to %s() must be null if you passing %s as first argument, %s given.',
77
                    __METHOD__,
78
                    Environment::class,
79
                    EngineInterface::class
80
                ));
81
            }
82
83
            parent::__construct($twigOrDeprecatedName);
84
        } else {
85
            throw new \TypeError(sprintf(
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('Argument 1 pass...wig\Environment::class).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
86
                'Argument 1 passed to %s() should be an instance of %s, and you should leave argument 2 as null.',
87
                __METHOD__,
88
                Environment::class
89
            ));
90
        }
91
92
        $this->pool = $pool;
93
        $this->templateRegistry = $templateRegistry ?: new TemplateRegistry();
94
    }
95
96
    public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
97
    {
98
        $dashboardGroups = $this->pool->getDashboardGroups();
99
100
        $settings = $blockContext->getSettings();
101
102
        $visibleGroups = [];
103
        foreach ($dashboardGroups as $name => $dashboardGroup) {
104
            if (!$settings['groups'] || \in_array($name, $settings['groups'], true)) {
105
                $visibleGroups[] = $dashboardGroup;
106
            }
107
        }
108
109
        return $this->renderPrivateResponse($this->templateRegistry->getTemplate('list_block'), [
110
            'block' => $blockContext->getBlock(),
111
            'settings' => $settings,
112
            'admin_pool' => $this->pool,
113
            'groups' => $visibleGroups,
114
        ], $response);
115
    }
116
117
    public function getName()
118
    {
119
        return 'Admin List';
120
    }
121
122
    public function configureSettings(OptionsResolver $resolver): void
123
    {
124
        $resolver->setDefaults([
125
            'groups' => false,
126
        ]);
127
128
        $resolver->setAllowedTypes('groups', ['bool', 'array']);
129
    }
130
}
131