Example #1
0
 /**
  * Generates a set of unique tokens according to the given token settings.
  * Returns false if the generation is not possible, due to set insert
  * probability MAX_INSERT_PROBABILITY.
  *
  * @return array|bool
  */
 public function generateCodes()
 {
     // Size of one segment of tokens to check against the db.
     $tokenCheckStep = ceil($this->configuration->getCount() / 250);
     if ($this->isValidGeneration()) {
         $finalTokenLength = $this->getFinalTokenLength();
         // Check if a max_packet_size Error is possible
         $possibleMaxQuerySizeError = $finalTokenLength * $this->configuration->getCount() / 1024 / 1024 > 15;
         // Return Query
         $resultTokenSet = false;
         // Tokens of one Insert Query
         $insertTokens = [];
         // Tokens of all Insert Queries together
         $insertCheckTokens = [];
         // Tokens of one segment of tokens to check against if they already exist in the db
         $checkTokens = [];
         // Count for all tokens to insert into db
         $insertCount = 0;
         // Count for tokens to check in db in on segment
         $checkTokenCount = 0;
         // Create unique tokens
         while ($insertCount < $this->configuration->getCount()) {
             // Considerations for last Couple of tokens, so that the amount of overall tokens is correct.
             if ($this->configuration->getCount() > $insertCount + $checkTokenCount) {
                 $token = $this->formatCode($this->generateCode());
                 // If the key already exists in the current checkTokens Segment,
                 // do not increase the checkTokensCount
                 if (!array_key_exists($token, $checkTokens)) {
                     $checkTokens[$token] = $token;
                     $checkTokenCount++;
                 }
             } else {
                 $checkTokenCount++;
             }
             // Check the temp array checkTokens if the just generated token already exists.
             // If so, unset the last token and decrease the count for the array of tokens to check
             if ($this->tokenExists($checkTokens, $insertCheckTokens)) {
                 $checkTokenCount--;
                 unset($checkTokens[$token]);
                 // Check if the length of the checkTokens Array matches the defined step range
                 // so the the checkTokens get matched against the database.
             } elseif ($checkTokenCount == $tokenCheckStep) {
                 // Check if any of the tokens in the temporary array checkTokens already exists,
                 // if not so, merge the checkTokens array with the array of tokens to insert and
                 // increase the overall count by the length of the checkArray i.e. the checkTokenStep
                 if (!OnlineShop_Framework_VoucherService_Token_List::tokensExist($checkTokens)) {
                     $insertTokens = array_merge($insertTokens, $checkTokens);
                     $insertCount += $tokenCheckStep;
                 }
                 $checkTokenCount = 0;
                 $checkTokens = [];
                 // If an max_package_size error is possible build a new insert query.
                 if ($possibleMaxQuerySizeError) {
                     if ($insertCount * $finalTokenLength / 1024 / 1024 > 15) {
                         $resultTokenSet[] = $insertTokens;
                         $insertCheckTokens = array_merge($insertTokens, $insertCheckTokens);
                         $insertTokens = [];
                     }
                 } else {
                     // If no Error is possible or insert query needed, the overall tokens
                     // are the insert tokens of the current query, because there will be only
                     // one or no query.
                     $insertCheckTokens = $insertTokens;
                 }
             }
         }
         // If there are tokens to insert add them to query.
         if (sizeof($insertTokens)) {
             $resultTokenSet[] = $insertTokens;
         }
         return $resultTokenSet;
     }
     return false;
 }