Completed
Push — master ( e4f005...8391dd )
by ANTHONIUS
03:42
created

Plugin   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 98.08%

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 135
ccs 51
cts 52
cp 0.9808
rs 10
c 0
b 0
f 0
wmc 19

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 7 1
A activate() 0 4 1
A __construct() 0 3 2
A setAssetsInstaller() 0 3 1
A onPrePackageUninstall() 0 4 1
A onPostPackageUpdate() 0 4 1
A scanModules() 0 20 5
A onPostPackageInstall() 0 4 1
A getAssetsInstaller() 0 11 3
A onPostAutoloadDump() 0 10 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\EventSubscriberInterface;
18
use Composer\Installer\PackageEvent;
19
use Composer\IO\IOInterface;
20
use Composer\Package\PackageInterface;
21
use Composer\Plugin\PluginInterface;
22
use Composer\Script\Event as ScriptEvent;
23
use Symfony\Component\Console\Input\ArgvInput;
24
use Symfony\Component\Console\Output\ConsoleOutput;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
27
/**
28
 * Class Plugin
29
 * @package Yawik\Composer
30
 * @author  Anthonius Munthi <[email protected]>
31
 * @since   0.32.0
32
 * @TODO    Create more documentation for methods
33
 */
34
class Plugin implements PluginInterface, EventSubscriberInterface
35
{
36
    const YAWIK_MODULE_TYPE = 'yawik-module';
37
38
    const SCAN_TYPE_INSTALL = 'install';
39
    const SCAN_TYPE_REMOVE  = 'remove';
40
41
    /**
42
     * @var Composer
43
     */
44
    private $composer;
45
46
    /**
47
     * @var IOInterface
48
     */
49
    private $output;
50
51
    /**
52
     * An array list of available modules
53
     *
54
     * @var array
55
     */
56
    private $installed = [];
57
58
    private $uninstalled = [];
59
60
    private $projectPath;
61
62
    /**
63
     * @var AssetsInstaller
64
     */
65
    private $assetsInstaller;
66
67 5
    public function __construct($projectPath=null)
68
    {
69 5
        $this->projectPath = is_null($projectPath) ? getcwd():$projectPath;
70 5
    }
71
72 4
    public function activate(Composer $composer, IOInterface $io)
73
    {
74 4
        $this->composer = $composer;
75 4
        $this->output   = $io;
76 4
    }
77
78
    /**
79
     * Define AssetsInstaller to use
80
     * This very usefull during testing process
81
     * @param $installer
82
     */
83 5
    public function setAssetsInstaller($installer)
84
    {
85 5
        $this->assetsInstaller = $installer;
86 5
    }
87
88
    /**
89
     * Provide composer event listeners.
90
     *
91
     * @return array
92
     */
93 1
    public static function getSubscribedEvents()
94
    {
95
        return [
96 1
            'post-autoload-dump'     => 'onPostAutoloadDump',
97
            'post-package-install'   => 'onPostPackageInstall',
98
            'post-package-update'    => 'onPostPackageUpdate',
99
            'pre-package-uninstall'  => 'onPrePackageUninstall'
100
        ];
101
    }
102
103 4
    public function onPostAutoloadDump()
104
    {
105 4
        if (count($this->installed) > 0) {
106 2
            $this->getAssetsInstaller()->install($this->installed);
107
        }
108 4
        if (count($this->uninstalled) > 0) {
109 1
            $this->getAssetsInstaller()->uninstall($this->uninstalled);
110
        }
111
112 4
        $this->getAssetsInstaller()->fixDirPermissions();
113 4
    }
114
115 2
    public function onPostPackageInstall(PackageEvent $event)
116
    {
117 2
        $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

117
        $package = $event->getOperation()->/** @scrutinizer ignore-call */ getPackage();
Loading history...
118 2
        $this->scanModules($package, static::SCAN_TYPE_INSTALL);
119 2
    }
120
121 1
    public function onPostPackageUpdate(PackageEvent $event)
122
    {
123 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

123
        $package = $event->getOperation()->/** @scrutinizer ignore-call */ getTargetPackage();
Loading history...
124 1
        $this->scanModules($package, static::SCAN_TYPE_INSTALL);
125 1
    }
126
127 1
    public function onPrePackageUninstall(PackageEvent $event)
128
    {
129 1
        $package = $event->getOperation()->getPackage();
130 1
        $this->scanModules($package, static::SCAN_TYPE_REMOVE);
131 1
    }
132
133
    /**
134
     * @return AssetsInstaller
135
     */
136 5
    public function getAssetsInstaller()
137
    {
138 5
        if (!is_object($this->assetsInstaller)) {
139 1
            $assetInstaller = new AssetsInstaller();
140 1
            if (php_sapi_name()==='cli') {
141 1
                $assetInstaller->setInput(new ArgvInput())->setOutput(new ConsoleOutput());
142 1
                $assetInstaller->getOutput()->setDecorated(true);
143
            }
144 1
            $this->assetsInstaller = $assetInstaller;
145
        }
146 5
        return $this->assetsInstaller;
147
    }
148
149 4
    private function scanModules(PackageInterface $package, $scanType='install')
150
    {
151 4
        $installer      = $this->composer->getInstallationManager();
152 4
        $packagePath    = $installer->getInstallPath($package);
153 4
        $type           = $package->getType();
154 4
        $publicDir      = $packagePath.'/public';
155 4
        $extras         = $package->getExtra();
156
157 4
        if (file_exists($publicDir) && $type === static::YAWIK_MODULE_TYPE) {
158
            // we skip undefined zf module definition
159 3
            if (isset($extras['zf']['module'])) {
160
                // we register module class name
161 3
                $moduleName     = $extras['zf']['module'];
162 3
                if (self::SCAN_TYPE_INSTALL == $scanType) {
163 2
                    $this->installed[$moduleName] = realpath($publicDir);
164
                } else {
165 3
                    $this->uninstalled[] = $moduleName;
166
                }
167
            } else {
168
                $this->output->write('[warning] No module definition for: ' . $package->getName());
169
            }
170
        }
171 4
    }
172
}
173