Completed
Pull Request — master (#52)
by Hiraku
02:16
created

Plugin   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 84.21%

Importance

Changes 13
Bugs 2 Features 3
Metric Value
wmc 19
c 13
b 2
f 3
lcom 1
cbo 7
dl 0
loc 143
ccs 48
cts 57
cp 0.8421
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A disable() 0 4 1
A isDisabled() 0 4 1
A activate() 0 19 3
A getSubscribedEvents() 0 11 1
A onPreFileDownload() 0 18 4
A onPostDependenciesSolving() 0 13 3
A filterPackages() 0 15 4
A setPluginConfig() 0 8 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
        }
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
                array('onPreFileDownload', 0),
78 1
            ),
79 1
            Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array(
80 1
                array('onPostDependenciesSolving', PHP_INT_MAX),
81
            ),
82
        );
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
            );
99 1
            $curlrfs->setPluginConfig($this->pluginConfig->get());
100 1
            $ev->setRemoteFilesystem($curlrfs);
101
        }
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
        }
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
        }
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