Completed
Pull Request — master (#51)
by Hiraku
02:26
created

Plugin   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 16
Bugs 2 Features 3
Metric Value
wmc 19
c 16
b 2
f 3
lcom 1
cbo 7
dl 0
loc 141
ccs 65
cts 65
cp 1
rs 10

8 Methods

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