Completed
Pull Request — master (#101)
by Hiraku
05:27
created

Plugin::onPreDependenciesSolving()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 8.8571
ccs 0
cts 0
cp 0
cc 5
eloc 10
nc 4
nop 1
crap 30
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 array */
28
    private $package;
29
    private $cached = false;
30
31
    /** @var boolean */
32
    private $disabled = false;
33
34
    private static $pluginClasses = array(
35
        'BaseRequest',
36
        'ConfigFacade',
37
        'CopyRequest',
38
        'CurlMulti',
39
        'CurlRemoteFilesystem',
40 2
        'FetchException',
41
        'FetchRequest',
42
        'FileDownloaderDummy',
43
        'ParallelizedComposerRepository',
44
        'Plugin',
45
        'Prefetcher',
46
        'Share',
47
    );
48
49
    public function activate(Composer $composer, IO\IOInterface $io)
50 2
    {
51 2
        // @codeCoverageIgnoreStart
52
        // guard for self-update problem
53
        if (__CLASS__ !== 'Hirak\Prestissimo\Plugin') {
54 2
            return $this->disable();
55 2
        }
56 2
        // @codeCoverageIgnoreEnd
57
58 1
        // load all classes
59
        foreach (self::$pluginClasses as $class) {
60
            class_exists(__NAMESPACE__ . '\\' . $class);
61 1
        }
62 1
63
        $this->io = $io;
64
        $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
66
        $this->package = $composer->getPackage();
0 ignored issues
show
Documentation Bug introduced by
It seems like $composer->getPackage() of type object<Composer\Package\RootPackageInterface> is incompatible with the declared type array of property $package.

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...
67
    }
68
69
    public static function getSubscribedEvents()
70 1
    {
71
        return array(
72 1
            CPlugin\PluginEvents::PRE_FILE_DOWNLOAD => 'onPreFileDownload',
73 1
            Installer\InstallerEvents::PRE_DEPENDENCIES_SOLVING => 'onPreDependenciesSolving',
74
            Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array(
75 1
                array('onPostDependenciesSolving', PHP_INT_MAX),
76 1
            ),
77 1
        );
78 1
    }
79 1
80
    /**
81 1
     * Keep-Alived file downloader
82
     */
83 1
    public function onPreFileDownload(CPlugin\PreFileDownloadEvent $ev)
84
    {
85 1
        if ($this->disabled) {
86 1
            return;
87
        }
88 1
        $rfs = $ev->getRemoteFilesystem();
89
        $curlrfs = new CurlRemoteFilesystem(
90 1
            $this->io,
91
            $this->config,
92
            $rfs->getOptions()
93
        );
94
        $ev->setRemoteFilesystem($curlrfs);
95
    }
96
97
    public function onPreDependenciesSolving(Installer\InstallerEvent $ev)
0 ignored issues
show
Unused Code introduced by
The parameter $ev is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        if ($this->disabled) {
100
            return;
101
        }
102
        if ($this->cached) {
103
            return;
104
        }
105
        $repos = $this->package->getRepositories();
0 ignored issues
show
Bug introduced by
The method getRepositories cannot be called on $this->package (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
106
        if (isset($repos['packagist']['type']) && $repos['packagist']['type'] === 'composer') {
107
            $repo = new ParallelizedComposerRepository($repos['packagist'], $this->io, $this->config);
108
            $repo->prefetch();
109
            $this->cached = true;
110
        }
111
    }
112
113
    /**
114
     * pre-fetch parallel by curl_multi
115
     */
116
    public function onPostDependenciesSolving(Installer\InstallerEvent $ev)
117
    {
118
        if ($this->disabled) {
119
            return;
120
        }
121
        $prefetcher = new Prefetcher;
122
        $prefetcher->fetchAllFromOperations(
123
            $this->io,
124
            $this->config,
125
            $ev->getOperations()
126
        );
127
    }
128
129
    public function disable()
130
    {
131
        $this->disabled = true;
132
    }
133
134
    public function isDisabled()
135
    {
136
        return $this->disabled;
137
    }
138
}
139