Passed
Push — stage ( e57dee...a7e110 )
by Jon
07:08
created

Git::getWrapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
/** Created by PhpStorm,  User: jonphipps,  Date: 2018-02-15,  Time: 4:19 PM */
4
5
namespace App\Services\Publish;
6
7
use App\Jobs\GenerateRdf;
8
use App\Models\Project;
9
use GitWrapper\GitException;
10
use GitWrapper\GitWrapper;
11
use Illuminate\Support\Facades\Storage;
12
13
class Git
14
{
15
    public static function getProjectPath(int $projectId): string
16
    {
17
        return GenerateRdf::PROJECT_ROOT . "/{$projectId}/";
18
    }
19
20
    public static function getProjectRepo(Project $project): ?string
21
    {
22
        $repo = $project->repo;
23
24
        return $repo ? "https://github.com/{$repo}.git" : null;
25
    }
26
27
    /**
28
     * @param \App\Models\Project $project
29
     * @param string              $disk
30
     *
31
     * @return void
32
     * @throws \GitWrapper\GitException
33
     */
34
    public static function initDir(Project $project, $disk = GenerateRdf::REPO_ROOT): void
35
    {
36
        $projectPath = self::getProjectPath($project->id);
37
        if (! Storage::disk($disk)->exists($projectPath)) {
38
            $dir = Storage::disk($disk)->path($projectPath);
39
40
            /** @var GitWrapper $wrapper */
41
            $wrapper = static::getWrapper();
42
            $repo    = self::getProjectRepo($project);
43
            if ($repo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $repo of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
44
                $git = $wrapper->cloneRepository($repo, $dir);
45
                if (! $git->isCloned()) {
46
                    throw new GitException($git);
47
                }
48
            } else {
49
                Storage::disk($disk)->createDir($projectPath);
0 ignored issues
show
Bug introduced by
The method createDir() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Contracts\Filesystem\Cloud. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
                Storage::disk($disk)->/** @scrutinizer ignore-call */ createDir($projectPath);
Loading history...
50
                $wrapper->init($dir);
51
            }
52
        }
53
    }
54
55
    /**
56
     * @param \App\Models\Project $project
57
     * @param string              $disk
58
     * @param                     $message
59
     *
60
     * @return void
61
     * @throws \GitWrapper\GitException
62
     */
63
    public static function commitDir(Project $project, $disk = GenerateRdf::REPO_ROOT, $message): void
64
    {
65
        $projectPath = self::getProjectPath($project->id);
66
        $dir         = Storage::disk($disk)->path($projectPath);
67
68
        /** @var GitWrapper $wrapper */
69
        $wrapper = static::getWrapper();
70
        $git = $wrapper->workingCopy($dir);
71
72
        if ($git->hasChanges()) {
73
            $git->add('.');
74
            $git->commit($message);
75
        }
76
    }
77
78
    /**
79
     * @return \GitWrapper\GitWrapper
80
     * @throws \GitWrapper\GitException
81
     */
82
    private static function getWrapper(): GitWrapper
83
    {
84
        return new GitWrapper('/usr/bin/git');
85
    }
86
}
87