Test Failed
Pull Request — master (#3)
by MediaCT
06:30
created

Plugin   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 72
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A activate() 0 2 1
A onNewCodeEvent() 0 21 4
A __construct() 0 5 2
A getSubscribedEvents() 0 5 1
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\CodingStandard\PhpStorm;
8
9
use Composer\Composer;
10
use Composer\EventDispatcher\EventSubscriberInterface;
11
use Composer\IO\IOInterface;
12
use Composer\Plugin\PluginInterface;
13
use Composer\Script\Event;
14
use Composer\Script\ScriptEvents;
15
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcher;
16
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcherInterface;
17
18
class Plugin implements PluginInterface, EventSubscriberInterface
19
{
20
    /**
21
     * @var ConfigPatcherInterface
22
     */
23
    private $patcher;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param ConfigPatcherInterface $patcher
29
     */
30
    public function __construct(ConfigPatcherInterface $patcher = null)
31
    {
32
        $this->patcher = $patcher !== null
33
            ? $patcher
34
            : new ConfigPatcher();
35
    }
36
37
    /**
38
     * Apply plugin modifications to Composer
39
     *
40
     * @param Composer    $composer
41
     * @param IOInterface $inputOutput
42
     *
43
     * @return void
44
     */
45
    public function activate(Composer $composer, IOInterface $inputOutput)
46
    {
47
    }
48
49
    /**
50
     * Get the subscribed events.
51
     *
52
     * @return array
53
     */
54
    public static function getSubscribedEvents()
55
    {
56
        return [
57
            ScriptEvents::POST_INSTALL_CMD => 'onNewCodeEvent',
58
            ScriptEvents::POST_UPDATE_CMD  => 'onNewCodeEvent'
59
        ];
60
    }
61
62
    /**
63
     * On new code.
64
     *
65
     * @param Event $event
66
     *
67
     * @return void
68
     */
69
    public function onNewCodeEvent(Event $event)
70
    {
71
        $vendorDir   = $event->getComposer()->getConfig()->get('vendor-dir');
72
        $projectDir  = dirname($vendorDir);
73
        $phpStormDir = $projectDir . DIRECTORY_SEPARATOR . '.idea';
74
        $filesDir    = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'files';
75
76
        if (is_dir($phpStormDir) && is_dir($filesDir)) {
77
            $this->patcher->patch(
78
                new Environment(
79
                    new Filesystem($phpStormDir),
80
                    new Filesystem($filesDir),
81
                    new Filesystem($projectDir),
82
                    $event->getIO(),
83
                    $event->getComposer()
84
                )
85
            );
86
87
            $output = $event->getIO();
88
            if ($output->isVerbose()) {
89
                $output->write('Patched the PhpStorm config');
90
            }
91
        }
92
    }
93
}
94