isTracked($element) || !is_scalar($element)) { // element already tracked, avoid duplicate registration return false; } if (is_numeric($element)) { // cast to number to save some space occupied by json_encode for strings $element = is_float($element) ? (float) $element : (int) $element; } // make sure this trait has been attached only to a cron job instance if (!$this instanceof VBOCronJob) { throw new Exception('This tracker can be attached only to a VBOCronJob instance', 500); } // register the element at the beginning of the list $data = $this->getData(); array_unshift($data->flag_char, $element); // sanitize the elements contained within the cache in order // avoid a length exceeding the maximum threshold $this->sanitizeFlagCache($data); return true; } /** * Checks whether the specified element has been already processed. * * @param mixed $element The element to check. * * @return boolean True if already processed, false otherwise. */ protected function isTracked($element) { // make sure this trait has been attached only to a cron job instance if (!$this instanceof VBOCronJob) { throw new Exception('This tracker can be attached only to a VBOCronJob instance', 500); } return in_array($element, $this->getData()->flag_char); } /** * Ensures the array with the tracked elements doesn't exceed the maximum limit. * * @param object $data The cron job data. * * @return void */ private function sanitizeFlagCache($data) { // count the total number of characters that occupy the cache $len = 0; foreach ($data->flag_char as $element) { // sum value with the total number of characters $len += strlen((string) $element); if (!is_numeric($element)) { // we have a string element, we need to include other 2 characters reserved for wrapping $len += 2; } // increase the length by 1 to include also the elements separator (,) $len++; } if ($data->flag_char) { // remove the trailing comma separator $len--; } // sum also the initial and trailing brackets $len += 2; // even if the maximum number of allowed characters is 65535, we prefer to always stay under // 10000 elements, since json_encode may use a specific encoding for some characters if ($len > $this->maximumCharsTrackableElements) { // it is enough to pop the last 2 items from the list in order to stay under the desired limit, // since this method always triggers while registering a new element array_pop($data->flag_char); array_pop($data->flag_char); } } }