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