Passed
Push — master ( a2d6f0...8d3605 )
by AHMED JOHARI
06:58
created

Manager::getComponentFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Joesama\Pintu\Components;
3
4
use Exception;
5
use ReflectionClass;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
use Symfony\Component\Finder\Finder;
9
use Illuminate\Support\ServiceProvider;
10
11
class Manager
12
{
13
    private const FILE_NAME = 'component';
14
15
    private const FILE_EXT = '.php';
16
17
    /**
18
     * Component Path
19
     *
20
     * @var string
21
     */
22
    protected $componentPath;
23
24
    /**
25
     * Component Object.
26
     *
27
     * @var Collection
28
     */
29
    protected $component;
30
    
31
    /**
32
     * Initiate Component Manager.
33
     *
34
     * @param array $component
35
     */
36
    public function __construct($componentProvider)
37
    {
38
        if (!($componentProvider instanceof ServiceProvider)) {
39
            throw new Exception(
40
                \get_class($componentProvider) . ' must an instance of ' .  \basename(ServiceProvider::class),
41
                1
42
            );
43
        }
44
45
        $this->componentPath = $this->componentPath($componentProvider);
46
47
        $this->component = $this->componentSource();
48
    }
49
50
    /**
51
     * Return component collection.
52
     *
53
     * @return Collection
54
     */
55
    public function getComponent(): Collection
56
    {
57
        return Collection::make($this->component->get('component'));
58
    }
59
60
    /**
61
     * Return component collection.
62
     *
63
     * @return Collection
64
     */
65
    public function getApi(): Collection
66
    {
67
        return Collection::make($this->component->get('api'));
68
    }
69
70
    /**
71
     * Return the component file path.
72
     *
73
     * @return string
74
     */
75
    public function getComponentFilePath(): string
76
    {
77
        return $this->componentPath .'/' . self::FILE_NAME . self::FILE_EXT;
78
    }
79
80
    /**
81
     * Return the component file stub.
82
     *
83
     * @return string
84
     */
85
    public function getComponentFileStub(): string
86
    {
87
        return __DIR__.'/stubs/component.stub';
88
    }
89
90
    /**
91
     * Get the component resources.
92
     *
93
     * @return Collection
94
     */
95
    private function componentSource(): Collection
96
    {
97
        $content = [];
98
99
        if (realpath($this->componentPath) !== false) {
100
            $componentFile = Collection::make(
101
                Finder::create()
102
                ->files()
103
                ->name(self::FILE_NAME . self::FILE_EXT)
104
                ->in($this->componentPath)
105
            )->first();
106
107
            if ($componentFile !== null) {
108
                $content = include $componentFile->getRealPath();
109
            }
110
        }
111
112
        return Collection::make($content);
113
    }
114
115
    /**
116
     * Get the path for define component.
117
     *
118
     * @param ServiceProvider $provider
119
     *
120
     * @return string
121
     */
122
    private function componentPath(ServiceProvider $provider): string
123
    {
124
        $reflector = new ReflectionClass($provider);
125
       
126
        $classNameSpace = Collection::make(explode('\\', $reflector->getNamespaceName()));
127
128
        $baseDir = dirname($reflector->getFileName(), $classNameSpace->count());
129
130
        return $baseDir . '/' . self::FILE_NAME;
131
    }
132
}
133