Completed
Pull Request — master (#80)
by Hiraku
03:59
created

Plugin::onPreFileDownload()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 18
ccs 15
cts 15
cp 1
rs 9.2
cc 4
eloc 12
nc 3
nop 1
crap 4
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
        'Aspects\AspectAuth',
35
        'Aspects\AspectProxy',
36
        'Aspects\AspectRedirect',
37
        'Aspects\GitHubRequest',
38
        'Aspects\GitLabRequest',
39
        'Aspects\HttpGetRequest',
40
        'Aspects\HttpGetResponse',
41
        'Aspects\JoinPoint',
42
        'Config',
43
        'CurlMulti',
44
        'Factory',
45
        'FileDownloaderDummy',
46
        'OutputFile',
47
        'ParallelDownloader',
48
        'Plugin',
49
    );
50
51
    public function activate(Composer $composer, IO\IOInterface $io)
52 3
    {
53
        // @codeCoverageIgnoreStart
54
        // guard for self-update problem
55
        if (__CLASS__ !== 'Hirak\Prestissimo\Plugin') {
56
            return $this->disable();
57
        }
58
        // @codeCoverageIgnoreEnd
59
60
        // load all classes
61
        foreach (self::$pluginClasses as $class) {
62 3
            class_exists(__NAMESPACE__ . '\\' . $class);
63 3
        }
64 3
65
        $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...
66 3
        $this->io = $io;
67 3
        $this->pluginConfig = $this->setPluginConfig();
68 3
    }
69 3
70
    public static function getSubscribedEvents()
71 1
    {
72
        return array(
73
            Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array(
74 1
                array('onPostDependenciesSolving', PHP_INT_MAX),
75 1
            ),
76 1
        );
77 1
    }
78 1
79 1
    /**
80 1
     * pre-fetch parallel by curl_multi
81
     */
82
    public function onPostDependenciesSolving(Installer\InstallerEvent $ev)
83 1
    {
84
        if ($this->disabled) {
85 1
            return;
86 1
        }
87
        $ops = $ev->getOperations();
88 1
        $packages = $this->filterPackages($ops);
89 1
        $pluginConfig = $this->pluginConfig->get();
90 1
        if (count($packages) >= $pluginConfig['minConnections']) {
91
            $downloader = new ParallelDownloader($this->io, $this->config);
92 1
            $downloader->download($packages, $pluginConfig);
93 1
        }
94 1
    }
95 1
96 1
    /**
97 1
     * @param DependencyResolver\Operation\OperationInterface[]
98 1
     * @return Package\PackageInterface[]
99 1
     */
100 1
    private static function filterPackages(array $operations)
101
    {
102
        $packs = array();
103
        foreach ($operations as $op) {
104
            $type = $op->getJobType();
105 1
            if ('install' === $type) {
106
                $packs[] = $op->getPackage();
107 1
                continue;
108 1
            }
109
110 1
            if ('update' === $type) {
111 1
                $packs[] = $op->getTargetPackage();
112 1
                continue;
113 1
            }
114 1
        }
115 1
        return $packs;
116 1
    }
117 1
118
    private function setPluginConfig()
119
    {
120
        $config = $this->config->get('prestissimo');
121
        if (!is_array($config)) {
122
            $config = array();
123 1
        }
124
        return new Config($config);
125 1
    }
126 1
127 1
    public function disable()
128 1
    {
129 1
        $this->disabled = true;
130 1
    }
131
132
    public function isDisabled()
133 1
    {
134 1
        return $this->disabled;
135 1
    }
136
}
137