Completed
Push — master ( d6b303...8271f2 )
by Sullivan
02:14
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
16
/**
17
 * @author Sullivan Senechal <[email protected]>
18
 */
19
final class VersionsCheckPlugin implements PluginInterface, EventSubscriberInterface
20
{
21
    /**
22
     * @var Composer
23
     */
24
    private $composer;
25
26
    /**
27
     * @var IOInterface
28
     */
29
    private $io;
30
31
    /**
32
     * @var VersionsCheck
33
     */
34
    private $versionsCheck;
35
36
    /**
37
     * @var bool
38
     */
39
    private $preferLowest;
40
41
    /**
42
     * @var array
43
     */
44
    private $options = array();
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function activate(Composer $composer, IOInterface $io)
50
    {
51
        $this->composer = $composer;
52
        $this->io = $io;
53
        $this->versionsCheck = new VersionsCheck();
54
        $this->options = $this->resolveOptions();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public static function getSubscribedEvents()
61
    {
62
        return array(
63
            PluginEvents::COMMAND => array(
64
                array('command'),
65
            ),
66
            ScriptEvents::POST_UPDATE_CMD => array(
67
                array('postUpdate', -100),
68
            ),
69
        );
70
    }
71
72
    /**
73
     * @param CommandEvent $event
74
     */
75
    public function command(CommandEvent $event)
76
    {
77
        $input = $event->getInput();
78
        $this->preferLowest = $input->hasOption('prefer-lowest') && true === $input->getOption('prefer-lowest');
79
    }
80
81
    /**
82
     * @param Event $event
83
     */
84
    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...
85
    {
86
        if (true === $this->preferLowest) {
87
            return;
88
        }
89
90
        $this->checkVersions($this->composer->getRepositoryManager(), $this->composer->getPackage());
91
    }
92
93
    /**
94
     * Tries to get plugin options and resolves them.
95
     *
96
     * @return array
97
     */
98
    private function resolveOptions()
99
    {
100
        $pluginConfig = $this->composer->getConfig()
101
            ? $this->composer->getConfig()->get('sllh-composer-versions-check')
102
            : null
103
        ;
104
105
        $options = array(
106
            'show-links' => true,
107
        );
108
109
        if (null === $pluginConfig) {
110
            return $options;
111
        }
112
113
        $options['show-links'] = isset($pluginConfig['show-links']) ? (bool) $pluginConfig['show-links'] : true;
114
115
        return $options;
116
    }
117
118
    /**
119
     * @param RepositoryManager    $repositoryManager
120
     * @param RootPackageInterface $rootPackage
121
     */
122
    private function checkVersions(RepositoryManager $repositoryManager, RootPackageInterface $rootPackage)
123
    {
124
        foreach ($repositoryManager->getRepositories() as $repository) {
125
            $this->versionsCheck->checkPackages(
126
                $repository,
127
                $repositoryManager->getLocalRepository(),
128
                $rootPackage
129
            );
130
        }
131
132
        $this->io->write($this->versionsCheck->getOutput($this->options['show-links']), false);
133
    }
134
}
135