Passed
Push — master ( 762407...a7439c )
by Roy
04:51 queued 03:09
created

ModulizerRepository::hasModules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelModulize\Services;
4
5
use Illuminate\Console\DetectsApplicationNamespace;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Collection;
8
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
9
use LaravelModulize\Contracts\ModulizerRepositoryInterface;
10
11
class ModulizerRepository implements ModulizerRepositoryInterface
12
{
13
    use DetectsApplicationNamespace;
14
15
    public $migrations = [];
16
17
    public $translations = [];
18
19
    /**
20
     * Instance of Filesystem
21
     *
22
     * @var \Illuminate\Contracts\Filesystem\Filesystem
23
     */
24
    protected $filesystem;
25
26
    /**
27
     * Construct ModulizerRepository
28
     *
29
     * @param \Illuminate\Filesystem\Filesystem $filesystem
30
     */
31
    public function __construct(Filesystem $filesystem)
32
    {
33
        $this->filesystem = $filesystem;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filesystem of type Illuminate\Filesystem\Filesystem is incompatible with the declared type Illuminate\Contracts\Filesystem\Filesystem of property $filesystem.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
    }
35
36
    /**
37
     * Get the configurable base path to the folder the modules will be in.
38
     *
39
     * @return string
40
     */
41
    public function getBasePath(): string
42
    {
43
        return config('modulizer.modules_path');
44
    }
45
46
    /**
47
     * Determine if there are modules available
48
     *
49
     * @return boolean
50
     */
51
    public function hasModules(): bool
52
    {
53
        return $this->filesExist(
54
            $this->getBasePath()
55
        );
56
    }
57
58
    /**
59
     * Collect the available modules
60
     *
61
     * @return \Illuminate\Support\Collection
62
     */
63
    public function getModules(): Collection
64
    {
65
        return collect($this->filesystem->directories($this->getBasePath()))
66
            ->map(function ($directory) {
67
                return class_basename($directory);
68
            });
69
    }
70
71
    /**
72
     * Retrieve the path for a single module
73
     *
74
     * @param string $module
75
     * @return string
76
     */
77
    public function getModulePath(string $module): string
78
    {
79
        return $this->getBasePath() . "/{$module}";
80
    }
81
82
    /**
83
     * Collect all files preset at the given path
84
     *
85
     * @param string $path
86
     * @return \Illuminate\Support\Collection
87
     */
88
    public function getFiles(string $path): Collection
89
    {
90
        return collect($this->filesystem->files($path));
91
    }
92
93
    /**
94
     * Collect all files preset at the given pattern
95
     *
96
     * @param string $path
97
     * @return \Illuminate\Support\Collection
98
     */
99
    public function glob(string $pattern): Collection
100
    {
101
        return collect($this->filesystem->glob($pattern));
0 ignored issues
show
Bug introduced by
The method glob() 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

101
        return collect($this->filesystem->/** @scrutinizer ignore-call */ glob($pattern));
Loading history...
102
    }
103
104
    /**
105
     * Determine if a path or file exists
106
     *
107
     * @param string $path
108
     * @return boolean
109
     */
110
    public function filesExist(string $path): bool
111
    {
112
        return $this->filesystem->exists(
113
            $path
114
        );
115
    }
116
117
    /**
118
     * Get the app's root namespace
119
     *
120
     * @return string
121
     */
122
    public function getRootNamespace(): string
123
    {
124
        return $this->getAppNamespace();
125
    }
126
127
    /**
128
     * Get the namespace the modules should recieve
129
     * This namespace will be a child of the root namespace
130
     *
131
     * @return string
132
     */
133
    public function getModulesNamespace(): string
134
    {
135
        return config('modulizer.namespace');
136
    }
137
138
    /**
139
     * Retrieve the namespace of a single module
140
     *
141
     * @param string $module
142
     * @return string
143
     */
144
    public function getModuleNamespace(string $module): string
145
    {
146
        return $this->getRootNamespace() . $this->getModulesNamespace() . $module;
147
    }
148
149
    public function addTranslation(string $path, string $namespace)
150
    {
151
        $this->translations[] = (object) [
152
            'path' => $path,
153
            'namespace' => $namespace,
154
        ];
155
    }
156
157
    /**
158
     * Register factories.
159
     *
160
     * @param  string  $path
161
     * @return void
162
     */
163
    public function registerEloquentFactoriesFrom($path)
164
    {
165
        $this->app->make(EloquentFactory::class)->load($path);
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist on LaravelModulize\Services\ModulizerRepository. Did you maybe forget to declare it?
Loading history...
166
    }
167
}
168