Passed
Pull Request — main (#1)
by Michael
02:58
created

AutoBindsToContainer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
c 2
b 0
f 0
dl 0
loc 120
ccs 38
cts 38
cp 1
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 12 1
A getFolderFiles() 0 9 2
A prepareFilename() 0 5 1
A guessInterfaceWith() 0 14 2
A namespaceFrom() 0 5 1
A interfaceFrom() 0 12 2
A concreteFrom() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\AutoBinder\Traits;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
use Symfony\Component\Finder\SplFileInfo;
10
11
trait AutoBindsToContainer
12
{
13
    /**
14
     * Run the folders scanning & bind the results.
15
     *
16
     * @return void
17
     */
18 6
    protected function run(): void
19
    {
20 6
        collect($this->getFolderFiles())
0 ignored issues
show
Bug introduced by
$this->getFolderFiles() of type Symfony\Component\Finder\SplFileInfo[]|array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

20
        collect(/** @scrutinizer ignore-type */ $this->getFolderFiles())
Loading history...
21 6
            ->each(function (SplFileInfo $file) {
22 6
                $relativePath             = $file->getRelativePathname();
23 6
                $filenameWithoutExtension = $file->getFilenameWithoutExtension();
24 6
                $filenameWithRelativePath = $this->prepareFilename($relativePath);
25
26 6
                $interface = $this->interfaceFrom($filenameWithoutExtension);
27 6
                $concrete  = $this->concreteFrom($filenameWithRelativePath);
28
29 6
                app()->{$this->bindingType}($interface, $concrete);
30
            });
31
    }
32
33
    /**
34
     * Get the folder files.
35
     *
36
     * @return array
37
     */
38 6
    protected function getFolderFiles(): array
39
    {
40 6
        $filesystem = app('files');
41
42 6
        $folder = base_path($this->basePath . DIRECTORY_SEPARATOR . $this->classFolder);
43
44 6
        return $filesystem->isDirectory($folder)
45 6
            ? $filesystem->allFiles($folder)
46 6
            : [];
47
    }
48
49
    /**
50
     * Prepare the filename.
51
     *
52
     * @param string $filename
53
     *
54
     * @return string
55
     */
56 6
    protected function prepareFilename(string $filename): string
57
    {
58 6
        return (string) Str::of($filename)
59 6
            ->replace('/', '\\')
60 6
            ->substr(0, (int) strrpos($filename, '.'));
61
    }
62
63
    /**
64
     * Get the namespace from a given path.
65
     *
66
     * @param string $path
67
     *
68
     * @return string
69
     */
70 6
    protected function namespaceFrom(string $path): string
71
    {
72 6
        return (string) Str::of($path)
73 6
            ->replace('/', '\\')
74 6
            ->ucfirst();
75
    }
76
77
    /**
78
     * Get the interface from filename.
79
     *
80
     * @param string $filenameWithoutExtension
81
     *
82
     * @return string
83
     */
84 6
    protected function interfaceFrom(string $filenameWithoutExtension): string
85
    {
86 6
        $interface = $this->guessInterfaceWith($filenameWithoutExtension);
87
88 6
        if (is_null($interface)) {
89 4
            return $this->interfaceNamespace
90
                . '\\'
91
                . $filenameWithoutExtension
92 4
                . ($this->interfacePostfix);
93
        }
94
95 2
        return $interface;
96
    }
97
98
    /**
99
     * Guess the interface with a given filename.
100
     *
101
     * @param string $filenameWithoutExtension
102
     *
103
     * @return string|null
104
     */
105 6
    protected function guessInterfaceWith(string $filenameWithoutExtension): ?string
106
    {
107 6
        if (! Str::contains($this->interfaceNamespace, '\\')) {
108 2
            return $this->classNamespace
109
                . '\\'
110 2
                . $this->classFolder
111
                . '\\'
112 2
                . $this->interfaceNamespace
113
                . '\\'
114
                . $filenameWithoutExtension
115 2
                . ($this->interfacePostfix);
116
        }
117
118 4
        return null;
119
    }
120
121
    /**
122
     * Get the concrete from filename.
123
     *
124
     * @param string $filenameWithRelativePath
125
     *
126
     * @return string
127
     */
128 6
    protected function concreteFrom(string $filenameWithRelativePath): string
129
    {
130 6
        return $this->classNamespace . '\\' . $this->classFolder . '\\' . $filenameWithRelativePath;
131
    }
132
}
133