ScriptHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 9
Bugs 2 Features 0
Metric Value
wmc 7
c 9
b 2
f 0
lcom 1
cbo 5
dl 0
loc 76
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A npmInstall() 0 11 2
A webpack() 0 8 1
A executeCommand() 0 14 2
A getOptions() 0 12 2
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Kreta\Distribution\Composer;
14
15
use Composer\Script\Event;
16
use Symfony\Component\Process\Process;
17
18
/**
19
 * ScriptHandler class that contains static methods
20
 * to use as Composer scripts when the Kreta
21
 * Standard distribution is going to be installed.
22
 *
23
 * @author Beñat Espiña <[email protected]>
24
 */
25
final class ScriptHandler
26
{
27
    const WEB_BUNDLE_PATH = 'vendor/kreta/kreta/src/Kreta/Bundle/WebBundle';
28
    const BIN_NPM = '../../../../../../../bin/npm';
29
30
    /**
31
     * Executes the npm install command in the web bundle directory.
32
     *
33
     * @param Event $event The command event
34
     */
35
    public static function npmInstall(Event $event)
36
    {
37
        echo "Installing the NPM dependencies (this operation can take a few minutes)\n";
38
39
        $options = self::getOptions($event);
40
        $symlink = $options['npm-bin-links'] ? '' : '--no-bin-links';
41
42
        static::executeCommand(
0 ignored issues
show
Comprehensibility introduced by
Since Kreta\Distribution\Composer\ScriptHandler is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
43
            sprintf('cd %s && %s install %s', self::WEB_BUNDLE_PATH, self::BIN_NPM, $symlink)
44
        );
45
    }
46
47
    /**
48
     * Executes the webpack command as NPM script in the web bundle directory.
49
     */
50
    public static function webpack()
51
    {
52
        echo "Building the assets\n";
53
54
        static::executeCommand(
0 ignored issues
show
Comprehensibility introduced by
Since Kreta\Distribution\Composer\ScriptHandler is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
55
            sprintf('cd %s && %s run prod:deploy', self::WEB_BUNDLE_PATH, self::BIN_NPM)
56
        );
57
    }
58
59
    /**
60
     * Private method that executes all the logic about
61
     * command with Symfony process component.
62
     *
63
     * @param string $cmd     The command itself
64
     * @param int    $timeout The timeout
65
     */
66
    private static function executeCommand($cmd, $timeout = 600)
67
    {
68
        $process = new Process($cmd, null, null, null, $timeout);
69
        $process->run();
70
        if (!$process->isSuccessful()) {
71
            throw new \RuntimeException(
72
                sprintf(
73
                    "An error occurred when executing the %s command.\n %s",
74
                    escapeshellarg($cmd),
75
                    $process->getErrorOutput()
76
                )
77
            );
78
        }
79
    }
80
81
    /**
82
     * Private method that manages the options of commands.
83
     *
84
     * @param Event $event The command event
85
     *
86
     * @return array
87
     */
88
    private static function getOptions(Event $event)
89
    {
90
        $extras = $event->getComposer()->getPackage()->getExtra();
91
        $options = isset($extras['kreta-distribution']) ? $extras['kreta-distribution'] : [];
92
93
        $options = array_merge([
94
            'npm-bin-links'   => true,
95
            'process-timeout' => $event->getComposer()->getConfig()->get('process-timeout'),
96
        ], $options);
97
98
        return $options;
99
    }
100
}
101