Completed
Pull Request — master (#42)
by Hiraku
02:14
created

Plugin::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
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
    public function activate(Composer $composer, IO\IOInterface $io)
34
    {
35
        $this->composer = $composer;
36
        $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...
37
        $this->io = $io;
38
        $this->pluginConfig = $this->setPluginConfig();
39
    }
40
41
    public static function getSubscribedEvents()
42
    {
43
        return array(
44
            CPlugin\PluginEvents::PRE_FILE_DOWNLOAD => array(
45
                array('onPreFileDownload', 0),
46
            ),
47
            Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array(
48
                array('onPostDependenciesSolving', PHP_INT_MAX),
49
            ),
50
        );
51
    }
52
53
    public function onPreFileDownload(CPlugin\PreFileDownloadEvent $ev)
54
    {
55
        $scheme = parse_url($ev->getProcessedUrl(), PHP_URL_SCHEME);
56
        if ($scheme === 'http' || $scheme === 'https') {
57
            $rfs = $ev->getRemoteFilesystem();
58
59
            $curlrfs = new CurlRemoteFilesystem(
60
                $this->io,
61
                $this->config,
62
                $rfs->getOptions()
63
            );
64
            $curlrfs->setPluginConfig($this->pluginConfig->get());
65
            $ev->setRemoteFilesystem($curlrfs);
66
        }
67
    }
68
69
    /**
70
     * pre-fetch parallel by curl_multi
71
     */
72
    public function onPostDependenciesSolving(Installer\InstallerEvent $ev)
73
    {
74
        $ops = $ev->getOperations();
75
        $packages = $this->filterPackages($ops);
76
        $pluginConfig = $this->pluginConfig->get();
77
        if (count($packages) >= $pluginConfig['minConnections']) {
78
            $downloader = new ParallelDownloader($this->io, $this->config);
79
            $downloader->download($packages, $pluginConfig);
80
        }
81
    }
82
83
    /**
84
     * @param DependencyResolver\Operation\OperationInterface[]
85
     * @return Package\PackageInterface[]
86
     */
87
    private static function filterPackages(array $operations)
88
    {
89
        $packs = array();
90
        foreach ($operations as $op) {
91
            switch ($op->getJobType()) {
92
                case 'install':
93
                    $packs[] = $op->getPackage();
94
                    break;
95
                case 'update':
96
                    $packs[] = $op->getTargetPackage();
97
                    break;
98
            }
99
        }
100
        return $packs;
101
    }
102
103
    private function setPluginConfig()
104
    {
105
        $config = $this->config->get('prestissimo');
106
        if (!is_array($config)) {
107
            $config = array();
108
        }
109
        return new Config($config);
110
    }
111
}
112