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

AdminListBlockService::__construct()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.6577
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
            if (!\is_string($twigOrDeprecatedName)) {
57
                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...
58
                    'Passing other than string as argument 1 to %s() is not allowed when %s is passed as argument 2.'
59
                    .' You must pass an string instead.',
60
                    __METHOD__,
61
                    EngineInterface::class
62
                ));
63
            }
64
65
            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...
66
        } elseif ($twigOrDeprecatedName instanceof Environment) {
67
            if (null !== $deprecatedTemplatingOrNull) {
68
                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...
69
                    'Argument 2 passed to %s() must be null if you passing %s as first argument, %s given.',
70
                    __METHOD__,
71
                    Environment::class,
72
                    EngineInterface::class
73
                ));
74
            }
75
76
            parent::__construct($twigOrDeprecatedName);
77
        } else {
78
            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...
79
                'Argument 1 passed to %s() should be an instance of %s, and you should leave argument 2 as null.',
80
                __METHOD__,
81
                Environment::class
82
            ));
83
        }
84
85
        $this->pool = $pool;
86
        $this->templateRegistry = $templateRegistry ?: new TemplateRegistry();
87
    }
88
89
    public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
90
    {
91
        $dashboardGroups = $this->pool->getDashboardGroups();
92
93
        $settings = $blockContext->getSettings();
94
95
        $visibleGroups = [];
96
        foreach ($dashboardGroups as $name => $dashboardGroup) {
97
            if (!$settings['groups'] || \in_array($name, $settings['groups'], true)) {
98
                $visibleGroups[] = $dashboardGroup;
99
            }
100
        }
101
102
        return $this->renderPrivateResponse($this->templateRegistry->getTemplate('list_block'), [
103
            'block' => $blockContext->getBlock(),
104
            'settings' => $settings,
105
            'admin_pool' => $this->pool,
106
            'groups' => $visibleGroups,
107
        ], $response);
108
    }
109
110
    public function getName()
111
    {
112
        return 'Admin List';
113
    }
114
115
    public function configureSettings(OptionsResolver $resolver): void
116
    {
117
        $resolver->setDefaults([
118
            'groups' => false,
119
        ]);
120
121
        $resolver->setAllowedTypes('groups', ['bool', 'array']);
122
    }
123
}
124