Passed
Push — master ( a1972e...8e8c77 )
by Guillaume
08:02
created

DocsetNewer::makeUserDocsetsDirectoryIfNeeded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 14
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Godbout\DashDocsetBuilder\Services;
4
5
use Illuminate\Support\Facades\File;
6
use Illuminate\Support\Str;
7
8
final class DocsetNewer
9
{
10 24
    public function new(string $newDocset = null)
11
    {
12 24
        $userDocsetsDirectory = $this->makeUserDocsetsDirectoryIfNeeded();
13
14 24
        if (! $newDocset) {
15 12
            return $this->generateRickAstleyDocsetClass($userDocsetsDirectory);
16
        }
17
18 18
        return $this->generateUserArgumentDocsetClass($userDocsetsDirectory, $newDocset);
19
    }
20
21 24
    protected function makeUserDocsetsDirectoryIfNeeded()
22
    {
23 24
        $userDocsetsDirectory = app_path() . '/../../../../app/Docsets';
24
25
        /**
26
         * Dirty shit to be able to run tests on this repo
27
         */
28 24
        if (! Str::contains($userDocsetsDirectory, '/vendor/godbout/dash-docset-builder/')) {
29 24
            $userDocsetsDirectory = Str::replaceFirst('/../../../../app', '', $userDocsetsDirectory);
30
        }
31
32 24
        File::makeDirectory($userDocsetsDirectory, 0755, true, true);
33
34 24
        return $userDocsetsDirectory;
35
    }
36
37 12
    protected function generateRickAstleyDocsetClass(string $userDocsetsDirectory)
38
    {
39 12
        return File::copy(
40 12
            app_path() . '/Services/stubs/RickAstley.stub',
41 12
            $userDocsetsDirectory . '/RickAstley.php'
42
        );
43
    }
44
45 18
    protected function generateUserArgumentDocsetClass(string $userDocsetsDirectory, string $argument)
46
    {
47 18
        $docsetStubContent = File::get(app_path() . '/Services/stubs/NewDocset.stub');
48
49 18
        $docsetStubContent = strtr($docsetStubContent, [
50 18
            '{{ class }}' => Str::studly($argument),
51 18
            '{{ code }}' => Str::kebab($argument),
52 18
            '{{ name }}' => str_replace('-', ' ', Str::title($argument)),
53 18
            '{{ url }}' => Str::lower($argument) . '.com',
54
        ]);
55
56 18
        return File::put(
57 18
            $userDocsetsDirectory . '/' . Str::studly($argument) . '.php',
58 6
            $docsetStubContent
59
        );
60
    }
61
}
62