1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BoxedCode\Tracking\Trackers; |
4
|
|
|
|
5
|
|
|
use BoxedCode\Tracking\TrackableResourceModel; |
6
|
|
|
use BoxedCode\Tracking\TrackedEvent; |
7
|
|
|
use Illuminate\Contracts\Config\Repository; |
8
|
|
|
use Illuminate\Contracts\Container\Container; |
9
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Routing\Router; |
12
|
|
|
use League\Url\Url; |
13
|
|
|
|
14
|
|
|
abstract class Tracker |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Container instance. |
18
|
|
|
* |
19
|
|
|
* @var \Illuminate\Contracts\Container\Container |
20
|
|
|
*/ |
21
|
|
|
protected $container; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Tracker data model. |
25
|
|
|
* |
26
|
|
|
* @var \BoxedCode\Tracking\TrackableResourceModel |
27
|
|
|
*/ |
28
|
|
|
protected $model; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Configuration repository. |
32
|
|
|
* |
33
|
|
|
* @var \Illuminate\Contracts\Config\Repository |
34
|
|
|
*/ |
35
|
|
|
protected $config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Event dispatcher. |
39
|
|
|
* |
40
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
41
|
|
|
*/ |
42
|
|
|
protected $events; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The trackers type handle. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $handle; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The trackers route name. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $route_name; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The trackers route parameter. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $route_parameter; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Tracker constructor. |
67
|
|
|
* |
68
|
|
|
* @param \Illuminate\Contracts\Container\Container $container |
69
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events |
70
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config |
71
|
|
|
*/ |
72
|
38 |
|
public function __construct(Container $container, Dispatcher $events, Repository $config) |
73
|
|
|
{ |
74
|
38 |
|
$this->container = $container; |
75
|
|
|
|
76
|
38 |
|
$this->events = $events; |
77
|
|
|
|
78
|
38 |
|
$this->config = $config; |
79
|
38 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the type handle. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
10 |
|
public function getHandle() |
87
|
|
|
{ |
88
|
10 |
|
return $this->handle; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the route name. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
3 |
|
public function getRouteName() |
97
|
|
|
{ |
98
|
3 |
|
return $this->route_name; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the route parameter key. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
1 |
|
public function getRouteKey() |
107
|
|
|
{ |
108
|
1 |
|
return $this->route_parameter; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the data model. |
113
|
|
|
* |
114
|
|
|
* @param \BoxedCode\Tracking\TrackableResourceModel $model |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
25 |
|
public function setModel(TrackableResourceModel $model) |
118
|
|
|
{ |
119
|
25 |
|
$this->model = $model; |
120
|
|
|
|
121
|
25 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the data model. |
126
|
|
|
* |
127
|
|
|
* @return \BoxedCode\Tracking\TrackableResourceModel |
128
|
|
|
*/ |
129
|
3 |
|
public function getModel() |
130
|
|
|
{ |
131
|
3 |
|
return $this->model; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set the routing parameter. |
136
|
|
|
* |
137
|
|
|
* @param $parameter |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
1 |
|
public function setRoutingParameter($parameter) |
141
|
|
|
{ |
142
|
1 |
|
$this->route_parameter = $parameter; |
143
|
|
|
|
144
|
1 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the routing parameter. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
1 |
|
public function getRoutingParameter() |
153
|
|
|
{ |
154
|
1 |
|
return $this->route_parameter; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the routing path. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
38 |
|
protected function getRoutingPath() |
163
|
|
|
{ |
164
|
38 |
|
$path = str_finish($this->config->get('tracking.path'), '/'); |
165
|
|
|
|
166
|
38 |
|
return $path.$this->route_parameter.'/{id}'; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Register the trackers route. |
171
|
|
|
* |
172
|
|
|
* @param \Illuminate\Routing\Router $router |
173
|
|
|
*/ |
174
|
38 |
|
public function registerRoute(Router $router) |
175
|
|
|
{ |
176
|
38 |
|
$path = $this->getRoutingPath(); |
177
|
|
|
|
178
|
38 |
|
$router->get($path, ['as' => $this->route_name, function (Request $request, $id) { |
179
|
8 |
|
if (! ($model = TrackableResourceModel::find($id))) { |
180
|
1 |
|
abort(404); |
181
|
|
|
} |
182
|
|
|
|
183
|
7 |
|
if (false === $this->events->fire('tracking.tracked', new TrackedEvent($model, $request))) { |
184
|
|
|
abort(404); |
185
|
|
|
} else { |
186
|
7 |
|
return $this->handle($request, $model); |
187
|
|
|
} |
188
|
38 |
|
}]); |
189
|
38 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Handle the tracking request. |
193
|
|
|
* |
194
|
|
|
* @param \Illuminate\Http\Request $request |
195
|
|
|
* @param \BoxedCode\Tracking\TrackableResourceModel $model |
196
|
|
|
* @return mixed |
197
|
|
|
*/ |
198
|
|
|
abstract public function handle(Request $request, TrackableResourceModel $model); |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Generate a model attribute array from an argument array. |
202
|
|
|
* |
203
|
|
|
* @param array $args |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
3 |
|
public function getModelAttributes(array $args) |
207
|
|
|
{ |
208
|
|
|
$attrs = [ |
209
|
3 |
|
'id' => $this->getUniqueId(), |
210
|
3 |
|
'type' => get_class($this), |
211
|
3 |
|
]; |
212
|
|
|
|
213
|
3 |
|
return array_merge($args, $attrs); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get a 'trackable' url for the current data model. |
218
|
|
|
* |
219
|
|
|
* @param array $parameters |
220
|
|
|
* @return \League\Url\Url |
221
|
|
|
*/ |
222
|
13 |
|
public function getTrackedUrl($parameters = []) |
223
|
|
|
{ |
224
|
13 |
|
$resolver = $this->config->get('tracking.resolver', 'route'); |
225
|
|
|
|
226
|
13 |
|
$url = call_user_func_array($resolver, [$this->route_name, $this->model->getKey()]); |
227
|
|
|
|
228
|
13 |
|
$params = ''; |
229
|
|
|
|
230
|
13 |
|
if (count($parameters) > 0) { |
231
|
2 |
|
$params = "?"; |
232
|
|
|
|
233
|
2 |
|
foreach ($parameters as $key => $value) { |
234
|
2 |
|
$params .= "$key=$value&"; |
235
|
2 |
|
} |
236
|
|
|
|
237
|
2 |
|
$params = substr($params, 0, -1); |
238
|
2 |
|
} |
239
|
|
|
|
240
|
13 |
|
return $url . $params; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Get a unique model id. |
245
|
|
|
* |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
3 |
|
protected function getUniqueId() |
249
|
|
|
{ |
250
|
3 |
|
while (! isset($id) || TrackableResourceModel::find($id)) { |
251
|
3 |
|
$id = str_random(6); |
252
|
3 |
|
} |
253
|
|
|
|
254
|
3 |
|
return $id; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get a string representation of the tracker, the 'trackable' url. |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
7 |
|
public function __toString() |
262
|
|
|
{ |
263
|
7 |
|
return (string) $this->getTrackedUrl(); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|