Passed
Pull Request — main (#1)
by Michael
03:09
created

AutoBindsToContainer::interfaceFrom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 2
rs 10
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 5
    protected function run(): void
19
    {
20 5
        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 5
            ->each(function (SplFileInfo $file) {
22 5
                $relativePath             = $file->getRelativePathname();
23 5
                $filenameWithoutExtension = $file->getFilenameWithoutExtension();
24 5
                $filenameWithRelativePath = $this->prepareFilename($relativePath);
25
26 5
                $interface = $this->interfaceFrom($filenameWithoutExtension);
27 5
                $concrete  = $this->concreteFrom($filenameWithRelativePath);
28
29 5
                app()->{$this->bindingType}($interface, $concrete);
30
            });
31
    }
32
33
    /**
34
     * Get the folder files.
35
     *
36
     * @return array
37
     */
38 5
    protected function getFolderFiles(): array
39
    {
40 5
        $filesystem = app('files');
41
42 5
        $folder = base_path($this->basePath . DIRECTORY_SEPARATOR . $this->classFolder);
43
44 5
        return $filesystem->isDirectory($folder)
45 5
            ? $filesystem->allFiles($folder)
46 5
            : [];
47
    }
48
49
    /**
50
     * Prepare the filename.
51
     *
52
     * @param string $filename
53
     *
54
     * @return string
55
     */
56 5
    protected function prepareFilename(string $filename): string
57
    {
58 5
        return (string) Str::of($filename)
59 5
            ->replace('/', '\\')
60 5
            ->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 5
    protected function namespaceFrom(string $path): string
71
    {
72 5
        return (string) Str::of($path)
73 5
            ->replace('/', '\\')
74 5
            ->ucfirst();
75
    }
76
77
    /**
78
     * Get the interface from filename.
79
     *
80
     * @param string $filenameWithoutExtension
81
     *
82
     * @return string
83
     */
84 5
    protected function interfaceFrom(string $filenameWithoutExtension): string
85
    {
86 5
        $interface = $this->guessInterfaceWith($filenameWithoutExtension);
87
88 5
        if (is_null($interface)) {
89 3
            return $this->interfaceNamespace
90
                . '\\'
91
                . $filenameWithoutExtension
92 3
                . ($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 5
    protected function guessInterfaceWith(string $filenameWithoutExtension): ?string
106
    {
107 5
        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 3
        return null;
119
    }
120
121
    /**
122
     * Get the concrete from filename.
123
     *
124
     * @param string $filenameWithRelativePath
125
     *
126
     * @return string
127
     */
128 5
    protected function concreteFrom(string $filenameWithRelativePath): string
129
    {
130 5
        return $this->classNamespace . '\\' . $this->classFolder . '\\' . $filenameWithRelativePath;
131
    }
132
}
133