Completed
Push — master ( 419b5f...f7351b )
by Sullivan
02:34
created

VersionsCheckPlugin::resolveOptions()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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