Completed
Push — master ( df10ee...95deef )
by Hiraku
8s
created

Plugin::prefetchComposerRepositories()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.6393

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 9
cts 14
cp 0.6429
rs 8.8571
cc 6
eloc 11
nc 5
nop 0
crap 7.6393
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\Script;
15
use Composer\Installer;
16
use Composer\DependencyResolver;
17
18
class Plugin implements
19
    CPlugin\PluginInterface,
20
    EventDispatcher\EventSubscriberInterface
21
{
22
    /** @var IO\IOInterface */
23
    private $io;
24
25
    /** @var Composer\Config */
26
    private $config;
27
28
    /** @var array */
29
    private $package;
30
    private $cached = false;
31
32
    /** @var boolean */
33
    private $disabled = false;
34
35
    private static $pluginClasses = array(
36
        'BaseRequest',
37
        'ConfigFacade',
38
        'CopyRequest',
39
        'CurlMulti',
40
        'CurlRemoteFilesystem',
41
        'FetchException',
42
        'FetchRequest',
43
        'FileDownloaderDummy',
44
        'ParallelizedComposerRepository',
45
        'Plugin',
46
        'Prefetcher',
47
        'Share',
48
    );
49
50 3
    public function activate(Composer $composer, IO\IOInterface $io)
0 ignored issues
show
Coding Style introduced by
activate uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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->io = $io;
65 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...
66 3
        $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 3
        foreach ($GLOBALS['argv'] as $arg) {
69
            switch ($arg) {
70 3
                case 'create-project':
71 3
                case 'update':
72 3
                case 'outdated':
73 3
                case 'require':
74
                    $this->prefetchComposerRepositories();
75
                    break 2;
76 3
                case 'install':
77
                    if (file_exists('composer.json') && !file_exists('composer.lock')) {
78
                        $this->prefetchComposerRepositories();
79
                    }
80
                    break 2;
81
            }
82 3
        }
83 3
    }
84
85 1
    public static function getSubscribedEvents()
86
    {
87
        return array(
88 1
            CPlugin\PluginEvents::PRE_FILE_DOWNLOAD => 'onPreFileDownload',
89
//            Script\ScriptEvents::POST_ROOT_PACKAGE_INSTALL => 'prefetchComposerRepositories',
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
//            Installer\InstallerEvents::PRE_DEPENDENCIES_SOLVING => 'prefetchComposerRepositories',
91 1
            Installer\InstallerEvents::POST_DEPENDENCIES_SOLVING => array(
92 1
                array('onPostDependenciesSolving', PHP_INT_MAX),
93 1
            ),
94 1
        );
95
    }
96
97
    /**
98
     * Keep-Alived file downloader
99
     */
100
    public function onPreFileDownload(CPlugin\PreFileDownloadEvent $ev)
101
    {
102
        if ($this->disabled) {
103
            return;
104
        }
105
        $rfs = $ev->getRemoteFilesystem();
106
        $curlrfs = new CurlRemoteFilesystem(
107
            $this->io,
108
            $this->config,
109
            $rfs->getOptions()
110
        );
111
        $ev->setRemoteFilesystem($curlrfs);
112
    }
113
114 1
    public function prefetchComposerRepositories()
115
    {
116 1
        if ($this->disabled) {
117 1
            return;
118
        }
119 1
        if ($this->cached) {
120
            return;
121
        }
122 1
        $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...
123 1
        foreach ($repos as $label => $repo) {
124
            if (isset($repo['type']) && $repo['type'] === 'composer') {
125
                $r = new ParallelizedComposerRepository($repo, $this->io, $this->config);
126
                $r->prefetch();
127
            }
128 1
        }
129 1
        $this->cached = true;
130 1
    }
131
132
    /**
133
     * pre-fetch parallel by curl_multi
134
     */
135 1
    public function onPostDependenciesSolving(Installer\InstallerEvent $ev)
136
    {
137 1
        if ($this->disabled) {
138 1
            return;
139
        }
140 1
        $prefetcher = new Prefetcher;
141 1
        $prefetcher->fetchAllFromOperations(
142 1
            $this->io,
143 1
            $this->config,
144 1
            $ev->getOperations()
145 1
        );
146 1
    }
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