Commander::composerInstalled()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace UnikCodes\Console;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Command\Command;
8
9
class Commander extends Command
10
{
11
    /**
12
     * Filesystem.
13
     *
14
     * @var \Illuminate\Filesystem\Filesystem
15
     */
16
    protected $filesystem;
17
18
    public function __construct($name = null)
19
    {
20
        if (! $this->gitInstalled()) {
21
            throw new \Exception('Git are not installed in your machine!');
22
        }
23
24
        if (! $this->composerInstalled()) {
25
            throw new \Exception('Composer are not installed in your machine!');
26
        }
27
28
        parent::__construct($name);
29
        $this->filesystem = new Filesystem();
30
    }
31
32
    /**
33
     * Initiliase git repository.
34
     *
35
     * @return void
36
     */
37
    public function gitInit()
38
    {
39
        if ($this->gitInstalled()) {
40
            exec('git init && git add . && git commit -m "Initial Commits"');
41
        }
42
    }
43
44
    /**
45
     * Commit composer.lock on update dependencies.
46
     *
47
     * @return void
48
     */
49
    public function gitCommitUpdateDependecies()
50
    {
51
        if ($this->gitInstalled()) {
52
            exec('git add composer.lock && git commit -m "Upadate dependencies"');
53
        }
54
    }
55
56
    /**
57
     * Check if git is installed.
58
     */
59
    public function gitInstalled(): bool
60
    {
61
        return ! empty(exec('which git'));
62
    }
63
64
    /**
65
     * Check if git is installed.
66
     */
67
    public function composerInstalled(): bool
68
    {
69
        return ! empty(exec('which composer')) || file_exists(getcwd() . '/composer.phar');
70
    }
71
72
    /**
73
     * Get the composer command for the environment.
74
     *
75
     * @return string
76
     */
77
    public function findComposer()
78
    {
79
        if (file_exists(getcwd() . '/composer.phar')) {
80
            return '"' . PHP_BINARY . '" composer.phar';
81
        }
82
83
        return 'composer';
84
    }
85
86
    /**
87
     * Get Composer Configuration.
88
     *
89
     * @param string $path
90
     */
91
    public function getComposerConfig($path)
92
    {
93
        $composerJson = file_get_contents($path . DIRECTORY_SEPARATOR . 'composer.json');
94
95
        return json_decode($composerJson);
96
    }
97
98
    /**
99
     * Get Qualified Namespace from Path Given.
100
     *
101
     * @param string $path
102
     *
103
     * @return string
104
     */
105
    public function getQualifiedNamespaceFromPath($path)
106
    {
107
        $json               = $this->getComposerConfig($path);
108
        $qualifiedNamespace = key((array) $json->autoload->{'psr-4'});
109
110
        return $qualifiedNamespace;
111
    }
112
113
    /**
114
     * Install Package Dependencies.
115
     */
116
    public function composerInstall()
117
    {
118
        if ('testing' != env('APP_ENV')) {
0 ignored issues
show
introduced by
The condition 'testing' != env('APP_ENV') is always true.
Loading history...
119
            exec($this->findComposer() . ' install --no-progress --no-suggest');
120
        }
121
    }
122
123
    /**
124
     * UPdate Package Dependencies.
125
     */
126
    public function composerUpdate()
127
    {
128
        if ('testing' != env('APP_ENV')) {
0 ignored issues
show
introduced by
The condition 'testing' != env('APP_ENV') is always true.
Loading history...
129
            exec($this->findComposer() . ' update --no-progress --no-suggest');
130
        }
131
    }
132
133
    /**
134
     * Link local package to target project.
135
     *
136
     * @see http://calebporzio.com/bash-alias-composer-link-use-local-folders-as-composer-dependancies/
137
     *
138
     * @param string $name
139
     * @param string $pathOrUrl
140
     */
141
    public function composerLink($name, $pathOrUrl)
142
    {
143
        if ('testing' != env('APP_ENV')) {
0 ignored issues
show
introduced by
The condition 'testing' != env('APP_ENV') is always true.
Loading history...
144
            exec($this->findComposer() . ' config repositories.' . $name . ' \'{"type": "path", "url": "' . $pathOrUrl . '"}\' --file composer.json');
145
146
            return true;
147
        }
148
149
        return false;
150
    }
151
152
    public function cleanupName($value)
153
    {
154
        return Str::slug($value, '-');
155
    }
156
157
    public function getQualifiedClassName($package)
158
    {
159
        return Str::studly($package);
160
    }
161
162
    public function getPackageName($package)
163
    {
164
        return strtolower($this->cleanupName($package));
165
    }
166
167
    public function getVendorName($vendor)
168
    {
169
        return strtolower($this->cleanupName($vendor));
170
    }
171
172
    public function getQualifiedNamespace($vendor, $package)
173
    {
174
        return Str::studly($vendor) . '\\' . Str::studly($package);
175
    }
176
}
177