Complex classes like SessionTracker 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 SessionTracker, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\LaravelTracker\Trackers; |
||
| 16 | class SessionTracker implements SessionTrackerContract |
||
| 17 | { |
||
| 18 | /* ------------------------------------------------------------------------------------------------ |
||
| 19 | | Properties |
||
| 20 | | ------------------------------------------------------------------------------------------------ |
||
| 21 | */ |
||
| 22 | /** @var \Illuminate\Session\Store */ |
||
| 23 | private $session; |
||
| 24 | |||
| 25 | /** @var array */ |
||
| 26 | private $sessionInfo = []; |
||
| 27 | |||
| 28 | /* ------------------------------------------------------------------------------------------------ |
||
| 29 | | Constructor |
||
| 30 | | ------------------------------------------------------------------------------------------------ |
||
| 31 | */ |
||
| 32 | /** |
||
| 33 | * SessionTracker constructor. |
||
| 34 | * |
||
| 35 | * @param \Illuminate\Session\Store $session |
||
| 36 | */ |
||
| 37 | 24 | public function __construct(SessionStore $session) |
|
| 38 | { |
||
| 39 | 24 | $this->session = $session; |
|
| 40 | 24 | } |
|
| 41 | |||
| 42 | /* ------------------------------------------------------------------------------------------------ |
||
| 43 | | Getters & Setters |
||
| 44 | | ------------------------------------------------------------------------------------------------ |
||
| 45 | */ |
||
| 46 | /** |
||
| 47 | * Get the session key. |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | */ |
||
| 51 | 6 | private function getSessionKey() |
|
| 52 | { |
||
| 53 | 6 | return config('laravel-tracker.session.name', 'session_name_here'); |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Set the session data. |
||
| 58 | * |
||
| 59 | * @param array $data |
||
| 60 | */ |
||
| 61 | 6 | private function setSessionData(array $data) |
|
| 62 | { |
||
| 63 | 6 | $this->generateSession($data); |
|
| 64 | |||
| 65 | 6 | if ($this->createSessionIfIsUnknown()) { |
|
| 66 | $this->ensureSessionDataIsComplete(); |
||
| 67 | } |
||
| 68 | 2 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Get the session id. |
||
| 72 | * |
||
| 73 | * @return int |
||
| 74 | */ |
||
| 75 | 2 | private function getSessionId() |
|
| 76 | { |
||
| 77 | 2 | return $this->sessionInfo['id']; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set the session id. |
||
| 82 | * |
||
| 83 | * @param mixed $id |
||
| 84 | */ |
||
| 85 | 2 | private function setSessionId($id) |
|
| 86 | { |
||
| 87 | 2 | $this->sessionInfo['id'] = $id; |
|
| 88 | 2 | } |
|
| 89 | |||
| 90 | /* ------------------------------------------------------------------------------------------------ |
||
| 91 | | Main Functions |
||
| 92 | | ------------------------------------------------------------------------------------------------ |
||
| 93 | */ |
||
| 94 | /** |
||
| 95 | * Track the session. |
||
| 96 | * |
||
| 97 | * @param array $data |
||
| 98 | * |
||
| 99 | * @return int |
||
| 100 | */ |
||
| 101 | 6 | public function track(array $data) |
|
| 102 | { |
||
| 103 | 6 | $this->setSessionData($data); |
|
| 104 | |||
| 105 | 2 | return $this->getSessionId(); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Check the session data. |
||
| 110 | * |
||
| 111 | * @param array $newData |
||
| 112 | * @param array $currentData |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | 6 | public function checkData(array $newData, array $currentData) |
|
| 117 | { |
||
| 118 | 6 | return ($newData && $currentData && $newData !== $currentData) |
|
| 119 | 3 | ? $this->updateData($newData) |
|
| 120 | 6 | : $newData; |
|
| 121 | } |
||
| 122 | |||
| 123 | /* ------------------------------------------------------------------------------------------------ |
||
| 124 | | Other Functions |
||
| 125 | | ------------------------------------------------------------------------------------------------ |
||
| 126 | */ |
||
| 127 | /** |
||
| 128 | * Update the session data. |
||
| 129 | * |
||
| 130 | * @param array $data |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | private function updateData(array $data) |
||
| 143 | |||
| 144 | 6 | private function getSessionData($column = null) |
|
| 145 | { |
||
| 146 | 6 | $data = $this->session->get($this->getSessionKey()); |
|
| 147 | |||
| 148 | 6 | return $column ? Arr::get($data, $column, null) : $data; |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Check if user changed. |
||
| 153 | * |
||
| 154 | * @param array $data |
||
| 155 | * @param \Arcanedev\LaravelTracker\Models\Session $model |
||
| 156 | * |
||
| 157 | * @return \Arcanedev\LaravelTracker\Models\Session Session |
||
| 158 | */ |
||
| 159 | private function checkIfUserChanged(array $data, $model) |
||
| 172 | |||
| 173 | private function regenerateSystemSession($data = null) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Reset the session uuid. |
||
| 187 | * |
||
| 188 | * @param array|null $data |
||
| 189 | * |
||
| 190 | * @return array|null |
||
| 191 | */ |
||
| 192 | private function resetSessionUuid($data = null) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Put the session data. |
||
| 208 | * |
||
| 209 | * @param mixed $data |
||
| 210 | */ |
||
| 211 | 2 | private function putSessionData($data) |
|
| 212 | { |
||
| 213 | 2 | $this->session->put([ |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Check the session uuid. |
||
| 220 | */ |
||
| 221 | 6 | private function checkSessionUuid() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Get the system session id. |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 6 | private function getSystemSessionId() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | 6 | private function createSessionIfIsUnknown() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Check if the session is known. |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | 6 | private function sessionIsKnown() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Find a session by its uuid. |
||
| 282 | * |
||
| 283 | * @param string $uuid |
||
| 284 | * |
||
| 285 | * @return \Arcanedev\LaravelTracker\Models\Session |
||
| 286 | */ |
||
| 287 | private function findByUuid($uuid) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Store the session information. |
||
| 294 | */ |
||
| 295 | 2 | private function storeSession() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Generate session data. |
||
| 302 | * |
||
| 303 | * @param array $sessionInfo |
||
| 304 | */ |
||
| 305 | 6 | private function generateSession($sessionInfo) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Check if the session data is reliable. |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | 6 | private function checkSessionDataIsReliable() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Ensure that the session data is complete. |
||
| 339 | */ |
||
| 340 | private function ensureSessionDataIsComplete() |
||
| 360 | } |
||
| 361 |
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.