Completed
Push — master ( 564ad8...daabe2 )
by Hiraku
8s
created

Plugin   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 18
Bugs 1 Features 3
Metric Value
wmc 15
c 18
b 1
f 3
lcom 1
cbo 4
dl 0
loc 117
ccs 47
cts 47
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A activate() 0 18 3
A getSubscribedEvents() 0 8 1
A onPostDependenciesSolving() 0 14 3
A filterPackages() 0 17 4
A setPluginConfig() 0 8 2
A disable() 0 4 1
A isDisabled() 0 4 1
1
<?php
2
/*
3
 * hirak/prestissimo
4
 * @author Hiraku NAKANO
5
 * @license MIT https://github.com/hirak/prestissimo
6
 */
7
namespace Hirak\Prestissimo;
8
9
use Composer\Composer;
10
use Composer\IO;
11
use Composer\Plugin as CPlugin;
12
use Composer\EventDispatcher;
13
use Composer\Package;
14
use Composer\Installer;
15
use Composer\DependencyResolver;
16
17
class Plugin implements
18
    CPlugin\PluginInterface,
19
    EventDispatcher\EventSubscriberInterface
20
{
21
    /** @var IO\IOInterface */
22
    private $io;
23
24
    /** @var Composer\Config */
25
    private $config;
26
27
    /** @var Config */
28
    private $pluginConfig;
29
30
    /** @var boolean */
31
    private $disabled = false;
32
33
    private static $pluginClasses = array(
34
        'GitHubRequest',
35
        'GitLabRequest',
36
        'HttpGetRequest',
37
        'HttpGetResponse',
38
        'Config',
39
        'CurlMulti',
40
        'Factory',
41
        'FileDownloaderDummy',
42
        'OutputFile',
43
        'ParallelDownloader',
44
        'Plugin',
45
    );
46
47 2
    public function activate(Composer $composer, IO\IOInterface $io)
48
    {
49
        // @codeCoverageIgnoreStart
50
        // guard for self-update problem
51
        if (__CLASS__ !== 'Hirak\Prestissimo\Plugin') {
52
            return $this->disable();
53
        }
54
        // @codeCoverageIgnoreEnd
55
56
        // load all classes
57 2
        foreach (self::$pluginClasses as $class) {
58 2
            class_exists(__NAMESPACE__ . '\\' . $class);
59 2
        }
60
61 2
        $this->config = $composer->getConfig();
0 ignored issues
show
Documentation Bug introduced by
It seems like $composer->getConfig() of type object<Composer\Config> is incompatible with the declared type object<Composer\Composer\Config> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62 2
        $this->io = $io;
63 2
        $this->pluginConfig = $this->setPluginConfig();
64 2
    }
65
66 1
    public static function getSubscribedEvents()
67
    {
68
        return array(
69 1
            Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array(
70 1
                array('onPostDependenciesSolving', PHP_INT_MAX),
71 1
            ),
72 1
        );
73
    }
74
75
    /**
76
     * pre-fetch parallel by curl_multi
77
     */
78 1
    public function onPostDependenciesSolving(Installer\InstallerEvent $ev)
79
    {
80 1
        if ($this->disabled) {
81 1
            return;
82
        }
83 1
        $ops = $ev->getOperations();
84 1
        $packages = $this->filterPackages($ops);
85 1
        $pluginConfig = $this->pluginConfig->get();
86
87 1
        if ($packages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $packages of type Composer\Package\PackageInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
88 1
            $downloader = new ParallelDownloader($this->io, $this->config);
89 1
            $downloader->download($packages, $pluginConfig);
90 1
        }
91 1
    }
92
93
    /**
94
     * @param DependencyResolver\Operation\OperationInterface[]
95
     * @return Package\PackageInterface[]
96
     */
97 1
    private static function filterPackages(array $operations)
98
    {
99 1
        $packs = array();
100 1
        foreach ($operations as $op) {
101 1
            $type = $op->getJobType();
102 1
            if ('install' === $type) {
103 1
                $packs[] = $op->getPackage();
104 1
                continue;
105
            }
106
107 1
            if ('update' === $type) {
108 1
                $packs[] = $op->getTargetPackage();
109 1
                continue;
110
            }
111 1
        }
112 1
        return $packs;
113
    }
114
115 2
    private function setPluginConfig()
116
    {
117 2
        $config = $this->config->get('prestissimo');
118 2
        if (!is_array($config)) {
119 2
            $config = array();
120 2
        }
121 2
        return new Config($config);
122
    }
123
124 1
    public function disable()
125
    {
126 1
        $this->disabled = true;
127 1
    }
128
129 1
    public function isDisabled()
130
    {
131 1
        return $this->disabled;
132
    }
133
}
134