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

AdminListBlockService   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 6
dl 0
loc 119
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
45
     * - Environment $twig
46
     * - Pool $pool
47
     * - ?TemplateRegistryInterface $templateRegistry = null
48
     *
49
     * @param Environment|string                  $twigOrName
50
     * @param EngineInterface|Pool|null           $poolOrTemplating
51
     * @param Pool|TemplateRegistryInterface|null $templateRegistryOrPool
52
     */
53
    public function __construct(
54
        $twigOrName,
55
        ?object $poolOrTemplating,
56
        ?object $templateRegistryOrPool,
57
        ?TemplateRegistryInterface $templateRegistry = null
58
    ) {
59
        if ($poolOrTemplating instanceof Pool) {
60
            if (!$twigOrName instanceof Environment) {
61
                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...
62
                    'Argument 1 passed to %s() must be either an instance of %s, %s given.',
63
                    __METHOD__,
64
                    Environment::class,
65
                    \is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName)
66
                ));
67
            }
68
69
            if (null !== $templateRegistryOrPool && !$templateRegistryOrPool instanceof TemplateRegistryInterface) {
70
                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...
71
                    'Argument 3 passed to %s() must be either null or an instance of %s, %s given.',
72
                    __METHOD__,
73
                    TemplateRegistryInterface::class,
74
                    \is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName)
75
                ));
76
            }
77
78
            parent::__construct($twigOrName);
79
80
            $this->pool = $poolOrTemplating;
81
            $this->templateRegistry = $templateRegistryOrPool ?: new TemplateRegistry();
82
        } elseif (null === $poolOrTemplating || $poolOrTemplating instanceof EngineInterface) {
83
            @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...
84
                'Passing %s as argument 2 to %s() is deprecated since sonata-project/admin-bundle 3.x'
85
                .' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
86
                null === $poolOrTemplating ? 'null' : EngineInterface::class,
87
                __METHOD__,
88
                Pool::class
89
            ), E_USER_DEPRECATED);
90
91
            if (!$templateRegistryOrPool instanceof Pool) {
92
                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...
93
                    'Argument 2 passed to %s() must be an instance of %s, %s given.',
94
                    __METHOD__,
95
                    Pool::class,
96
                    null === $templateRegistryOrPool ? 'null' : 'instance of '.\get_class($templateRegistryOrPool)
97
                ));
98
            }
99
100
            parent::__construct($twigOrName, $poolOrTemplating);
101
102
            $this->pool = $templateRegistryOrPool;
103
            $this->templateRegistry = $templateRegistry ?: new TemplateRegistry();
104
        } else {
105
            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...
106
                'Argument 2 passed to %s() must be either null or an instance of %s or preferably %s, instance of %s given.',
107
                __METHOD__,
108
                EngineInterface::class,
109
                Pool::class,
110
                \get_class($poolOrTemplating)
111
            ));
112
        }
113
    }
114
115
    public function execute(BlockContextInterface $blockContext, ?Response $response = null)
116
    {
117
        $dashboardGroups = $this->pool->getDashboardGroups();
118
119
        $settings = $blockContext->getSettings();
120
121
        $visibleGroups = [];
122
        foreach ($dashboardGroups as $name => $dashboardGroup) {
123
            if (!$settings['groups'] || \in_array($name, $settings['groups'], true)) {
124
                $visibleGroups[] = $dashboardGroup;
125
            }
126
        }
127
128
        return $this->renderPrivateResponse($this->templateRegistry->getTemplate('list_block'), [
129
            'block' => $blockContext->getBlock(),
130
            'settings' => $settings,
131
            'admin_pool' => $this->pool,
132
            'groups' => $visibleGroups,
133
        ], $response);
134
    }
135
136
    public function getName()
137
    {
138
        return 'Admin List';
139
    }
140
141
    public function configureSettings(OptionsResolver $resolver)
142
    {
143
        $resolver->setDefaults([
144
            'groups' => false,
145
        ]);
146
147
        $resolver->setAllowedTypes('groups', ['bool', 'array']);
148
    }
149
}
150