/**
  * This is a simple system to generate unique keys. It will need to be improved in the future for better performance.
  * 
  * @param Object $object - must have a getPeer method.
  */
 private static function getUniqueKey($object, $ppk_field = self::KEY_FIELD_NAME, $key_length = self::KEY_LENGTH, $key_char_pool = self::KEY_CHARACTER_POOL)
 {
     $object_peer = self::getObjectPeer($object);
     if (is_null($object_peer)) {
         throw new \Exception("Object must have a Peer class.");
     }
     $tries = 0;
     while (self::primaryKeyValueExists($new_id = \Altumo\String\String::generateRandomString($key_length, $key_char_pool), $object_peer, $ppk_field)) {
         if ($tries++ > 30) {
             throw new \Exception("Tried to generate a unique value for {$ppk_field} {$tries} times and failed.");
         }
     }
     return $new_id;
 }
Exemplo n.º 2
0
 /**
  * Stores the SSL certificate in a temporary file and returns its path.
  * 
  * When making a request with an SSL certificate, curl requires that
  * certificate to be in a file. This function stores the certificate in a 
  * temporary file and returns its path.
  * 
  * 
  * @see deleteSslCertificateTempFile
  *   // This method will be called after the certificate has been used in order
  *   // to clean up the file that was created.
  * 
  * @throws \Exception
  *   // If the temp certificate file cannot be created or written
  * 
  * @param string $temp_path
  */
 protected function createSslCertificateTempFile()
 {
     // If a certificate temp file has already been created, delete it
     $this->deleteSslCertificateTempFile();
     // Gets certificate data
     $ssl_cert_data = $this->getSslCertificateData();
     \Altumo\Validation\Strings::assertNonEmptyString($ssl_cert_data);
     // Use system temp directory path
     $temp_path = sys_get_temp_dir();
     // Make a new filename for the certificate using its own hash and a random string
     $temp_filename = $temp_path . '/altumo_pem_' . sha1($ssl_cert_data) . '_' . \Altumo\String\String::generateRandomString(4);
     // Write the contents of the certificate to the temporary file
     if (file_put_contents($temp_filename, $this->getSslCertificateData()) === false) {
         throw new \Exception('Unable to write to temporary certificate file.');
     }
     $this->setSslCertificateTempFile($temp_filename);
     return $temp_filename;
 }