Passed
Push — main ( c1da4d...ec84d3 )
by Mohammad
02:58
created

PathResolver::resolvePath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Shamaseen\Repository;
4
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\Str;
7
8
class PathResolver
9
{
10
    protected string $modelName;
11
    protected string $userPath;
12
    protected string $basePath;
13
14
    public function __construct($modelName, $userPath, $basePath)
15
    {
16
        $this->modelName = $modelName;
17
        $this->userPath = $userPath;
18
        $this->basePath = $basePath;
19
    }
20
21
    public static array $configTypePathMap = [
22
        'Controller' => 'controllers_path',
23
        'Model' => 'models_path',
24
        'Repository' => 'repositories_path',
25
        'Request' => 'requests_path',
26
        'Resource' => 'json_resources_path',
27
    ];
28
29
    /**
30
     * @param string $type
31
     * @return string
32
     */
33
    public function outputPath(string $type): string
34
    {
35
        return $type === 'Model'
36
            ? $this->directionFromBase($type) . "/" . $this->modelName . ".php"
37
            : $this->directionFromBase($type) . "/" . $this->modelName . $type . ".php";
38
    }
39
40
    public function directionFromBase($type): string
41
    {
42
        return $this->typePath($type) . $this->pathSection($this->userPath);
43
    }
44
45
    public function absolutePath($type): string
46
    {
47
        return base_path($this->basePath ."/" . $this->outputPath($type));
48
    }
49
50
    public function pathSection(?string $section): string
51
    {
52
        return $section ? "/".$section : '';
53
    }
54
55
    public function typePath(string $type): string
56
    {
57
        return Config::get('repository.'.self::$configTypePathMap[$type]);
58
    }
59
60
    public function getPathRelativeToProject(string $path): string
61
    {
62
        if (Str::startsWith($path, base_path())) {
63
            return Str::after($path, base_path());
64
        }
65
66
        return $path;
67
    }
68
69
    /**
70
     * Resolve each ../
71
     * @param string $path
72
     * @return string
73
     */
74
    public function resolvePath(string $path): string
75
    {
76
        $paths = explode("/", $path);
77
78
        for ($i = 1; $i < count($paths); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
79
            if ($paths[$i] === "..") {
80
                unset($paths[$i-1],$paths[$i]);
81
            }
82
        }
83
84
        return implode("/", $paths);
85
    }
86
87
    public function typeNamespace(string $type): string
88
    {
89
        $resolvedPath = $this->resolvePath($this->basePath . "/". $this->typePath($type) . $this->pathSection($this->userPath));
90
91
        $upperCaseEachSection = Str::of($resolvedPath)->explode('/')
92
            ->reduce(fn ($total, $part) => $total."/".ucfirst($part));
93
94
        return Str::of($this->getPathRelativeToProject($upperCaseEachSection))->ltrim('/')->replace("/", "\\");
95
    }
96
97
    /**
98
     * Get stub path base on type.
99
     *
100
     * @param string $type determine which stub should choose to get content
101
     *
102
     * @return string
103
     */
104
    public function getStubPath(string $type): string
105
    {
106
        return Config::get('repository.stubs_path') . "/$type.stub";
107
    }
108
}
109