Complex classes like Amplitude 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 Amplitude, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class Amplitude |
||
| 5 | { |
||
| 6 | const AMPLITUDE_API_URL = 'https://api.amplitude.com/httpapi'; |
||
| 7 | |||
| 8 | const EXCEPTION_MSG_NO_API_KEY = 'API Key is required to log an event'; |
||
| 9 | const EXCEPTION_MSG_NO_EVENT_TYPE = 'Event Type is required to log or queue an event'; |
||
| 10 | const EXCEPTION_MSG_NO_USER_OR_DEVICE = 'Either user_id or device_id required to log an event'; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The API key to use for all events generated by this instance |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $apiKey; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The event that will be used for the next event being tracked |
||
| 21 | * |
||
| 22 | * @var \Zumba\Amplitude\Event |
||
| 23 | */ |
||
| 24 | protected $event; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The user ID to use for events generated by this instance |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $userId; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The user data to set on the next event logged to Amplitude |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $userProperties = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The device ID to use for events generated by this instance |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $deviceId; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Queue of events, used to allow generating events that might happen prior to amplitude being fully initialized |
||
| 49 | * |
||
| 50 | * @var \Zumba\Amplitude\Event[] |
||
| 51 | */ |
||
| 52 | protected $queue = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Flag for if user is opted out of tracking |
||
| 56 | * |
||
| 57 | * @var boolean |
||
| 58 | */ |
||
| 59 | protected $optOut = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Flag for if should save the last HTTP response for debugging purposes |
||
| 63 | * |
||
| 64 | * @var boolean True to enable saving the last response |
||
| 65 | */ |
||
| 66 | protected $debugResponse = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Last response from logging event |
||
| 70 | * |
||
| 71 | * @var array|null |
||
| 72 | */ |
||
| 73 | protected $lastHttpResponse; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Array of Amplitude instances |
||
| 77 | * |
||
| 78 | * @var \Zumba\Amplitude\Amplitude[] |
||
| 79 | */ |
||
| 80 | private static $instances = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Singleton to get named instance |
||
| 84 | * |
||
| 85 | * Using this is optional, it depends on the use-case if it is better to use a singleton instance or just create |
||
| 86 | * a new object directly. |
||
| 87 | * |
||
| 88 | * Useful if want to possibly send multiple events for the same user in a single page load, or even keep track |
||
| 89 | * of multiple named instances, each could track to it's own api key and/or user/device ID. |
||
| 90 | * |
||
| 91 | * Each instance maintains it's own: |
||
| 92 | * - API Key |
||
| 93 | * - User ID |
||
| 94 | * - Device ID |
||
| 95 | * - User Properties |
||
| 96 | * - Event Queue (if events are queued before the amplitude instance is initialized) |
||
| 97 | * - Event object - for the next event that will be sent or queued |
||
| 98 | * - Opt out status |
||
| 99 | * |
||
| 100 | * @param string $instanceName Optional, can use to maintain multiple singleton instances of amplitude, each with |
||
| 101 | * it's own API key set |
||
| 102 | * @return \Zumba\Amplitude\Amplitude |
||
| 103 | */ |
||
| 104 | public static function getInstance($instanceName = 'default') |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Constructor, optionally sets the api key |
||
| 114 | * |
||
| 115 | * @param string $apiKey |
||
| 116 | */ |
||
| 117 | public function __construct($apiKey = null) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Initialize amplitude |
||
| 126 | * |
||
| 127 | * This lets you set the api key, and optionally the user ID. |
||
| 128 | * |
||
| 129 | * @param string $apiKey Amplitude API key |
||
| 130 | * @param string $userId |
||
| 131 | * @return \Zumba\Amplitude\Amplitude |
||
| 132 | */ |
||
| 133 | public function init($apiKey, $userId = null) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Log any events that were queued before amplitude was initialized |
||
| 144 | * |
||
| 145 | * Note that api key, and either the user ID or device ID need to be set prior to calling this. |
||
| 146 | * |
||
| 147 | * @return \Zumba\Amplitude\Amplitude |
||
| 148 | * @throws \LogicException |
||
| 149 | */ |
||
| 150 | public function logQueuedEvents() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Clear out all events in the queue, without sending them to amplitude |
||
| 165 | * |
||
| 166 | * @return \Zumba\Amplitude\Amplitude |
||
| 167 | */ |
||
| 168 | public function resetQueue() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Gets the event that will be used for the next event logged by call to logEvent() or queueEvent() |
||
| 176 | * |
||
| 177 | * You can also pass in an event or array of event properties. If you pass in an event, it will be set as the |
||
| 178 | * event to be used for the next call to queueEvent() or logEvent() |
||
| 179 | * |
||
| 180 | * @param null|array|\Zumba\Amplitude\Event Can pass in an event to set as the next event to run, or array to set |
||
| 181 | * properties on that event |
||
| 182 | * @return \Zumba\Amplitude\Event |
||
| 183 | */ |
||
| 184 | public function event($event = null) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Resets the event currently in the process of being set up (what is returned by event()) |
||
| 201 | * |
||
| 202 | * @return \Zumba\Amplitude\Amplitude |
||
| 203 | */ |
||
| 204 | public function resetEvent() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Log an event immediately |
||
| 212 | * |
||
| 213 | * Requires amplitude is already initialized and user ID or device ID is set. If need to wait until amplitude |
||
| 214 | * is initialized, use queueEvent() method instead. |
||
| 215 | * |
||
| 216 | * Can either pass in information to be logged, or can set up the Event object before hand, see the event() |
||
| 217 | * method for more information |
||
| 218 | * |
||
| 219 | * @param string $eventType Required if not set on event object prior to calling this |
||
| 220 | * @param array $eventProperties Optional, properties to set on event |
||
| 221 | * @return \Zumba\Amplitude\Amplitude |
||
| 222 | * @throws \LogicException Thorws exception if any of the requirments are not met, such as api key set |
||
| 223 | */ |
||
| 224 | public function logEvent($eventType = '', array $eventProperties = []) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set the persistent data on the event object |
||
| 256 | * |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | protected function setPersistentEventData() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Log or queue the event, depending on if amplitude instance is already set up or not |
||
| 276 | * |
||
| 277 | * Note that this is an internal queue, the queue is lost between page loads. |
||
| 278 | * |
||
| 279 | * This functions identically to logEvent, with the exception that if Amplitude is not yet set up, it queues the |
||
| 280 | * event to be logged later (during same page load). |
||
| 281 | * |
||
| 282 | * If the API key, and either user ID or device ID are already set in the amplitude instance, and there is not |
||
| 283 | * already events in the queue that have not been run, this will log the event immediately. Note that having |
||
| 284 | * the userId or deviceId set on the event itself does not affect if it queues the event or not, only if set on |
||
| 285 | * the Amplitude instance. |
||
| 286 | * |
||
| 287 | * Otherwise it will queue the event, and will be run after the amplitude instance is initialized and |
||
| 288 | * logQueuedEvents() method is run |
||
| 289 | * |
||
| 290 | * @param string $eventType |
||
| 291 | * @param array $eventProperties |
||
| 292 | * @return \Zumba\Amplitude\Amplitude |
||
| 293 | * @throws \LogicException |
||
| 294 | */ |
||
| 295 | public function queueEvent($eventType = '', array $eventProperties = []) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Set the user ID for future events logged |
||
| 320 | * |
||
| 321 | * Any set with this will take precedence over any set on the Event object |
||
| 322 | * |
||
| 323 | * @param string $userId |
||
| 324 | * @return \Zumba\Amplitude\Amplitude |
||
| 325 | */ |
||
| 326 | public function setUserId($userId) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set the device ID for future events logged |
||
| 334 | * |
||
| 335 | * Any set with this will take precedence over any set on the Event object |
||
| 336 | * |
||
| 337 | * @param string $deviceId |
||
| 338 | * @return \Zumba\Amplitude\Amplitude |
||
| 339 | */ |
||
| 340 | public function setDeviceId($deviceId) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Set the user properties, will be sent with the next event sent to Amplitude |
||
| 348 | * |
||
| 349 | * Any set with this will take precedence over any set on the Event object |
||
| 350 | * |
||
| 351 | * If no events are logged, it will not get sent to Amplitude |
||
| 352 | * |
||
| 353 | * @param array $userProperties |
||
| 354 | */ |
||
| 355 | public function setUserProperties(array $userProperties) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Resets user properties added with setUserProperties() if they have not already been sent in an event to Amplitude |
||
| 363 | * |
||
| 364 | * @return \Zumba\Amplitude\Amplitude |
||
| 365 | */ |
||
| 366 | public function resetUserProperties() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Check if there are events in the queue that have not been sent |
||
| 374 | * |
||
| 375 | * @return boolean |
||
| 376 | */ |
||
| 377 | public function hasQueuedEvents() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Resets all user information |
||
| 384 | * |
||
| 385 | * This resets the user ID, device ID previously set using setUserId or setDeviceId. |
||
| 386 | * |
||
| 387 | * If additional information was previously set using setUserProperties() method, and the event has not already |
||
| 388 | * been sent to Amplitude, it will reset that information as well. |
||
| 389 | * |
||
| 390 | * Does not reset user information if set manually on an individual event in the queue. |
||
| 391 | * |
||
| 392 | * @return \Zumba\Amplitude\Amplitude |
||
| 393 | */ |
||
| 394 | public function resetUser() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Set opt out for the current user. |
||
| 404 | * |
||
| 405 | * If set to true, will not send any future events to amplitude for this amplitude instance. |
||
| 406 | * |
||
| 407 | * @param boolean $optOut |
||
| 408 | * @return \Zumba\Amplitude\Amplitude |
||
| 409 | */ |
||
| 410 | public function setOptOut($optOut) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Getter for currently set api key |
||
| 418 | * |
||
| 419 | * @return string|null |
||
| 420 | */ |
||
| 421 | public function getApiKey() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Getter for currently set user ID |
||
| 428 | * |
||
| 429 | * @return string|null |
||
| 430 | */ |
||
| 431 | public function getUserId() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Getter for currently set device ID |
||
| 438 | * |
||
| 439 | * @return string|null |
||
| 440 | */ |
||
| 441 | public function getDeviceId() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Getter for all currently set user properties, that will be automatically sent on next Amplitude event |
||
| 448 | * |
||
| 449 | * Once the properties have been sent in an Amplitude event, they will be cleared. |
||
| 450 | * |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | public function getUserProperties() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Get the current value for opt out. |
||
| 460 | * |
||
| 461 | * @return boolean |
||
| 462 | */ |
||
| 463 | public function getOptOut() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Sets flag for debugging responses |
||
| 470 | * |
||
| 471 | * See getLastHttpResponse() for getting the last response |
||
| 472 | * |
||
| 473 | * @param boolean $debug If true, will keep track of last response from Amplitude for debugging purposes |
||
| 474 | * @return \Zumba\Amplitude\Amplitude |
||
| 475 | */ |
||
| 476 | public function setDebugResponse($debug) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Gets current flag value for whether to record the last response from Amplitude when sending events |
||
| 484 | * |
||
| 485 | * @return boolean |
||
| 486 | */ |
||
| 487 | public function getDebugResponse() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * For debugging new connections - returns array with details of the last log, or null if no logs sent. |
||
| 494 | * |
||
| 495 | * Returned array will be in the format: |
||
| 496 | * <code> |
||
| 497 | * $lastHttpResponse = [ |
||
| 498 | * 'code' => 100, // HTTP Response Code |
||
| 499 | * 'message' => 'success', // Response received |
||
| 500 | * 'curl_error' => '...', // Only set if there was some error when attempting to send the request |
||
| 501 | * ]; |
||
| 502 | * </code> |
||
| 503 | * |
||
| 504 | * @return array|null Returns null if debug response is disabled, or if no events sent to Amplitude yet |
||
| 505 | */ |
||
| 506 | public function getLastHttpResponse() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Send the event currently set in $this->event to amplitude |
||
| 513 | * |
||
| 514 | * Requres $this->event and $this->apiKey to be set, otherwise it throws an exception. |
||
| 515 | * |
||
| 516 | * @return void |
||
| 517 | * @throws \InternalErrorException If event or api key not set |
||
| 518 | */ |
||
| 519 | protected function sendEvent() |
||
| 547 | } |
||
| 548 |