Completed
Push — master ( ef068f...0ead48 )
by Axel
05:18
created

AbstractBlockHandler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
c 0
b 0
f 0
dl 0
loc 131
rs 10
wmc 15

12 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 2
A addFlash() 0 11 3
A getFormClassName() 0 3 1
A getFormOptions() 0 3 1
A getFormTemplate() 0 3 1
A __construct() 0 16 1
A getPropertyDefaults() 0 3 1
A getBundle() 0 3 1
A hasPermission() 0 7 1
A display() 0 3 1
A renderView() 0 3 1
A getType() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
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 Zikula\BlocksModule;
15
16
use LogicException;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
use Symfony\Contracts\Translation\TranslatorInterface;
19
use Twig\Environment;
20
use Zikula\Bundle\CoreBundle\AbstractBundle;
21
use Zikula\Bundle\CoreBundle\Translation\TranslatorTrait;
22
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
23
use Zikula\ExtensionsModule\ExtensionVariablesTrait;
24
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface;
25
26
abstract class AbstractBlockHandler implements BlockHandlerInterface
27
{
28
    use TranslatorTrait;
29
    use ExtensionVariablesTrait;
30
31
    /**
32
     * @var AbstractBundle
33
     */
34
    protected $bundle;
35
36
    /**
37
     * @var RequestStack
38
     */
39
    protected $requestStack;
40
41
    /**
42
     * @var PermissionApiInterface
43
     */
44
    protected $permissionApi;
45
46
    /**
47
     * @var Environment
48
     */
49
    protected $twig;
50
51
    public function __construct(
52
        AbstractBundle $bundle,
53
        RequestStack $requestStack,
54
        TranslatorInterface $translator,
55
        VariableApiInterface $variableApi,
56
        PermissionApiInterface $permissionApi,
57
        Environment $twig
58
    ) {
59
        $this->bundle = $bundle;
60
        $this->extensionName = $bundle->getName(); // for ExtensionVariablesTrait
61
        $this->requestStack = $requestStack;
62
        $this->setTranslator($translator); // for TranslatorTrait
63
        $this->variableApi = $variableApi; // for ExtensionVariablesTrait
0 ignored issues
show
Documentation Bug introduced by
$variableApi is of type Zikula\ExtensionsModule\...ce\VariableApiInterface, but the property $variableApi was declared to be of type Zikula\ExtensionsModule\Api\VariableApi. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
64
        $this->permissionApi = $permissionApi;
65
        $this->twig = $twig;
66
        $this->boot($bundle);
67
    }
68
69
    /**
70
     * Boot the handler.
71
     */
72
    protected function boot(AbstractBundle $bundle): void
73
    {
74
        // load optional bootstrap
75
        $bootstrap = $bundle->getPath() . '/bootstrap.php';
76
        if (file_exists($bootstrap)) {
77
            include_once $bootstrap;
78
        }
79
    }
80
81
    public function getFormClassName(): string
82
    {
83
        return '';
84
    }
85
86
    public function getFormOptions(): array
87
    {
88
        return [];
89
    }
90
91
    public function getFormTemplate(): string
92
    {
93
        return '@ZikulaBlocksModule/Block/default_modify.html.twig';
94
    }
95
96
    public function getPropertyDefaults(): array
97
    {
98
        return [];
99
    }
100
101
    public function display(array $properties): string
102
    {
103
        return nl2br(implode("\n", $properties));
104
    }
105
106
    public function getType(): string
107
    {
108
        // default to the ClassName without the `Block` suffix
109
        // note: This string is intentionally left untranslated.
110
        $fqCn = get_class($this);
111
        $pos = mb_strrpos($fqCn, '\\');
112
113
        return mb_substr($fqCn, $pos + 1, -5);
114
    }
115
116
    /**
117
     * Adds a flash message to the current session for type.
118
     *
119
     * @throws LogicException
120
     */
121
    protected function addFlash(string $type, string $message): void
122
    {
123
        $request = $this->requestStack->getCurrentRequest();
124
        if (null === $request) {
125
            return;
126
        }
127
        if (!$request->hasSession()) {
128
            throw new LogicException('You can not use the addFlash method if sessions are disabled.');
129
        }
130
131
        $request->getSession()->getFlashBag()->add($type, $message);
132
    }
133
134
    /**
135
     * Returns a rendered view.
136
     */
137
    public function renderView(string $view, array $parameters = []): string
138
    {
139
        return $this->twig->render($view, $parameters);
140
    }
141
142
    /**
143
     * Convenience shortcut to check if user has requested permissions.
144
     */
145
    protected function hasPermission(
146
        string $component = null,
147
        string $instance = null,
148
        int $level = null,
149
        int $user = null
150
    ): bool {
151
        return $this->permissionApi->hasPermission($component, $instance, $level, $user);
0 ignored issues
show
Bug introduced by
It seems like $level can also be of type null; however, parameter $level of Zikula\PermissionsModule...erface::hasPermission() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
        return $this->permissionApi->hasPermission($component, $instance, /** @scrutinizer ignore-type */ $level, $user);
Loading history...
152
    }
153
154
    public function getBundle(): AbstractBundle
155
    {
156
        return $this->bundle;
157
    }
158
}
159