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