Complex classes like VisitorTracker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use VisitorTracker, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\LaravelTracker\Trackers; |
||
| 15 | class VisitorTracker extends AbstractTracker implements VisitorTrackerContract |
||
| 16 | { |
||
| 17 | /* ----------------------------------------------------------------- |
||
| 18 | | Properties |
||
| 19 | | ----------------------------------------------------------------- |
||
| 20 | */ |
||
| 21 | |||
| 22 | /** @var array */ |
||
| 23 | private $visitorInfo = []; |
||
| 24 | |||
| 25 | /* ----------------------------------------------------------------- |
||
| 26 | | Getters and Setters |
||
| 27 | | ----------------------------------------------------------------- |
||
| 28 | */ |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Get the model. |
||
| 32 | * |
||
| 33 | * @return \Arcanedev\LaravelTracker\Models\Visitor |
||
| 34 | */ |
||
| 35 | 9 | protected function getModel() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Get the session key. |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | 9 | private function getSessionKey() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Set the visitor data. |
||
| 52 | * |
||
| 53 | * @param array $data |
||
| 54 | */ |
||
| 55 | 9 | private function setVisitorData(array $data) |
|
| 56 | { |
||
| 57 | 9 | $this->generateVisitor($data); |
|
| 58 | |||
| 59 | 9 | if ($this->createVisitorIfIsUnknown()) { |
|
| 60 | $this->ensureVisitorDataIsComplete(); |
||
| 61 | } |
||
| 62 | 9 | } |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Get the visitor id. |
||
| 66 | * |
||
| 67 | * @return int |
||
| 68 | */ |
||
| 69 | 9 | private function getVisitorId() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Set the visitor id. |
||
| 76 | * |
||
| 77 | * @param mixed $id |
||
| 78 | */ |
||
| 79 | 9 | private function setVisitorId($id) |
|
| 83 | |||
| 84 | /* ----------------------------------------------------------------- |
||
| 85 | | Main Methods |
||
| 86 | | ----------------------------------------------------------------- |
||
| 87 | */ |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Track the visitor. |
||
| 91 | * |
||
| 92 | * @param array $data |
||
| 93 | * |
||
| 94 | * @return int |
||
| 95 | */ |
||
| 96 | 9 | public function track(array $data) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Check the visitor data. |
||
| 105 | * |
||
| 106 | * @param array $currentData |
||
| 107 | * @param array $newData |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | 6 | public function checkData(array $currentData, array $newData) |
|
| 117 | |||
| 118 | /* ----------------------------------------------------------------- |
||
| 119 | | Other Methods |
||
| 120 | | ----------------------------------------------------------------- |
||
| 121 | */ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Update the visitor data. |
||
| 125 | * |
||
| 126 | * @param array $data |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | 6 | private function updateData(array $data) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get the visitor data. |
||
| 139 | * |
||
| 140 | * @param string|null $column |
||
| 141 | * |
||
| 142 | * @return mixed |
||
| 143 | */ |
||
| 144 | 9 | private function getVisitorData($column = null) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Check if user changed. |
||
| 153 | * |
||
| 154 | * @param array $data |
||
| 155 | */ |
||
| 156 | 6 | private function checkIfUserChanged(array $data) |
|
| 157 | { |
||
| 158 | 6 | $model = $this->getModel()->newQuery()->find($this->getVisitorData('id')); |
|
| 159 | |||
| 160 | if ( |
||
| 161 | 6 | ! is_null($model) && |
|
| 162 | 4 | ! is_null($model->user_id) && |
|
| 163 | 4 | ! is_null($data['user_id']) && |
|
| 164 | 4 | $data['user_id'] !== $model->user_id |
|
| 165 | ) { |
||
| 166 | $newVisitor = $this->regenerateSystemVisitor($data); |
||
| 167 | $model = $this->findByUuid($newVisitor['uuid']); |
||
| 168 | $model->update(Arr::except($data, ['id', 'uuid'])); |
||
| 169 | } |
||
| 170 | 6 | } |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Regenerate visitor data for the system. |
||
| 174 | * |
||
| 175 | * @param array|null $data |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | private function regenerateSystemVisitor($data = null) |
||
| 180 | { |
||
| 181 | $data = $data ?: $this->getVisitorData(); |
||
| 182 | |||
| 183 | if ($data) { |
||
| 184 | $this->resetVisitorUuid($data); |
||
| 185 | $this->createVisitorIfIsUnknown(); |
||
| 186 | } |
||
| 187 | |||
| 188 | return $this->visitorInfo; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Reset the visitor uuid. |
||
| 193 | * |
||
| 194 | * @param array|null $data |
||
| 195 | * |
||
| 196 | * @return array|null |
||
| 197 | */ |
||
| 198 | private function resetVisitorUuid($data = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Put the session data. |
||
| 214 | * |
||
| 215 | * @param array $data |
||
| 216 | */ |
||
| 217 | 9 | private function putSessionData(array $data) |
|
| 218 | { |
||
| 219 | 9 | $this->session()->put([ |
|
| 220 | 9 | $this->getSessionKey() => $data |
|
| 221 | ]); |
||
| 222 | 9 | } |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Check the visitor uuid. |
||
| 226 | */ |
||
| 227 | 9 | private function checkVisitorUuid() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Get the visitor id from the system. |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | 9 | private function getVisitorIdFromSystem() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Create a new visitor if is unknown. |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | 9 | private function createVisitorIfIsUnknown() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Check if the visitor is known. |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 9 | private function isVisitorKnown() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Find a visitor by its uuid. |
||
| 293 | * |
||
| 294 | * @param string $uuid |
||
| 295 | * |
||
| 296 | * @return \Arcanedev\LaravelTracker\Models\Visitor |
||
| 297 | */ |
||
| 298 | private function findByUuid($uuid) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Generate visitor data. |
||
| 305 | * |
||
| 306 | * @param array $visitorInfo |
||
| 307 | */ |
||
| 308 | 9 | private function generateVisitor(array $visitorInfo) |
|
| 309 | { |
||
| 310 | 9 | $this->visitorInfo = $visitorInfo; |
|
| 311 | |||
| 312 | 9 | if ( ! $this->checkVisitorDataIsReliable()) |
|
| 313 | $this->regenerateSystemVisitor(); |
||
| 314 | |||
| 315 | 9 | $this->checkVisitorUuid(); |
|
| 316 | 9 | } |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Check if the visitor data is reliable. |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | 9 | private function checkVisitorDataIsReliable() |
|
| 324 | { |
||
| 325 | 9 | $data = $this->getVisitorData(); |
|
| 326 | |||
| 327 | 9 | foreach (['user_id', 'client_ip', 'user_agent'] as $key) { |
|
| 328 | 9 | if ($this->checkDataIsUnreliable($data, $key)) return false; |
|
| 329 | } |
||
| 330 | |||
| 331 | 9 | return true; |
|
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Check the data is unreliable. |
||
| 336 | * |
||
| 337 | * @param array|null $data |
||
| 338 | * @param string $key |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | */ |
||
| 342 | 9 | private function checkDataIsUnreliable($data, $key) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Ensure that the visitor data is complete. |
||
| 349 | */ |
||
| 350 | private function ensureVisitorDataIsComplete() |
||
| 351 | { |
||
| 352 | $visitorData = $this->getVisitorData(); |
||
| 371 | } |
||
| 372 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.