ComposerPlugin   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
wmc 5
lcom 0
cbo 4
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A activate() 0 25 4
A getCapabilities() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Commands;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Composer\Composer;
22
use Composer\IO\IOInterface;
23
use Composer\Plugin\Capability\CommandProvider;
24
use Composer\Plugin\Capable;
25
use Composer\Plugin\PluginInterface;
26
use Limoncello\Commands\Traits\CacheFilePathTrait;
27
use function assert;
28
use function array_merge;
29
use function is_array;
30
31
/**
32
 * @package Limoncello\Commands
33
 */
34
class ComposerPlugin implements PluginInterface, Capable
35
{
36
    use CacheFilePathTrait;
37
38 1
    /**
39
     * @inheritdoc
40
     *
41 1
     * @SuppressWarnings(PHPMD.StaticAccess)
42
     */
43
    public function activate(Composer $composer, IOInterface $ioInterface)
44
    {
45
        $builtInCommands = [
46
            new CommandsCommand(),
47
        ];
48
49 1
        // Due to https://github.com/composer/composer/issues/6315 we cannot load
50 1
        // application at this stage.
51 1
        //
52
        // So we create command proxies and when one one of them is called we actually
53 1
        // create application and execute the command.
54 1
        $commands          = [];
55 1
        $commandsCacheFile = $this->getCommandsCacheFilePath($composer);
56 1
        if ($commandsCacheFile !== null && file_exists($commandsCacheFile) === true) {
57 1
            /** @noinspection PhpIncludeInspection */
58
            $cacheData = require $commandsCacheFile;
59
            assert(is_array($cacheData));
60
            foreach ($cacheData as $commandData) {
61 1
                list($name, $description, $help, $arguments, $options, $callable) = $commandData;
62
                $commands[] = new LimoncelloCommand($name, $description, $help, $arguments, $options, $callable);
63
            }
64
        }
65
66
        ComposerCommandProvider::setCommands(array_merge($builtInCommands, $commands));
67 1
    }
68
69
    /**
70 1
     * @inheritdoc
71
     */
72
    public function getCapabilities()
73
    {
74
        return [
75
            CommandProvider::class => ComposerCommandProvider::class,
76
        ];
77
    }
78
}
79