Completed
Push — master ( e8ce57...2ef872 )
by Craig
10:53 queued 04:32
created

AbstractExtensionInstaller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 6
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
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\ExtensionsModule\Installer;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use Doctrine\Persistence\ObjectManager;
18
use LogicException;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
use Symfony\Contracts\Translation\TranslatorInterface;
21
use Zikula\Bundle\CoreBundle\Doctrine\Helper\SchemaHelper;
22
use Zikula\Bundle\CoreBundle\Translation\TranslatorTrait;
23
use Zikula\ExtensionsModule\AbstractExtension;
24
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
25
use Zikula\ExtensionsModule\ExtensionVariablesTrait;
26
27
/**
28
 * Base class for extension installation and upgrade routines.
29
 */
30
abstract class AbstractExtensionInstaller implements ExtensionInstallerInterface
31
{
32
    use TranslatorTrait;
33
    use ExtensionVariablesTrait;
34
35
    /**
36
     * @var string the extension name
37
     */
38
    protected $name;
39
40
    /**
41
     * @var AbstractExtension
42
     */
43
    protected $extension;
44
45
    /**
46
     * @var ManagerRegistry
47
     */
48
    protected $managerRegistry;
49
50
    /**
51
     * @var ObjectManager
52
     */
53
    protected $entityManager;
54
55
    /**
56
     * @var SchemaHelper
57
     */
58
    protected $schemaTool;
59
60
    /**
61
     * @var RequestStack
62
     */
63
    protected $requestStack;
64
65
    public function __construct(
66
        AbstractExtension $extension,
67
        ManagerRegistry $managerRegistry,
68
        SchemaHelper $schemaTool,
69
        RequestStack $requestStack,
70
        TranslatorInterface $translator,
71
        VariableApiInterface $variableApi
72
    ) {
73
        $this->extension = $extension;
74
        $this->name = $extension->getName();
75
        $this->managerRegistry = $managerRegistry;
76
        $this->entityManager = $managerRegistry->getManager();
77
        $this->schemaTool = $schemaTool;
78
        $this->requestStack = $requestStack;
79
        $this->setTranslator($translator);
80
        $this->extensionName = $this->name; // ExtensionVariablesTrait
81
        $this->variableApi = $variableApi; // 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...
82
    }
83
84
    /**
85
     * Initialise the extension
86
     */
87
    abstract public function install(): bool;
88
89
    /**
90
     * Upgrade the extension.
91
     */
92
    abstract public function upgrade(string $oldVersion): bool;
93
94
    /**
95
     * Delete the extension.
96
     */
97
    abstract public function uninstall(): bool;
98
99
    /**
100
     * Convenience shortcut to add a session flash message.
101
     */
102
    public function addFlash(string $type, string $message): void
103
    {
104
        $request = $this->requestStack->getCurrentRequest();
105
        if (null === $request) {
106
            echo ucfirst($type) . ': ' . $message . "\n";
107
108
            return;
109
        }
110
        if (!$request->hasSession()) {
111
            throw new LogicException('You can not use the addFlash method if sessions are disabled.');
112
        }
113
114
        $request->getSession()->getFlashBag()->add($type, $message);
115
    }
116
}
117