Passed
Branch master (4933f5)
by ANTHONIUS
05:09
created

Plugin   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 160
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 7 1
A onPrePackageUninstall() 0 4 1
A getInstalledModules() 0 3 1
A onPostPackageUpdate() 0 4 1
A getUninstalledModules() 0 3 1
A onPostPackageInstall() 0 4 1
A addModules() 0 17 4
A activate() 0 14 1
A getApplication() 0 12 3
A onPostAutoloadDump() 0 17 3
1
<?php
2
3
/*
4
 * This file is part of the Yawik project.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
11
namespace Yawik\Composer;
12
13
use Composer\Composer;
14
use Composer\DependencyResolver\Operation\InstallOperation;
15
use Composer\DependencyResolver\Operation\UpdateOperation;
16
use Composer\DependencyResolver\Operation\UninstallOperation;
17
use Composer\EventDispatcher\EventDispatcher;
18
use Composer\EventDispatcher\EventSubscriberInterface;
19
use Composer\Installer\PackageEvent;
20
use Composer\IO\IOInterface;
21
use Composer\Package\PackageInterface;
22
use Composer\Plugin\PluginInterface;
23
use Composer\Script\Event as ScriptEvent;
24
use Core\Application;
25
use Symfony\Component\Console\Input\ArgvInput;
26
use Symfony\Component\Console\Output\ConsoleOutput;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Style\SymfonyStyle;
29
use Yawik\Composer\Event\ActivateEvent;
30
use Yawik\Composer\Event\ConfigureEvent;
31
32
/**
33
 * Class Plugin
34
 * @package Yawik\Composer
35
 * @author  Anthonius Munthi <[email protected]>
36
 * @since   0.32.0
37
 * @TODO    Create more documentation for methods
38
 */
39
class Plugin implements PluginInterface, EventSubscriberInterface
40
{
41
    const YAWIK_ACTIVATE_EVENT  = 'yawik.activate';
42
43
    const YAWIK_CONFIGURE_EVENT = 'yawik.configure';
44
45
    const YAWIK_MODULE_TYPE     = 'yawik-module';
46
47
    const ADD_TYPE_INSTALL      = 'install';
48
49
    const ADD_TYPE_REMOVE       = 'remove';
50
51
    /**
52
     * @var EventDispatcher
53
     */
54
    protected $dispatcher;
55
56
    /**
57
     * @var Application
58
     */
59
    protected $application;
60
61
    /**
62
     * @var Composer
63
     */
64
    protected $composer;
65
66
    /**
67
     * @var IOInterface
68
     */
69
    protected $output;
70
71
    /**
72
     * An array list of available modules
73
     *
74
     * @var array
75
     */
76
    protected $installed = [];
77
78
    protected $uninstalled = [];
79
80
    /**
81
     * A lists of available installed yawik modules type
82
     * @var array
83
     */
84
    protected $yawikModules;
85
86 2
    public function activate(Composer $composer, IOInterface $io)
87
    {
88 2
        $dispatcher       = $composer->getEventDispatcher();
89 2
        $assets           = new AssetsInstaller();
90 2
        $fixer            = new PermissionsFixer();
91
92
93 2
        $event = new ActivateEvent($composer, $io);
94 2
        $dispatcher->addSubscriber($assets);
95 2
        $dispatcher->addSubscriber($fixer);
96
97 2
        $dispatcher->dispatch(static::YAWIK_ACTIVATE_EVENT, $event);
98 2
        $this->composer   = $composer;
99 2
        $this->output     = $io;
100 2
    }
101
102
    /**
103
     * Provide composer event listeners.
104
     *
105
     * @return array
106
     */
107 1
    public static function getSubscribedEvents()
108
    {
109
        return [
110 1
            'post-autoload-dump'     => 'onPostAutoloadDump',
111
            'post-package-install'   => 'onPostPackageInstall',
112
            'post-package-update'    => 'onPostPackageUpdate',
113
            'pre-package-uninstall'  => 'onPrePackageUninstall'
114
        ];
115
    }
116
117
    /**
118
     * Get Yawik Application to use
119
     * @return Application|\Zend\Mvc\Application
120
     */
121 2
    public function getApplication()
122
    {
123
        // @codeCoverageIgnoreStart
124
        if (is_file($file = __DIR__.'/../../../autoload.php')) {
125
            include $file;
126
        }
127
        // @codeCoverageIgnoreEnd
128
129 2
        if (!is_object($this->application)) {
130 1
            $this->application = Application::init();
131
        }
132 2
        return $this->application;
133
    }
134
135 2
    public function getInstalledModules()
136
    {
137 2
        return $this->installed;
138
    }
139
140 1
    public function getUninstalledModules()
141
    {
142 1
        return $this->uninstalled;
143
    }
144
145 1
    public function onPostAutoloadDump()
146
    {
147 1
        $app              = $this->getApplication();
148 1
        $modules          = $app->getServiceManager()->get('ModuleManager')->getLoadedModules();
149 1
        $installed        = $this->installed;
150 1
        $coreOptions      = $app->getServiceManager()->get('Core/Options');
151
152 1
        foreach ($installed as $moduleName) {
153 1
            $className = $moduleName . '\\Module';
154 1
            if (class_exists($className, true)) {
155 1
                $modules[] = new $className;
156
            }
157
        }
158
159 1
        $event = new ConfigureEvent($coreOptions, $modules);
160 1
        $dispatcher = $this->composer->getEventDispatcher();
161 1
        $dispatcher->dispatch(self::YAWIK_CONFIGURE_EVENT, $event);
162 1
    }
163
164 1
    public function onPostPackageInstall(PackageEvent $event)
165
    {
166 1
        $package = $event->getOperation()->getPackage();
0 ignored issues
show
Bug introduced by
The method getPackage() does not exist on Composer\DependencyResol...tion\OperationInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Composer\DependencyResol...eration\SolverOperation or Composer\DependencyResol...eration\UpdateOperation. Are you sure you never get one of those? ( Ignorable by Annotation )

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

166
        $package = $event->getOperation()->/** @scrutinizer ignore-call */ getPackage();
Loading history...
167 1
        $this->addModules($package, static::ADD_TYPE_INSTALL);
168 1
    }
169
170 1
    public function onPostPackageUpdate(PackageEvent $event)
171
    {
172 1
        $package = $event->getOperation()->getTargetPackage();
0 ignored issues
show
Bug introduced by
The method getTargetPackage() does not exist on Composer\DependencyResol...tion\OperationInterface. It seems like you code against a sub-type of Composer\DependencyResol...tion\OperationInterface such as Composer\DependencyResol...eration\UpdateOperation. ( Ignorable by Annotation )

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

172
        $package = $event->getOperation()->/** @scrutinizer ignore-call */ getTargetPackage();
Loading history...
173 1
        $this->addModules($package, static::ADD_TYPE_INSTALL);
174 1
    }
175
176 1
    public function onPrePackageUninstall(PackageEvent $event)
177
    {
178 1
        $package = $event->getOperation()->getPackage();
179 1
        $this->addModules($package, static::ADD_TYPE_REMOVE);
180 1
    }
181
182 2
    public function addModules(PackageInterface $package, $scanType='install')
183
    {
184 2
        $type           = $package->getType();
185 2
        $extras         = $package->getExtra();
186
187 2
        if ($type === static::YAWIK_MODULE_TYPE) {
188
            // we skip undefined zf module definition
189 2
            if (isset($extras['zf']['module'])) {
190
                // we register module class name
191 2
                $moduleName     = $extras['zf']['module'];
192 2
                if (self::ADD_TYPE_REMOVE == $scanType) {
193 1
                    $this->uninstalled[] = $moduleName;
194
                } else {
195 2
                    $this->installed[]   = $moduleName;
196
                }
197
            } else {
198 1
                $this->output->write('[warning] No module definition for: ' . $package->getName());
199
            }
200
        }
201 2
    }
202
}
203