Esempio n. 1
0
 /**
  * @param string $qbankURL The URL to the QBank API.
  * @param Credentials $credentials The credentials used to connect.
  * @param array $options Associative array containing options.
  * <ul>
  * <li>Cache $options[cache] A cache implementation to store tokens and responses in. Highly recommended.</li>
  * <li>QBankCachePolicy $options[cachePolicy] A policy on how to use caching for API queries, if not provided cache will not be available for API queries.</li>
  * <li>LoggerInterface $options[log] A PSR-3 log implementation.</li>
  * <li>bool $options[verifyCertificates] Whether to verify certificates for https connections. Defaults to true.</li>
  * </ul>
  */
 public function __construct($qbankURL, Credentials $credentials, array $options = [])
 {
     // Setup logging
     if (!empty($options['log']) && $options['log'] instanceof LoggerInterface) {
         $this->logger = $options['log'];
     } else {
         $this->logger = new NullLogger();
     }
     $this->basepath = $this->buildBasepath($qbankURL);
     // Store credentials for later use
     $this->credentials = $credentials;
     // Optionaly setup cache
     if (!empty($options['cache']) && $options['cache'] instanceof Cache) {
         $this->cache = $options['cache'];
         if ($this->cache instanceof CacheProvider && !$this->cache->getNamespace()) {
             $this->cache->setNamespace(md5($this->basepath . $this->credentials->getUsername() . $this->credentials->getPassword()));
         }
     } else {
         $this->logger->notice('No caching supplied! Without caching both performance and security is reduced.');
     }
     // Setup the cache policy
     if (!empty($options['cachePolicy']) && $options['cachePolicy'] instanceof CachePolicy) {
         $this->cachePolicy = $options['cachePolicy'];
         if (!$this->cache instanceof Cache && $this->cachePolicy->isEnabled()) {
             throw new \LogicException('You have supplied a cache policy that says cache is enabled but no cache provider have been defined.');
         }
     } else {
         $this->cachePolicy = new CachePolicy(false, 0);
         $this->logger->warning('No cache policy supplied! Without a cache policy no API queries will be cached.');
     }
     if (isset($options['verifyCertificates'])) {
         $this->verifyCertificates = (bool) $options['verifyCertificates'];
     } else {
         $this->verifyCertificates = true;
     }
 }