InstallerPlugin::onNewCodeEvent()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 23
ccs 17
cts 17
cp 1
crap 3
rs 9.7333
1
<?php
2
3
/**
4
 * Copyright MediaCT. All rights reserved.
5
 * https://www.mediact.nl
6
 * @deprecated
7
 */
8
9
declare(strict_types=1);
10
11
namespace Mediact\CodingStandard\PhpStorm;
12
13
use Composer\Composer;
14
use Composer\EventDispatcher\EventSubscriberInterface;
15
use Composer\IO\IOInterface;
16
use Composer\Plugin\PluginInterface;
17
use Composer\Script\Event;
18
use Composer\Script\ScriptEvents;
19
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcher;
20
use Mediact\CodingStandard\PhpStorm\Patcher\ConfigPatcherInterface;
21
22
class InstallerPlugin implements PluginInterface, EventSubscriberInterface
23
{
24
    /**
25
     * @var ConfigPatcherInterface
26
     */
27
    private $patcher;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param ConfigPatcherInterface $patcher
33
     */
34 1
    public function __construct(ConfigPatcherInterface $patcher = null)
35
    {
36 1
        $this->patcher = $patcher !== null
37 1
            ? $patcher
38
            : new ConfigPatcher();
39 1
    }
40
41
    /**
42
     * Apply plugin modifications to Composer
43
     *
44
     * @param Composer    $composer
45
     * @param IOInterface $inputOutput
46
     *
47
     * @return void
48
     */
49
    public function activate(Composer $composer, IOInterface $inputOutput): void
50
    {
51
    }
52
53
    /**
54
     * Remove any hooks from Composer.
55
     *
56
     * @param Composer    $composer
57
     * @param IOInterface $io
58
     *
59
     * @return void
60
     */
61
    public function deactivate(Composer $composer, IOInterface $io): void
62
    {
63
    }
64
65
    /**
66
     * Prepare the plugin to be uninstalled
67
     *
68
     * @param Composer    $composer
69
     * @param IOInterface $io
70
     *
71
     * @return void
72
     */
73
    public function uninstall(Composer $composer, IOInterface $io): void
74
    {
75
    }
76
77
    /**
78
     * Get the subscribed events.
79
     *
80
     * @return array
81
     */
82 1
    public static function getSubscribedEvents(): array
83
    {
84
        return [
85 1
            ScriptEvents::POST_INSTALL_CMD => 'onNewCodeEvent',
86 1
            ScriptEvents::POST_UPDATE_CMD  => 'onNewCodeEvent'
87
        ];
88
    }
89
90
    /**
91
     * On new code.
92
     *
93
     * @param Event $event
94
     *
95
     * @return void
96
     */
97 1
    public function onNewCodeEvent(Event $event): void
98
    {
99 1
        $vendorDir   = $event->getComposer()->getConfig()->get('vendor-dir');
100 1
        $projectDir  = dirname($vendorDir);
101 1
        $phpStormDir = $projectDir . DIRECTORY_SEPARATOR . '.idea';
102 1
        $filesDir    = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'files';
103
104 1
        $phpStormDefaultPath = $this->getPhpStormDefaultPath();
105
106 1
        if (is_dir($phpStormDir) && is_dir($filesDir)) {
107 1
            $this->patcher->patch(
108 1
                new Environment(
109 1
                    new Filesystem($phpStormDir),
110 1
                    new Filesystem($phpStormDefaultPath),
111 1
                    new Filesystem($filesDir),
112 1
                    new Filesystem($projectDir),
113 1
                    $event->getIO(),
114 1
                    $event->getComposer()
115
                )
116
            );
117
118 1
            $output = $event->getIO();
119 1
            $output->write('Patched the PhpStorm config');
120
        }
121 1
    }
122
123
    /**
124
     * Get the latest version of clients phpstorm directory
125
     *
126
     * @return string
127
     */
128
    public function getPhpStormDefaultPath(): string
129
    {
130
        $phpStormDefaultPath = '';
131
132
        if (isset($_SERVER['HOME'])) {
133
            $home = $_SERVER['HOME'];
134
        } else {
135
            $home = getenv("HOME");
136
        }
137
138
        $phpStormDefaultPaths    = array_reverse(glob("$home/.[pP]hp[sS]torm201*/config/"));
139
        $phpStormNewDefaultPaths = array_reverse(glob("$home/.config/JetBrains/[pP]hp[sS]torm202*/"));
140
141
        if (! empty($phpStormDefaultPaths)) {
142
            $phpStormDefaultPath = reset($phpStormDefaultPaths);
143
        } elseif (! empty($phpStormNewDefaultPaths)) {
144
            $phpStormDefaultPath = reset($phpStormNewDefaultPaths);
145
        }
146
147
        return $phpStormDefaultPath;
148
    }
149
}
150