1 | <?php |
||
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() |
|
90 | |||
91 | /** |
||
92 | * Get the route name. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 3 | public function getRouteName() |
|
100 | |||
101 | /** |
||
102 | * Get the route parameter key. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 1 | public function getRouteKey() |
|
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() |
|
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() |
|
156 | |||
157 | /** |
||
158 | * Get the routing path. |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 38 | protected function getRoutingPath() |
|
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) |
|
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 = []) |
|
242 | |||
243 | /** |
||
244 | * Get a unique model id. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | 3 | protected function getUniqueId() |
|
256 | |||
257 | /** |
||
258 | * Get a string representation of the tracker, the 'trackable' url. |
||
259 | * @return string |
||
260 | */ |
||
261 | 7 | public function __toString() |
|
265 | } |
||
266 |