ComposerInstall::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the skeleton package.
7
 *
8
 * (c) Gennady Knyazkin <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Gennadyx\Skeleton\Action;
15
16
use Gennadyx\Skeleton\Action\Traits\EventAwareTrait;
17
use Gennadyx\Skeleton\EventAwareInterface;
18
use Symfony\Component\Process\PhpExecutableFinder;
19
use Symfony\Component\Process\Process;
20
21
/**
22
 * Class ComposerInstall
23
 *
24
 * @author Gennady Knyazkin <[email protected]>
25
 */
26
final class ComposerInstall implements ActionInterface, EventAwareInterface
27
{
28
    use EventAwareTrait;
29
30
    /**
31
     * Execute action
32
     *
33
     * @return void
34
     * @throws \Exception
35
     */
36 2
    public function execute()
37
    {
38 2
        $phpBin  = $this->findPhp();
39 2
        $command = sprintf('%s %s update', $phpBin, realpath($_SERVER['argv'][0]));
40 2
        $this->run($command);
41 2
    }
42
43
    /**
44
     * @return string
45
     * @throws \RuntimeException
46
     */
47 2
    private function findPhp(): string
48
    {
49 2
        $phpBin = (new PhpExecutableFinder())->find();
50
51 2
        if (false === $phpBin) {
52
            throw new \RuntimeException('Failed to locate PHP binary to execute "composer install"');
53
        }
54
55 2
        return $phpBin;
56
    }
57
58
    /**
59
     * @param string $command
60
     *
61
     * @throws \Exception
62
     */
63 2
    private function run(string $command)
64
    {
65 2
        $cmd = new Process($command);
66 2
        $cmd->run();
67 2
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 4
    public function getPriority(): int
73
    {
74 4
        return PHP_INT_MAX;
75
    }
76
}
77