Completed
Pull Request — master (#50)
by Hiraku
03:11
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 Composer */
22
    private $composer;
23
24
    /** @var IO\IOInterface */
25
    private $io;
26
27
    /** @var Composer\Config */
28
    private $config;
29
30
    /** @var Config */
31
    private $pluginConfig;
32
33
    /** @var boolean */
34
    private $disabled = false;
35
36
    private static $pluginClasses = array(
37
        'Aspects\AspectAuth',
38
        'Aspects\AspectProxy',
39
        'Aspects\AspectRedirect',
40
        'Aspects\GitHubRequest',
41
        'Aspects\GitLabRequest',
42
        'Aspects\HttpGetRequest',
43
        'Aspects\HttpGetResponse',
44
        'Aspects\JoinPoint',
45
        'Config',
46
        'CurlRemoteFilesystem',
47
        'Factory',
48
        'OutputFile',
49
        'ParallelDownloader',
50
        'Plugin',
51
    );
52
53 3
    public function activate(Composer $composer, IO\IOInterface $io)
54
    {
55
        // @codeCoverageIgnoreStart
56
        // guard for self-update problem
57
        if (__CLASS__ !== 'Hirak\Prestissimo\Plugin') {
58
            return $this->disable();
59
        }
60
        // @codeCoverageIgnoreEnd
61
62
        // load all classes
63 3
        foreach (self::$pluginClasses as $class) {
64 3
            class_exists(__NAMESPACE__ . '\\' . $class);
65 3
        }
66
67 3
        $this->composer = $composer;
68 3
        $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...
69 3
        $this->io = $io;
70 3
        $this->pluginConfig = $this->setPluginConfig();
71 3
    }
72
73 1
    public static function getSubscribedEvents()
74
    {
75
        return array(
76 1
            CPlugin\PluginEvents::PRE_FILE_DOWNLOAD => array(
77 1
                array('onPreFileDownload', 0),
78 1
            ),
79 1
            Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array(
80 1
                array('onPostDependenciesSolving', PHP_INT_MAX),
81 1
            ),
82 1
        );
83
    }
84
85 1
    public function onPreFileDownload(CPlugin\PreFileDownloadEvent $ev)
86
    {
87 1
        if ($this->disabled) {
88 1
            return;
89
        }
90 1
        $scheme = parse_url($ev->getProcessedUrl(), PHP_URL_SCHEME);
91 1
        if ($scheme === 'http' || $scheme === 'https') {
92 1
            $rfs = $ev->getRemoteFilesystem();
93
94 1
            $curlrfs = new CurlRemoteFilesystem(
95 1
                $this->io,
96 1
                $this->config,
97 1
                $rfs->getOptions()
98 1
            );
99 1
            $curlrfs->setPluginConfig($this->pluginConfig->get());
100 1
            $ev->setRemoteFilesystem($curlrfs);
101 1
        }
102 1
    }
103
104
    /**
105
     * pre-fetch parallel by curl_multi
106
     */
107 1
    public function onPostDependenciesSolving(Installer\InstallerEvent $ev)
108
    {
109 1
        if ($this->disabled) {
110 1
            return;
111
        }
112 1
        $ops = $ev->getOperations();
113 1
        $packages = $this->filterPackages($ops);
114 1
        $pluginConfig = $this->pluginConfig->get();
115 1
        if (count($packages) >= $pluginConfig['minConnections']) {
116
            $downloader = new ParallelDownloader($this->io, $this->config);
117
            $downloader->download($packages, $pluginConfig);
118
        }
119 1
    }
120
121
    /**
122
     * @param DependencyResolver\Operation\OperationInterface[]
123
     * @return Package\PackageInterface[]
124
     */
125 1
    private static function filterPackages(array $operations)
126
    {
127 1
        $packs = array();
128 1
        foreach ($operations as $op) {
129
            switch ($op->getJobType()) {
130
                case 'install':
131
                    $packs[] = $op->getPackage();
132
                    break;
133
                case 'update':
134
                    $packs[] = $op->getTargetPackage();
135
                    break;
136
            }
137 1
        }
138 1
        return $packs;
139
    }
140
141 3
    private function setPluginConfig()
142
    {
143 3
        $config = $this->config->get('prestissimo');
144 3
        if (!is_array($config)) {
145 3
            $config = array();
146 3
        }
147 3
        return new Config($config);
148
    }
149
150 2
    public function disable()
151
    {
152 2
        $this->disabled = true;
153 2
    }
154
155 1
    public function isDisabled()
156
    {
157 1
        return $this->disabled;
158
    }
159
}
160