Completed
Push — stable ( 386c57...2cb6a4 )
by Nuno
02:05
created

Composer::run()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 14
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 8
nop 2
crap 42
1
<?php
2
3
/**
4
 * This file is part of Laravel Zero.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace LaravelZero\Framework\Providers\Composer;
13
14
use Throwable;
15
use Symfony\Component\Process\Process;
16
use Illuminate\Contracts\Foundation\Application;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use LaravelZero\Framework\Contracts\Providers\Composer as ComposerContract;
19
20
/**
21
 * This is the Laravel Zero Framework Composer implementation.
22
 */
23
class Composer implements ComposerContract
24
{
25
    /**
26
     * Holds an instance of the app.
27
     *
28
     * @var \Illuminate\Contracts\Foundation\Application
29
     */
30
    protected $app;
31
32
    /**
33
     * Composer constructor.
34
     *
35
     * @param \Illuminate\Contracts\Foundation\Application $app
36
     *
37
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
38
     */
39
    public function __construct(Application $app)
40
    {
41
        $this->app = $app;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function install(array $options = []): bool
48
    {
49
        $cmd = 'composer install';
50
51
        collect($options)->each(function ($option) use (&$cmd) {
52
            $cmd .= " $option";
53
        });
54
55
        return $this->run($cmd, $this->app->basePath());
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function require(string $package): bool
62
    {
63
        return $this->run("composer require $package", $this->app->basePath());
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function createProject(string $package, string $name, array $options): bool
70
    {
71
        $cmd = "composer create-project $package $name";
72
73
        collect($options)->each(function ($option) use (&$cmd) {
74
            $cmd .= " $option";
75
        });
76
77
        return $this->run($cmd);
78
    }
79
80
    /**
81
     * Runs the provided command.
82
     *
83
     * @param  string $cmd
84
     *
85
     * @return bool
86
     */
87
    private function run(string $cmd, string $cwd = null): bool
88
    {
89
        $process = new Process($cmd, $cwd);
90
91
        if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
92
            $process->setTty(true);
93
        }
94
95
        try {
96
            $output = $this->app->make(OutputInterface::class);
97
        } catch (Throwable $e) {
98
            $output = null;
99
        }
100
101
        if ($output) {
102
            $output->write("\n");
103
            $process->run(function ($type, $line) use ($output) {
104
                $output->write($line);
105
            });
106
        } else {
107
            $process->run();
108
        }
109
110
        return $process->isSuccessful();
111
    }
112
}
113