Completed
Pull Request — master (#37)
by Sullivan
02:33
created

VersionsCheckPlugin::satisfiesComposerVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace SLLH\ComposerVersionsCheck;
4
5
use Composer\Composer;
6
use Composer\EventDispatcher\EventSubscriberInterface;
7
use Composer\IO\IOInterface;
8
use Composer\Package\RootPackageInterface;
9
use Composer\Plugin\CommandEvent;
10
use Composer\Plugin\PluginEvents;
11
use Composer\Plugin\PluginInterface;
12
use Composer\Repository\RepositoryManager;
13
use Composer\Script\Event;
14
use Composer\Script\ScriptEvents;
15
16
/**
17
 * @author Sullivan Senechal <[email protected]>
18
 */
19
final class VersionsCheckPlugin implements PluginInterface, EventSubscriberInterface
20
{
21
    const COMPOSER_MIN_VERSION = '1.0.0';
22
23
    /**
24
     * @var Composer
25
     */
26
    private $composer;
27
28
    /**
29
     * @var IOInterface
30
     */
31
    private $io;
32
33
    /**
34
     * @var VersionsCheck
35
     */
36
    private $versionsCheck;
37
38
    /**
39
     * @var bool
40
     */
41
    private $preferLowest;
42
43
    /**
44
     * @var array
45
     */
46
    private $options = array();
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function activate(Composer $composer, IOInterface $io)
52
    {
53
        if (!static::satisfiesComposerVersion()) {
54
            $io->writeError(sprintf(
55
                '<error>Composer v%s is not supported by sllh/composer-versions-check plugin,'
56
                .' please upgrade to v%s or higher.</error>',
57
                Composer::VERSION,
58
                self::COMPOSER_MIN_VERSION
59
            ));
60
        }
61
        if ('@package_version@' === Composer::VERSION) {
62
            $io->write('<warning>You are running an unstable version of composer.'
63
                .' The sllh/composer-versions-check plugin might not works as expected.</warning>');
64
        }
65
66
        $this->composer = $composer;
67
        $this->io = $io;
68
        $this->versionsCheck = new VersionsCheck();
69
        $this->options = $this->resolveOptions();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public static function getSubscribedEvents()
76
    {
77
        // Do not subscribe the plugin if not compatible.
78
        if (!static::satisfiesComposerVersion()) {
79
            return array();
80
        }
81
82
        return array(
83
            PluginEvents::COMMAND => array(
84
                array('command'),
85
            ),
86
            ScriptEvents::POST_UPDATE_CMD => array(
87
                array('postUpdate', -100),
88
            ),
89
        );
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public static function satisfiesComposerVersion()
96
    {
97
        // Can't determine version. Assuming it satisfies.
98
        if ('@package_version@' === Composer::VERSION) {
99
            return true;
100
        }
101
102
        return version_compare(Composer::VERSION, self::COMPOSER_MIN_VERSION, '>=');
103
    }
104
105
    /**
106
     * @param CommandEvent $event
107
     */
108
    public function command(CommandEvent $event)
109
    {
110
        $input = $event->getInput();
111
        $this->preferLowest = $input->hasOption('prefer-lowest') && true === $input->getOption('prefer-lowest');
112
    }
113
114
    /**
115
     * @param Event $event
116
     */
117
    public function postUpdate(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
118
    {
119
        if (true === $this->preferLowest) {
120
            return;
121
        }
122
123
        $this->checkVersions($this->composer->getRepositoryManager(), $this->composer->getPackage());
124
    }
125
126
    /**
127
     * Tries to get plugin options and resolves them.
128
     *
129
     * @return array
130
     */
131
    private function resolveOptions()
132
    {
133
        $pluginConfig = $this->composer->getConfig()
134
            ? $this->composer->getConfig()->get('sllh-composer-versions-check')
135
            : null
136
        ;
137
138
        $options = array(
139
            'show-links' => true,
140
        );
141
142
        if (null === $pluginConfig) {
143
            return $options;
144
        }
145
146
        $options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : true;
147
148
        return $options;
149
    }
150
151
    /**
152
     * @param RepositoryManager    $repositoryManager
153
     * @param RootPackageInterface $rootPackage
154
     */
155
    private function checkVersions(RepositoryManager $repositoryManager, RootPackageInterface $rootPackage)
156
    {
157
        foreach ($repositoryManager->getRepositories() as $repository) {
158
            $this->versionsCheck->checkPackages(
159
                $repository,
160
                $repositoryManager->getLocalRepository(),
161
                $rootPackage
162
            );
163
        }
164
165
        $this->io->write($this->versionsCheck->getOutput($this->options['show-links']), false);
166
    }
167
}
168