1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Widgets\Utils; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Imanghafoori\Widgets\BaseWidget; |
7
|
|
|
|
8
|
|
|
class Normalizer |
9
|
|
|
{ |
10
|
|
|
private $widget; |
11
|
|
|
|
12
|
|
|
public function normalizeWidgetConfig(BaseWidget $widget) |
13
|
|
|
{ |
14
|
|
|
$this->widget = $widget; |
15
|
|
|
$this->normalizeControllerMethod(); |
16
|
|
|
$this->normalizePresenterName(); |
17
|
|
|
$this->normalizeTemplateName(); |
18
|
|
|
$this->normalizeContextAs(); |
19
|
|
|
$this->normalizeCacheLifeTime(); |
20
|
|
|
$this->normalizeCacheTags(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Figures out which method should be called as the controller. |
25
|
|
|
* @return null |
26
|
|
|
*/ |
27
|
|
|
private function normalizeControllerMethod() |
28
|
|
|
{ |
29
|
|
|
// If the user has explicitly declared controller class path on the sub-class |
30
|
|
|
if ($this->widget->controller === null) { |
31
|
|
|
if (!method_exists($this->widget, 'data')) { |
32
|
|
|
throw new \BadMethodCallException("'data' method not found on " . class_basename($this->widget)); |
33
|
|
|
} |
34
|
|
|
// We decide to call data method on widget object. |
35
|
|
|
$this->widget->controller = [$this->widget, 'data']; |
36
|
|
View Code Duplication |
} else { |
|
|
|
|
37
|
|
|
// If the user has specified the controller class path |
38
|
|
|
if (!class_exists($this->widget->controller)) { |
39
|
|
|
throw new \InvalidArgumentException("Controller class: [{$this->widget->controller}] not found."); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// we decide to call data method on that. |
43
|
|
|
$this->widget->controller = ($this->widget->controller) . '@data'; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Figures out which method should be called as the presenter |
49
|
|
|
* @return null |
50
|
|
|
*/ |
51
|
|
|
private function normalizePresenterName() |
52
|
|
|
{ |
53
|
|
|
if ($this->widget->presenter === 'default') { |
54
|
|
|
$presenter = class_basename($this->widget) . 'Presenter'; |
55
|
|
|
|
56
|
|
|
if (class_exists($presenter)) { |
57
|
|
|
$this->widget->presenter = $presenter . '@presenter'; |
58
|
|
|
} else { |
59
|
|
|
$this->widget->presenter = null; |
60
|
|
|
} |
61
|
|
View Code Duplication |
} else { |
|
|
|
|
62
|
|
|
if (class_exists($this->widget->presenter) === false) { |
63
|
|
|
throw new \InvalidArgumentException("Presenter Class [{$this->widget->presenter}] not found."); |
64
|
|
|
} |
65
|
|
|
$this->widget->presenter = $this->widget->presenter . '@present'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Figures out which template to render. |
72
|
|
|
* @return null |
73
|
|
|
*/ |
74
|
|
|
private function normalizeTemplateName() |
75
|
|
|
{ |
76
|
|
|
// class name without namespace. |
77
|
|
|
$className = str_replace('App\\Widgets\\', '', class_basename($this->widget)); |
78
|
|
|
// replace slashes with dots |
79
|
|
|
$className = str_replace(['\\', '/'], '.', $className); |
80
|
|
|
|
81
|
|
|
if ($this->widget->template === null) { |
82
|
|
|
$this->widget->template = 'Widgets::' . $className . 'View'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!view()->exists($this->widget->template)) { |
|
|
|
|
86
|
|
|
throw new \InvalidArgumentException("View file [{$className}View] not found by: '" . class_basename($this->widget) . " '"); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Figures out what the variable name should be in view file. |
92
|
|
|
* @return null |
93
|
|
|
*/ |
94
|
|
|
private function normalizeContextAs() |
95
|
|
|
{ |
96
|
|
|
// removes the $ sign. |
97
|
|
|
$this->widget->contextAs = str_replace('$', '', (string)$this->widget->contextAs); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* ّFigures out how long the cache life time should be. |
102
|
|
|
* @return null |
103
|
|
|
*/ |
104
|
|
|
private function normalizeCacheLifeTime() |
105
|
|
|
{ |
106
|
|
|
if ($this->widget->cacheLifeTime === 'env_default') { |
107
|
|
|
$this->widget->cacheLifeTime = (int)(env('WIDGET_DEFAULT_CACHE_LIFETIME', 0)); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->widget->cacheLifeTime === 'forever') { |
111
|
|
|
$this->widget->cacheLifeTime = -1; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* ّFigures out what the cache tags should be. |
118
|
|
|
* @return null |
119
|
|
|
*/ |
120
|
|
|
private function normalizeCacheTags() |
121
|
|
|
{ |
122
|
|
|
if ($this->cacheShouldBeTagged()) { |
123
|
|
|
if (is_string($this->widget->cacheTags)) { |
124
|
|
|
$this->widget->cacheTags = [$this->widget->cacheTags]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (!is_array($this->widget->cacheTags)) { |
128
|
|
|
throw new \InvalidArgumentException('Cache Tags should be of type String or Array.'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
/** |
133
|
|
|
* Determine whether cache tags should be applied or not |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
private function cacheShouldBeTagged() |
137
|
|
|
{ |
138
|
|
|
return !in_array(env('CACHE_DRIVER', 'file'), ['file', 'database']) and $this->widget->cacheTags; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.