Helpers::makeDirectory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php namespace Savannabits\JetstreamInertiaGenerator\Generators\Traits;
2
3
use Illuminate\Support\Facades\Schema;
4
use Illuminate\Support\Str;
5
6
trait Helpers {
7
8
    public function option($key = null) {
9
        return ($key === null || $this->hasOption($key)) ? parent::option($key) : null;
0 ignored issues
show
Bug introduced by
The method hasOption() does not exist on Savannabits\JetstreamIne...nerators\Traits\Helpers. Did you maybe mean option()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
10
    }
11
12
    /**
13
     * Build the directory for the class if necessary.
14
     *
15
     * @param string $path
16
     * @return string
17
     */
18
    protected function makeDirectory(string $path): string
19
    {
20
        if (! $this->files->isDirectory(dirname($path))) {
21
            $this->files->makeDirectory(dirname($path), 0777, true, true);
0 ignored issues
show
Bug introduced by
The property files does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
        }
23
24
        return $path;
25
    }
26
27
    /**
28
     * Determine if the file already exists.
29
     *
30
     * @param $path
31
     * @return bool
32
     */
33
    protected function alreadyExists($path): bool
34
    {
35
        return $this->files->exists($path);
36
    }
37
38
39
    /**
40
     * Check if provided relation has a table
41
     *
42
     * @param $relationTable
43
     * @return mixed
44
     */
45
    public function checkRelationTable($relationTable)
46
    {
47
        return Schema::hasTable($relationTable);
48
    }
49
50
    /**
51
     * sets Relation of Belongs To Many type
52
     *
53
     * @param $belongsToMany
54
     * @return mixed
55
     */
56
    //TODO add other relation types
57
    public function setBelongToManyRelation($belongsToMany)
58
    {
59
        $this->relations['belongsToMany'] = collect(explode(',', $belongsToMany))->filter(function($belongToManyRelation) {
0 ignored issues
show
Bug introduced by
The property relations does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
            return $this->checkRelationTable($belongToManyRelation);
61
        })->map(function($belongsToMany) {
62
            return [
63
                'current_table' => $this->tableName,
0 ignored issues
show
Bug introduced by
The property tableName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
                'related_table' => $belongsToMany,
65
                'related_model' => ($belongsToMany == 'roles') ? "Spatie\\Permission\\Models\\Role" : "App\\Models\\". Str::studly(Str::singular($belongsToMany)),
66
                'related_model_class' => ($belongsToMany == 'roles') ? "Spatie\\Permission\\Models\\Role::class" : "App\\Models\\". Str::studly(Str::singular($belongsToMany)).'::class',
67
                'related_model_name' => Str::studly(Str::singular($belongsToMany)),
68
                'related_model_name_plural' => Str::studly($belongsToMany),
69
                'related_model_variable_name' => lcfirst(Str::singular(class_basename($belongsToMany))),
70
                'relation_table' => trim(collect([$this->tableName, $belongsToMany])->sortBy(function($table) {
71
                    return $table;
72
                })->reduce(function($relationTable, $table) {
73
                    return $relationTable.'_'.$table;
74
                }), '_'),
75
                'foreign_key' => Str::singular($this->tableName).'_id',
76
                'related_key' => Str::singular($belongsToMany).'_id',
77
            ];
78
        })->keyBy('related_table');
79
    }
80
81
82
    /**
83
     * Determine if the content is already present in the file
84
     *
85
     * @param $path
86
     * @param $content
87
     * @return bool
88
     */
89
    protected function alreadyAppended($path, $content): bool
90
    {
91
        if (str_contains($this->files->get($path), $content)) {
92
            return true;
93
        }
94
        return false;
95
    }
96
97
}
98