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 | * Array of Amplitude instances | ||
| 63 | * | ||
| 64 | * @var \Zumba\Amplitude\Amplitude[] | ||
| 65 | */ | ||
| 66 | private static $instances = []; | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Singleton to get named instance | ||
| 70 | * | ||
| 71 | * Using this is optional, it depends on the use-case if it is better to use a singleton instance or just create | ||
| 72 | * a new object directly. | ||
| 73 | * | ||
| 74 | * Useful if want to possibly send multiple events for the same user in a single page load, or even keep track | ||
| 75 | * of multiple named instances, each could track to it's own api key and/or user. | ||
| 76 | * | ||
| 77 | * Example: | ||
| 78 | * <code> | ||
| 79 | * // In user initialization section of app... | ||
| 80 |      * Zumba\Amplitude\Amplitude::getInstance()->init('APIKEY','[email protected]') | ||
| 81 | * ->addUserProperties(['name' => 'Johnny 5']) | ||
| 82 | * ->logQueuedEvents(); | ||
| 83 | * | ||
| 84 | * // Elsewhere in your app, this could happen before OR after the above initialization... | ||
| 85 | * // See queueEvent() for more info | ||
| 86 | * Zumba\Amplitude\Amplitude::getInstance()->queueEvent( | ||
| 87 | * 'VEHICLE CREATED EVENT', | ||
| 88 | * ['wheels' => '2', 'name' => 'bicycle'] | ||
| 89 | * ); | ||
| 90 | * </code> | ||
| 91 | * | ||
| 92 | * @param string $instanceName Optional, can use to maintain multiple singleton instances of amplitude | ||
| 93 | * @return \Zumba\Amplitude\Amplitude | ||
| 94 | */ | ||
| 95 | public static function getInstance($instanceName = 'default') | ||
| 96 |     { | ||
| 97 |         if (empty(self::$instances[$instanceName])) { | ||
| 98 | self::$instances[$instanceName] = new static(); | ||
| 99 | } | ||
| 100 | return self::$instances[$instanceName]; | ||
| 101 | } | ||
| 102 | |||
| 103 | /** | ||
| 104 | * Constructor, optionally sets the api key | ||
| 105 | * | ||
| 106 | * @param string $apiKey | ||
| 107 | */ | ||
| 108 | public function __construct($apiKey = null) | ||
| 114 | |||
| 115 | /** | ||
| 116 | * Initialize amplitude | ||
| 117 | * | ||
| 118 | * This lets you set the api key, and optionally the user ID. | ||
| 119 | * | ||
| 120 | * @param string $apiKey Amplitude API key | ||
| 121 | * @param string $userId | ||
| 122 | * @return \Zumba\Amplitude\Amplitude | ||
| 123 | */ | ||
| 124 | public function init($apiKey, $userId = null) | ||
| 132 | |||
| 133 | /** | ||
| 134 | * Log any events that were queued before amplitude was initialized | ||
| 135 | * | ||
| 136 | * Note that api key, and either the user ID or device ID need to be set prior to calling this. | ||
| 137 | * | ||
| 138 | * @return \Zumba\Amplitude\Amplitude | ||
| 139 | * @throws \LogicException | ||
| 140 | */ | ||
| 141 | public function logQueuedEvents() | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Clear out all events in the queue, without sending them to amplitude | ||
| 156 | * | ||
| 157 | * @return \Zumba\Amplitude\Amplitude | ||
| 158 | */ | ||
| 159 | public function resetQueue() | ||
| 164 | |||
| 165 | /** | ||
| 166 | * Gets the event that will be used for the next event logged by call to logEvent() or queueEvent() | ||
| 167 | * | ||
| 168 | * If this style suites your project, you can do something like this: | ||
| 169 | * | ||
| 170 | * <code> | ||
| 171 | * $event = $amplitude->event(); | ||
| 172 | * // Set up event here... | ||
| 173 |      * $event->set('event_type', 'EVENT TYPE') | ||
| 174 |      *     ->set('custom property', 'Some Value'); | ||
| 175 | * | ||
| 176 | * // Calling this will log the event we just set up | ||
| 177 | * $amplitude->logEvent(); | ||
| 178 | * </code> | ||
| 179 | * | ||
| 180 | * That would be the equivelent of this: | ||
| 181 | * <code> | ||
| 182 |      * $amplitude->logEvent('EVENT TYPE', ['custom property' => 'Some Value']); | ||
| 183 | * </code> | ||
| 184 | * | ||
| 185 | * Note that either use above requires amplitude to be set up first with all the required things set (like api | ||
| 186 | * key and either user ID or device ID). You can replace logEvent with queueEvent if you may need to run it before | ||
| 187 | * amplitude is fully initialized. | ||
| 188 | * | ||
| 189 | * You can also pass in an event to set as the next event to run. | ||
| 190 | * | ||
| 191 | * @param null|array|\Zumba\Amplitude\Event Can pass in an event to set as the next event to run, or array to set | ||
| 192 | * properties on that event | ||
| 193 | * @return \Zumba\Amplitude\Event | ||
| 194 | */ | ||
| 195 | public function event($event = null) | ||
| 209 | |||
| 210 | /** | ||
| 211 | * Resets the event currently in the process of being set up (what is returned by event()) | ||
| 212 | * | ||
| 213 | * This could end up clearing user data set. | ||
| 214 | * | ||
| 215 | * @return \Zumba\Amplitude\Amplitude | ||
| 216 | */ | ||
| 217 | public function resetEvent() | ||
| 222 | |||
| 223 | /** | ||
| 224 | * Log an event immediately | ||
| 225 | * | ||
| 226 | * Requires amplitude is already initialized and user ID or device ID is set. If need to wait until amplitude | ||
| 227 | * is initialized, use queueEvent method instead. | ||
| 228 | * | ||
| 229 | * Can either pass in information to be logged, or can set up the event using OOP, see the event() method for more | ||
| 230 | * information | ||
| 231 | * | ||
| 232 | * @param string $eventType Required if not set on event object prior to calling this | ||
| 233 | * @param array $eventProperties Optional, properties to set on event | ||
| 234 | * @return \Zumba\Amplitude\Amplitude | ||
| 235 | * @throws \LogicException Thorws exception if any of the requirments are not met, such as api key set | ||
| 236 | */ | ||
| 237 | public function logEvent($eventType = '', array $eventProperties = []) | ||
| 280 | |||
| 281 | /** | ||
| 282 | * Log or queue the event, depending on if amplitude instance is already set up or not | ||
| 283 | * | ||
| 284 | * Note that this is an internal queue, the queue is lost between page loads. | ||
| 285 | * | ||
| 286 | * This functions identically to logEvent, with the exception that if Amplitude is not yet set up, it queues the | ||
| 287 | * event to be logged later (during same page load). | ||
| 288 | * | ||
| 289 | * If the API key, and either user ID or device ID are already set in the amplitude instance, and there is not | ||
| 290 | * already events in the queue that have not been run, this will log the event immediately. | ||
| 291 | * | ||
| 292 | * Otherwise it will queue the event, and will be run after the amplitude instance is initialized and | ||
| 293 | * logQueuedEvents() method is run | ||
| 294 | * | ||
| 295 | * @param string $eventType | ||
| 296 | * @param array $eventProperties | ||
| 297 | * @return \Zumba\Amplitude\Amplitude | ||
| 298 | * @throws \LogicException | ||
| 299 | */ | ||
| 300 | public function queueEvent($eventType = '', array $eventProperties = []) | ||
| 324 | |||
| 325 | /** | ||
| 326 | * Set the user ID for future events logged | ||
| 327 | * | ||
| 328 | * Note that setting the user ID directly on an individual Event object will take precedence over one set with this | ||
| 329 | * method. | ||
| 330 | * | ||
| 331 | * @param string $userId | ||
| 332 | * @return \Zumba\Amplitude\Amplitude | ||
| 333 | */ | ||
| 334 | public function setUserId($userId) | ||
| 339 | |||
| 340 | /** | ||
| 341 | * Set the device ID for future events logged | ||
| 342 | * | ||
| 343 | * Note that setting the device ID directly on an individual Event object will take precedence over one set with | ||
| 344 | * this method. | ||
| 345 | * | ||
| 346 | * @param string $deviceId | ||
| 347 | * @return \Zumba\Amplitude\Amplitude | ||
| 348 | */ | ||
| 349 | public function setDeviceId($deviceId) | ||
| 354 | |||
| 355 | /** | ||
| 356 | * Set the user properties, will be sent with the next event sent to Amplitude | ||
| 357 | * | ||
| 358 | * If no events are logged, it will not get sent to Amplitude | ||
| 359 | * | ||
| 360 | * @param array $userProperties | ||
| 361 | */ | ||
| 362 | public function addUserProperties(array $userProperties) | ||
| 367 | |||
| 368 | /** | ||
| 369 | * Resets user properties added with addUserProperties() if they have not already been sent in an event to Amplitude | ||
| 370 | * | ||
| 371 | * @return \Zumba\Amplitude\Amplitude | ||
| 372 | */ | ||
| 373 | public function resetUserProperties() | ||
| 378 | |||
| 379 | /** | ||
| 380 | * Check if there are events in the queue that have not been sent | ||
| 381 | * | ||
| 382 | * @return boolean | ||
| 383 | */ | ||
| 384 | public function hasQueuedEvents() | ||
| 388 | |||
| 389 | /** | ||
| 390 | * Resets all user information | ||
| 391 | * | ||
| 392 | * This resets the user ID, device ID previously set using setUserId or setDeviceId. | ||
| 393 | * | ||
| 394 | * If additional information was previously set using addUserProperties() method, and the event has not already | ||
| 395 | * been sent to Amplitude, it will reset that information as well. | ||
| 396 | * | ||
| 397 | * Does not reset user information if set manually on an individual event in the queue. | ||
| 398 | * | ||
| 399 | * @return \Zumba\Amplitude\Amplitude | ||
| 400 | */ | ||
| 401 | public function resetUser() | ||
| 408 | |||
| 409 | /** | ||
| 410 | * Set opt out for the current user. | ||
| 411 | * | ||
| 412 | * If set to true, will not send any future events to amplitude for this amplitude instance. | ||
| 413 | * | ||
| 414 | * @param boolean $optOut | ||
| 415 | * @return \Zumba\Amplitude\Amplitude | ||
| 416 | */ | ||
| 417 | public function setOptOut($optOut) | ||
| 422 | |||
| 423 | /** | ||
| 424 | * Getter for currently set api key | ||
| 425 | * | ||
| 426 | * @return string|null | ||
| 427 | */ | ||
| 428 | public function getApiKey() | ||
| 432 | |||
| 433 | /** | ||
| 434 | * Getter for currently set user ID | ||
| 435 | * | ||
| 436 | * @return string|null | ||
| 437 | */ | ||
| 438 | public function getUserId() | ||
| 442 | |||
| 443 | /** | ||
| 444 | * Getter for currently set device ID | ||
| 445 | * | ||
| 446 | * @return string|null | ||
| 447 | */ | ||
| 448 | public function getDeviceId() | ||
| 452 | |||
| 453 | /** | ||
| 454 | * Getter for all currently set user properties, that will be automatically sent on next Amplitude event | ||
| 455 | * | ||
| 456 | * Once the properties have been sent in an Amplitude event, they will be cleared. | ||
| 457 | * | ||
| 458 | * @return array | ||
| 459 | */ | ||
| 460 | public function getUserProperties() | ||
| 464 | |||
| 465 | /** | ||
| 466 | * Get the current value for opt out. | ||
| 467 | * | ||
| 468 | * @return boolean | ||
| 469 | */ | ||
| 470 | public function getOptOut() | ||
| 474 | |||
| 475 | /** | ||
| 476 | * Send the event currently set in $this->event to amplitude | ||
| 477 | * | ||
| 478 | * Requres $this->event and $this->apiKey to be set, otherwise it throws an exception. | ||
| 479 | * | ||
| 480 | * @return integer|boolean HTTP Status code or boolean false if problem making connection | ||
| 481 | * @throws \InternalErrorException If event or api key not set | ||
| 482 | */ | ||
| 483 | protected function sendEvent() | ||
| 503 | } | ||
| 504 |