Failed Conditions
Push — master ( 7d7406...164ad0 )
by Bernhard
06:03
created

PuliPlugin   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 37
Bugs 14 Features 5
Metric Value
wmc 10
c 37
b 14
f 5
lcom 1
cbo 4
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 8 1
A activate() 0 4 1
C listen() 0 24 7
A setPluginImpl() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the puli/composer-plugin package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\ComposerPlugin;
13
14
use Composer\Composer;
15
use Composer\EventDispatcher\EventSubscriberInterface;
16
use Composer\IO\IOInterface;
17
use Composer\Plugin\PluginInterface;
18
use Composer\Script\Event;
19
use Composer\Script\ScriptEvents;
20
21
/**
22
 * A Puli plugin for Composer.
23
 *
24
 * The plugin updates the Puli package repository based on the Composer
25
 * packages whenever `composer install` or `composer update` is executed.
26
 *
27
 * @since  1.0
28
 *
29
 * @author Bernhard Schussek <[email protected]>
30
 */
31
class PuliPlugin implements PluginInterface, EventSubscriberInterface
32
{
33
    /**
34
     * @var PuliPluginImpl
35
     */
36
    private $impl;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function getSubscribedEvents()
42
    {
43
        return array(
44
            ScriptEvents::POST_INSTALL_CMD => 'listen',
45
            ScriptEvents::POST_UPDATE_CMD => 'listen',
46
            ScriptEvents::POST_AUTOLOAD_DUMP => 'listen',
47
        );
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function activate(Composer $composer, IOInterface $io)
54
    {
55
        $composer->getEventDispatcher()->addSubscriber($this);
56
    }
57
58
    /**
59
     * Listens to Composer events.
60
     *
61
     * This method is very minimalist on purpose. We want to load the actual
62
     * implementation only after updating the Composer packages so that we get
63
     * the updated version (if available).
64
     *
65
     * @param Event $event The Composer event.
66
     */
67
    public function listen(Event $event)
68
    {
69
        // Plugin has been uninstalled
70
        if (!file_exists(__FILE__) || !file_exists(__DIR__.'/PuliPluginImpl.php')) {
71
            return;
72
        }
73
74
        // Load the implementation only after updating Composer so that we get
75
        // the new version of the plugin when a new one was installed
76
        if (null === $this->impl) {
77
            $this->impl = new PuliPluginImpl($event);
78
        }
79
80
        switch ($event->getName()) {
81
            case ScriptEvents::POST_AUTOLOAD_DUMP:
82
                $this->impl->postAutoloadDump();
83
                break;
84
85
            case ScriptEvents::POST_INSTALL_CMD:
86
            case ScriptEvents::POST_UPDATE_CMD:
87
                $this->impl->postInstall();
88
                break;
89
        }
90
    }
91
92
    public function setPluginImpl(PuliPluginImpl $impl)
93
    {
94
        $this->impl = $impl;
95
    }
96
}
97