Completed
Pull Request — 3.x (#6220)
by Vincent
03:15
created

AdminListBlockService   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 6
dl 0
loc 116
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 61 14
A execute() 0 20 4
A getName() 0 4 1
A configureSettings() 0 8 1
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: Change signature for (Environment $twig, Pool $pool, ?TemplateRegistryInterface $templateRegistry = null).
45
     *
46
     * @param Environment|string                  $twigOrName
47
     * @param EngineInterface|Pool|null           $poolOrTemplating
48
     * @param Pool|TemplateRegistryInterface|null $templateRegistryOrPool
49
     */
50
    public function __construct(
51
        $twigOrName,
52
        ?object $poolOrTemplating,
53
        ?object $templateRegistryOrPool,
54
        ?TemplateRegistryInterface $templateRegistry = null
55
    ) {
56
        if ($poolOrTemplating instanceof Pool) {
57
            if (!$twigOrName instanceof Environment) {
58
                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... \gettype($twigOrName)).

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...
59
                    'Argument 1 passed to %s() must be an instance of %s, %s given.',
60
                    __METHOD__,
61
                    Environment::class,
62
                    \is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName)
63
                ));
64
            }
65
66
            if (null !== $templateRegistryOrPool && !$templateRegistryOrPool instanceof TemplateRegistryInterface) {
67
                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 3 pass... \gettype($twigOrName)).

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...
68
                    'Argument 3 passed to %s() must be either null or an instance of %s, %s given.',
69
                    __METHOD__,
70
                    TemplateRegistryInterface::class,
71
                    \is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName)
72
                ));
73
            }
74
75
            parent::__construct($twigOrName);
76
77
            $this->pool = $poolOrTemplating;
78
            $this->templateRegistry = $templateRegistryOrPool ?: new TemplateRegistry();
79
        } elseif (null === $poolOrTemplating || $poolOrTemplating instanceof EngineInterface) {
80
            @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...
81
                'Passing %s as argument 2 to %s() is deprecated since sonata-project/admin-bundle 3.x'
82
                .' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
83
                null === $poolOrTemplating ? 'null' : EngineInterface::class,
84
                __METHOD__,
85
                Pool::class
86
            ), E_USER_DEPRECATED);
87
88
            if (!$templateRegistryOrPool instanceof Pool) {
89
                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...emplateRegistryOrPool)).

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...
90
                    'Argument 2 passed to %s() must be an instance of %s, %s given.',
91
                    __METHOD__,
92
                    Pool::class,
93
                    null === $templateRegistryOrPool ? 'null' : 'instance of '.\get_class($templateRegistryOrPool)
94
                ));
95
            }
96
97
            parent::__construct($twigOrName, $poolOrTemplating);
98
99
            $this->pool = $templateRegistryOrPool;
100
            $this->templateRegistry = $templateRegistry ?: new TemplateRegistry();
101
        } else {
102
            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...ass($poolOrTemplating)).

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...
103
                'Argument 2 passed to %s() must be either null or an instance of %s or preferably %s, instance of %s given.',
104
                __METHOD__,
105
                EngineInterface::class,
106
                Pool::class,
107
                \get_class($poolOrTemplating)
108
            ));
109
        }
110
    }
111
112
    public function execute(BlockContextInterface $blockContext, ?Response $response = null)
113
    {
114
        $dashboardGroups = $this->pool->getDashboardGroups();
115
116
        $settings = $blockContext->getSettings();
117
118
        $visibleGroups = [];
119
        foreach ($dashboardGroups as $name => $dashboardGroup) {
120
            if (!$settings['groups'] || \in_array($name, $settings['groups'], true)) {
121
                $visibleGroups[] = $dashboardGroup;
122
            }
123
        }
124
125
        return $this->renderPrivateResponse($this->templateRegistry->getTemplate('list_block'), [
126
            'block' => $blockContext->getBlock(),
127
            'settings' => $settings,
128
            'admin_pool' => $this->pool,
129
            'groups' => $visibleGroups,
130
        ], $response);
131
    }
132
133
    public function getName()
134
    {
135
        return 'Admin List';
136
    }
137
138
    public function configureSettings(OptionsResolver $resolver)
139
    {
140
        $resolver->setDefaults([
141
            'groups' => false,
142
        ]);
143
144
        $resolver->setAllowedTypes('groups', ['bool', 'array']);
145
    }
146
}
147