Completed
Pull Request — master (#80)
by Hiraku
02:20
created

Plugin::setPluginConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
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 1
        if (count($packages) >= $pluginConfig['minConnections']) {
87 1
            $downloader = new ParallelDownloader($this->io, $this->config);
88 1
            $downloader->download($packages, $pluginConfig);
89 1
        }
90 1
    }
91
92
    /**
93
     * @param DependencyResolver\Operation\OperationInterface[]
94
     * @return Package\PackageInterface[]
95
     */
96 1
    private static function filterPackages(array $operations)
97
    {
98 1
        $packs = array();
99 1
        foreach ($operations as $op) {
100 1
            $type = $op->getJobType();
101 1
            if ('install' === $type) {
102 1
                $packs[] = $op->getPackage();
103 1
                continue;
104
            }
105
106 1
            if ('update' === $type) {
107 1
                $packs[] = $op->getTargetPackage();
108 1
                continue;
109
            }
110 1
        }
111 1
        return $packs;
112
    }
113
114 2
    private function setPluginConfig()
115
    {
116 2
        $config = $this->config->get('prestissimo');
117 2
        if (!is_array($config)) {
118 2
            $config = array();
119 2
        }
120 2
        return new Config($config);
121
    }
122
123 1
    public function disable()
124
    {
125 1
        $this->disabled = true;
126 1
    }
127
128 1
    public function isDisabled()
129
    {
130 1
        return $this->disabled;
131
    }
132
}
133