Passed
Pull Request — main (#1)
by Michael
12:17
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 2
    protected function run(): void
19
    {
20 2
        collect($this->getFolderFiles())
21 2
            ->each(function (SplFileInfo $file) {
22 2
                $relativePath             = $file->getRelativePathname();
23 2
                $filenameWithoutExtension = $file->getFilenameWithoutExtension();
24 2
                $filenameWithRelativePath = $this->prepareFilename($relativePath);
25
26 2
                $interface = $this->interfaceFrom($filenameWithoutExtension);
27 2
                $concrete  = $this->concreteFrom($filenameWithRelativePath);
28
29 2
                app()->{$this->bindingType}($interface, $concrete);
30
            });
31
    }
32
33
    /**
34
     * Get the folder files.
35
     *
36
     * @return Collection
37
     */
38 2
    protected function getFolderFiles(): Collection
39
    {
40 2
        $filesystem = app('files');
41
42 2
        $folder = base_path($this->basePath . DIRECTORY_SEPARATOR . $this->classFolder);
43
44 2
        return collect($filesystem->isDirectory($folder) ? $filesystem->allFiles($folder) : []);
0 ignored issues
show
Bug introduced by
$filesystem->isDirectory...iles($folder) : array() 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

44
        return collect(/** @scrutinizer ignore-type */ $filesystem->isDirectory($folder) ? $filesystem->allFiles($folder) : []);
Loading history...
45
    }
46
47
    /**
48
     * Prepare the filename.
49
     *
50
     * @param string $filename
51
     *
52
     * @return string
53
     */
54 2
    protected function prepareFilename(string $filename): string
55
    {
56 2
        return (string) Str::of($filename)
57 2
            ->replace('/', '\\')
58 2
            ->substr(0, (int) strrpos($filename, '.'));
59
    }
60
61
    /**
62
     * Get the namespace from a given path.
63
     *
64
     * @param string $path
65
     *
66
     * @return string
67
     */
68 2
    protected function namespaceFrom(string $path): string
69
    {
70 2
        return (string) Str::of($path)
71 2
            ->replace('/', '\\')
72 2
            ->ucfirst();
73
    }
74
75
    /**
76
     * Get the interface from filename.
77
     *
78
     * @param string $filenameWithoutExtension
79
     *
80
     * @return string
81
     */
82 2
    protected function interfaceFrom(string $filenameWithoutExtension): string
83
    {
84 2
        $interface = $this->guessInterfaceWith($filenameWithoutExtension);
85
86 2
        if (is_null($interface)) {
87 1
            return $this->interfaceNamespace
88
                . '\\'
89
                . $filenameWithoutExtension
90 1
                . ($this->interfacePostfix);
91
        }
92
93 1
        return $interface;
94
    }
95
96
    /**
97
     * Guess the interface with a given filename.
98
     *
99
     * @param string $filenameWithoutExtension
100
     *
101
     * @return string|null
102
     */
103 2
    protected function guessInterfaceWith(string $filenameWithoutExtension): ?string
104
    {
105 2
        if (! Str::contains($this->interfaceNamespace, '\\')) {
106 1
            return $this->classNamespace
107
                . '\\'
108 1
                . $this->classFolder
109
                . '\\'
110 1
                . $this->interfaceNamespace
111
                . '\\'
112
                . $filenameWithoutExtension
113 1
                . ($this->interfacePostfix);
114
        }
115
116 1
        return null;
117
    }
118
119
    /**
120
     * Get the concrete from filename.
121
     *
122
     * @param string $filenameWithRelativePath
123
     *
124
     * @return string
125
     */
126 2
    protected function concreteFrom(string $filenameWithRelativePath): string
127
    {
128 2
        return $this->classNamespace . '\\' . $this->classFolder . '\\' . $filenameWithRelativePath;
129
    }
130
}
131