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

AutoBindsToContainer::getFolderFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 9
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\Str;
8
use Symfony\Component\Finder\SplFileInfo;
9
10
trait AutoBindsToContainer
11
{
12
    /**
13
     * Run the folders scanning & bind the results.
14
     *
15
     * @return void
16
     */
17 8
    protected function run(): void
18
    {
19 8
        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

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