Completed
Pull Request — master (#1150)
by
unknown
01:40
created

LivewireServiceProvider::getComponentName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Livewire;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Filesystem\Filesystem;
7
use Nwidart\Modules\Support\Config\GenerateConfigReader;
8
9
class LivewireServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Register the service provider.
13
     *
14
     * @return void
15
     */
16
    public function register()
17
    {
18
        $this->loadComponents();
19
    }
20
21
    /**
22
     * Get the services provided by the provider.
23
     *
24
     * @return array
25
     */
26
    public function provides()
27
    {
28
        return [];
29
    }
30
31
    protected function loadComponents()
32
    {
33
        if (! class_exists('Livewire')) return false;
34
35
        $modules = \Module::toCollection();
36
37
        $defaultNamespace = GenerateConfigReader::read('livewire-namespace')->getPath();
38
        
39
        $filesystem = new Filesystem();
40
41
        $modules->map(function ($module) use ($filesystem, $defaultNamespace) {
42
            $modulePath = $module->getPath();
43
44
            $path = $modulePath. '/'. strtr($defaultNamespace, ['\\' => '/']);
45
46
            $files = collect( $filesystem->isDirectory($path) ? $filesystem->allFiles($path) : [] );
47
48
            $files->map(function ($file) use ($module, $path, $defaultNamespace) {
49
                $componentPath = \Str::after($file->getPathname(), $path.'/');
50
51
                $componentClassPath = strtr( $componentPath , ['/' => '\\', '.php' => '']);
52
        
53
                $componentName = $this->getComponentName($componentClassPath, $module);
54
55
                $componentClassStr = '\\' . config('modules.namespace') . '\\' . $module->getName() . '\\' . $defaultNamespace . '\\' . $componentClassPath;
56
57
                $componentClass = get_class(new $componentClassStr);
58
59
                $loadComponent = \Livewire::component($componentName, $componentClass);
0 ignored issues
show
Unused Code introduced by
$loadComponent is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
            });
61
        });
62
    }
63
64
    protected function getComponentName($componentClassPath, $module = null)
65
    {
66
        $dirs = explode('\\', $componentClassPath);
67
68
        $componentName = collect($dirs)
69
            ->map([\Str::class, 'kebab'])
70
            ->implode('.');
71
72
        $moduleNamePrefix = ($module) ? $module->getLowerName().'::' : null;
73
74
       return $moduleNamePrefix . $componentName;
75
    }
76
}