Completed
Pull Request — master (#19)
by Sullivan
09:21 queued 05:31
created

VersionsCheckPlugin::resolveOptions()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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