Test Failed
Push — master ( fe7900...79d767 )
by Attila
11:12
created

ModuleIdGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 1
b 0
f 0
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A idOfClass() 0 21 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Contains the ModuleIdGenerator class.
7
 *
8
 * @copyright   Copyright (c) 2021 Attila Fulop
9
 * @author      Attila Fulop
10
 * @license     MIT
11
 * @since       2021-11-13
12
 *
13
 */
14
15
namespace Konekt\Concord\Utils;
16
17
use Illuminate\Support\Arr;
18
use Konekt\Concord\Contracts\Convention;
19
20
final class ModuleIdGenerator
21
{
22
    /**
23
     * Returns a standard module name based on the module provider's classname
24
     *
25
     * Eg.: '\Vendor\Module\Providers\ModuleServiceProvider' -> 'vendor.module'
26
     *      'App\Modules\Billing'                            -> 'billing'
27
     */
28
    public static function idOfClass(string $classname, ?Convention $convention = null): string
29
    {
30
        $convention = $convention ?: concord()->getConvention();
31
        $modulesFolder = $convention->modulesFolder();
32
        // Check if '\Modules\' is part of the namespace
33
        $p = strrpos($classname, "\\$modulesFolder\\");
34
        // if no \Modules\, but starts with 'Modules\' that's also a match
35
        $p = false === $p ? strpos($classname, "$modulesFolder\\") : $p;
36
        if (false !== $p) {
0 ignored issues
show
introduced by
The condition false !== $p is always true.
Loading history...
37
            $parts = explode('\\', substr($classname, $p + strlen($modulesFolder) + 1));
38
            $vendorAndModule = empty($parts[0]) ? Arr::only($parts, 1) : Arr::only($parts, 0);
39
        } else {
40
            $parts = explode('\\', $classname);
41
            $vendorAndModule = empty($parts[0]) ? Arr::only($parts, [1,2]) : Arr::only($parts, [0,1]);
42
        }
43
44
        array_walk($vendorAndModule, function (&$part) {
45
            $part = mb_strtolower($part);
46
        });
47
48
        return implode('.', $vendorAndModule);
49
    }
50
}
51