1 | <?php |
||
2 | |||
3 | namespace Maestriam\Samurai\Entities; |
||
4 | |||
5 | use Maestriam\Samurai\Exceptions\InvalidThemeNameException; |
||
6 | |||
7 | class Vendor extends Foundation |
||
8 | { |
||
9 | /** |
||
10 | * Nome do projeto |
||
11 | */ |
||
12 | private string $name; |
||
13 | |||
14 | /** |
||
15 | * Distribuidor do projeto |
||
16 | */ |
||
17 | private string $distributor; |
||
18 | |||
19 | /** |
||
20 | * Nome do distribuidor/Nome do projeto |
||
21 | */ |
||
22 | private string $package; |
||
23 | |||
24 | /** |
||
25 | * Instancia as regras de negócio sobre o vendor |
||
26 | * |
||
27 | * @param string $vendor |
||
28 | */ |
||
29 | public function __construct(string $package = null) |
||
30 | { |
||
31 | ($package) ? $this->set($package) : $this->default(); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Retorna o nome do distribuidor do pacote |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | public function distributor() : string |
||
40 | { |
||
41 | return $this->distributor; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Retorna o nome do projeto |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | public function name() : string |
||
50 | { |
||
51 | return $this->name; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Retorna o nome do distribuidor/nome do projeto |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function package() : string |
||
60 | { |
||
61 | return $this->package; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Retorna o namespace baseado no nome do distribuidor e do projeto |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public function namespace() : string |
||
70 | { |
||
71 | return ucfirst($this->distributor) ."/". ucfirst($this->name); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Define o nome do pacote do projeto |
||
76 | * |
||
77 | * @param string $package |
||
78 | * @return Vendor |
||
79 | */ |
||
80 | private function setPackage(string $package) : Vendor |
||
81 | { |
||
82 | if (! $this->valid()->vendor($package)) { |
||
83 | throw new InvalidThemeNameException($package); |
||
84 | } |
||
85 | |||
86 | $this->package = $package; |
||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Interpreta as informações vindas do vendor para definir |
||
92 | * o nome, caminho, namespace e distribuidor do tema |
||
93 | * |
||
94 | * @return Vendor |
||
95 | */ |
||
96 | private function parsePackage(): Vendor |
||
97 | { |
||
98 | if (! $this->package) { |
||
99 | throw new InvalidThemeNameException($this->package); |
||
100 | } |
||
101 | |||
102 | $package = $this->parser()->vendor($this->package); |
||
103 | |||
104 | $name = $package->name; |
||
105 | $dist = $package->distributor; |
||
106 | |||
107 | $this->setName($name)->setDist($dist); |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Carrega as informações padrões do vendor |
||
114 | * |
||
115 | * @return Vendor |
||
116 | */ |
||
117 | private function default() : Vendor |
||
118 | { |
||
119 | $name = $this->dir()->project(); |
||
120 | |||
121 | $distributor = $this->config()->dist(); |
||
122 | |||
123 | $package = sprintf("%s/%s", $distributor, $name); |
||
124 | |||
125 | return $this->set($package); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Interpreta e define as informações sobre o vendor |
||
130 | * |
||
131 | * @param string $package |
||
132 | * @return void |
||
133 | */ |
||
134 | private function set(string $package) : Vendor |
||
135 | { |
||
136 | return $this->setPackage($package)->parsePackage(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Define o nome do projeto |
||
141 | * |
||
142 | * @param string $name |
||
143 | * @return Vendor |
||
144 | */ |
||
145 | private function setName(string $name) : Vendor |
||
146 | { |
||
147 | if (! $this->valid()->theme($name)) { |
||
148 | throw new InvalidThemeNameException($name); |
||
149 | } |
||
150 | |||
151 | $this->name = strtolower($name); |
||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Define o nome da distribuição do projeto |
||
157 | * |
||
158 | * @param string $dist |
||
159 | * @return Vendor |
||
160 | */ |
||
161 | private function setDist(string $dist) : Vendor |
||
162 | { |
||
163 | if (! $this->valid()->theme($dist)) { |
||
164 | throw new InvalidThemeNameException($dist); |
||
165 | } |
||
166 | |||
167 | $this->distributor = strtolower($dist); |
||
168 | return $this; |
||
169 | } |
||
170 | } |
||
171 |