Completed
Pull Request — master (#37)
by Sullivan
02:25
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->writeError('<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
        if (!static::satisfiesComposerVersion()) {
78
            return array();
79
        }
80
81
        return array(
82
            PluginEvents::COMMAND => array(
83
                array('command'),
84
            ),
85
            ScriptEvents::POST_UPDATE_CMD => array(
86
                array('postUpdate', -100),
87
            ),
88
        );
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public static function satisfiesComposerVersion()
95
    {
96
        // Can't determine version. Assuming it satisfies.
97
        if ('@package_version@' === Composer::VERSION) {
98
            return true;
99
        }
100
101
        return version_compare(Composer::VERSION, self::COMPOSER_MIN_VERSION, '>=');
102
    }
103
104
    /**
105
     * @param CommandEvent $event
106
     */
107
    public function command(CommandEvent $event)
108
    {
109
        $input = $event->getInput();
110
        $this->preferLowest = $input->hasOption('prefer-lowest') && true === $input->getOption('prefer-lowest');
111
    }
112
113
    /**
114
     * @param Event $event
115
     */
116
    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...
117
    {
118
        if (true === $this->preferLowest) {
119
            return;
120
        }
121
122
        $this->checkVersions($this->composer->getRepositoryManager(), $this->composer->getPackage());
123
    }
124
125
    /**
126
     * Tries to get plugin options and resolves them.
127
     *
128
     * @return array
129
     */
130
    private function resolveOptions()
131
    {
132
        $pluginConfig = $this->composer->getConfig()
133
            ? $this->composer->getConfig()->get('sllh-composer-versions-check')
134
            : null
135
        ;
136
137
        $options = array(
138
            'show-links' => true,
139
        );
140
141
        if (null === $pluginConfig) {
142
            return $options;
143
        }
144
145
        $options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : true;
146
147
        return $options;
148
    }
149
150
    /**
151
     * @param RepositoryManager    $repositoryManager
152
     * @param RootPackageInterface $rootPackage
153
     */
154
    private function checkVersions(RepositoryManager $repositoryManager, RootPackageInterface $rootPackage)
155
    {
156
        foreach ($repositoryManager->getRepositories() as $repository) {
157
            $this->versionsCheck->checkPackages(
158
                $repository,
159
                $repositoryManager->getLocalRepository(),
160
                $rootPackage
161
            );
162
        }
163
164
        $this->io->write($this->versionsCheck->getOutput($this->options['show-links']), false);
165
    }
166
}
167