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

AdminStatsBlockService::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.9368
c 0
b 0
f 0
cc 5
nc 5
nop 3
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: Remove `$templating` argument.
38
     *
39
     * @param Environment|string $twigOrName
40
     */
41
    public function __construct($twigOrName, ?EngineInterface $templating, Pool $pool)
42
    {
43
        // NEXT_MAJOR: Remove conditions, only pass the Environment
44
        if ($templating instanceof EngineInterface) {
45
            @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...
46
                'Passing %s as argument 2 to %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x'
47
                . ' and will throw a \TypeError in version 4.0. You must pass null instead.',
48
                EngineInterface::class,
49
                __METHOD__
50
            ), E_USER_DEPRECATED);
51
52
            if (!\is_string($twigOrName)) {
53
                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...
54
                    'Passing other than string as argument 1 to %s() is not allowed when %s is passed as argument 2.'
55
                    . ' You must pass an string instead.',
56
                    __METHOD__,
57
                    EngineInterface::class
58
                ));
59
            }
60
61
            parent::__construct($twigOrName, $templating);
0 ignored issues
show
Documentation introduced by
$twigOrName 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 $templating.

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
        } elseif ($twigOrName instanceof Environment) {
63
            if (!\is_null($templating)) {
64
                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...
65
                    'Argument 2 passed to %s() must be null if you passing %s as first argument, %s given.',
66
                    __METHOD__,
67
                    Environment::class,
68
                    EngineInterface::class
69
                ));
70
            }
71
72
            parent::__construct($twigOrName);
73
        } else {
74
            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...
75
                'Argument 1 passed to %s() should be an instance of %s, and you should leave argument 2 as null.',
76
                __METHOD__,
77
                Environment::class
78
            ));
79
        }
80
81
        $this->pool = $pool;
82
    }
83
84
    public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
85
    {
86
        $admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('code'));
87
88
        $datagrid = $admin->getDatagrid();
89
90
        $filters = $blockContext->getSetting('filters');
91
92
        if (!isset($filters['_per_page'])) {
93
            $filters['_per_page'] = ['value' => $blockContext->getSetting('limit')];
94
        }
95
96
        foreach ($filters as $name => $data) {
97
            $datagrid->setValue($name, $data['type'] ?? null, $data['value']);
98
        }
99
100
        $datagrid->buildPager();
101
102
        return $this->renderPrivateResponse($blockContext->getTemplate(), [
103
            'block' => $blockContext->getBlock(),
104
            'settings' => $blockContext->getSettings(),
105
            'admin_pool' => $this->pool,
106
            'admin' => $admin,
107
            'pager' => $datagrid->getPager(),
108
            'datagrid' => $datagrid,
109
        ], $response);
110
    }
111
112
    public function getName()
113
    {
114
        return 'Admin Stats';
115
    }
116
117
    public function configureSettings(OptionsResolver $resolver): void
118
    {
119
        $resolver->setDefaults([
120
            'icon' => 'fa-line-chart',
121
            'text' => 'Statistics',
122
            'translation_domain' => null,
123
            'color' => 'bg-aqua',
124
            'code' => false,
125
            'filters' => [],
126
            'limit' => 1000,
127
            'template' => '@SonataAdmin/Block/block_stats.html.twig',
128
        ]);
129
    }
130
}
131