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

AdminStatsBlockService::configureSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 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\Search\SearchHandler;
18
use Sonata\AdminBundle\Templating\TemplateRegistry;
19
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
20
use Sonata\BlockBundle\Block\BlockContextInterface;
21
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
22
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\OptionsResolver\OptionsResolver;
25
use Twig\Environment;
26
27
/**
28
 * @final since sonata-project/admin-bundle 3.52
29
 *
30
 * @author Thomas Rabaix <[email protected]>
31
 */
32
class AdminStatsBlockService extends AbstractBlockService
33
{
34
    /**
35
     * @var Pool
36
     */
37
    protected $pool;
38
39
    /**
40
     * NEXT_MAJOR: Change signature for
41
     * - Environment $twig
42
     * - Pool $pool
43
     *
44
     * @param Environment|string        $twigOrName
45
     * @param Pool|EngineInterface|null $poolOrTemplating
46
     */
47
    public function __construct($twigOrName, ?object $poolOrTemplating, ?Pool $pool = null)
48
    {
49
        if ($poolOrTemplating instanceof Pool) {
50
            if (!$twigOrName instanceof Environment) {
51
                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...
52
                    'Argument 1 passed to %s() must be either an instance of %s, %s given.',
53
                    __METHOD__,
54
                    Environment::class,
55
                    \is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName)
56
                ));
57
            }
58
59
            parent::__construct($twigOrName);
60
61
            $this->pool = $poolOrTemplating;
62
        } elseif (null === $poolOrTemplating || $poolOrTemplating instanceof EngineInterface) {
63
            @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...
64
                'Passing %s as argument 2 to %s() is deprecated since sonata-project/admin-bundle 3.x'
65
                .' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
66
                null === $poolOrTemplating ? 'null' : EngineInterface::class,
67
                __METHOD__,
68
                Pool::class
69
            ), E_USER_DEPRECATED);
70
71
            if (null === $pool) {
72
                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 null as...ndle\Admin\Pool::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...
73
                    'Passing null as argument 3 to %s() is not allowed when %s is passed as argument 2.'
74
                    .' You must pass an instance of %s instead.',
75
                    __METHOD__,
76
                    EngineInterface::class,
77
                    Pool::class
78
                ));
79
            }
80
81
            parent::__construct($twigOrName, $poolOrTemplating);
82
83
            $this->pool = $pool;
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 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...
86
                'Argument 2 passed to %s() must be either null or an instance of %s or preferably %s, instance of %s given.',
87
                __METHOD__,
88
                EngineInterface::class,
89
                Pool::class,
90
                \get_class($poolOrTemplating)
91
            ));
92
        }
93
    }
94
95
    public function execute(BlockContextInterface $blockContext, ?Response $response = null)
96
    {
97
        $admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('code'));
98
99
        $datagrid = $admin->getDatagrid();
100
101
        $filters = $blockContext->getSetting('filters');
102
103
        if (!isset($filters['_per_page'])) {
104
            $filters['_per_page'] = ['value' => $blockContext->getSetting('limit')];
105
        }
106
107
        foreach ($filters as $name => $data) {
108
            $datagrid->setValue($name, $data['type'] ?? null, $data['value']);
109
        }
110
111
        $datagrid->buildPager();
112
113
        return $this->renderPrivateResponse($blockContext->getTemplate(), [
114
            'block' => $blockContext->getBlock(),
115
            'settings' => $blockContext->getSettings(),
116
            'admin_pool' => $this->pool,
117
            'admin' => $admin,
118
            'pager' => $datagrid->getPager(),
119
            'datagrid' => $datagrid,
120
        ], $response);
121
    }
122
123
    public function getName()
124
    {
125
        return 'Admin Stats';
126
    }
127
128
    public function configureSettings(OptionsResolver $resolver)
129
    {
130
        $resolver->setDefaults([
131
            'icon' => 'fa-line-chart',
132
            'text' => 'Statistics',
133
            'translation_domain' => null,
134
            'color' => 'bg-aqua',
135
            'code' => false,
136
            'filters' => [],
137
            'limit' => 1000,
138
            'template' => '@SonataAdmin/Block/block_stats.html.twig',
139
        ]);
140
    }
141
}
142