/**
  * @throws BadAuthenticationException
  */
 public function authorize()
 {
     if (!$this->sessionContainer->offsetExists('data')) {
         throw new BadAuthenticationException('You must be first authenticated');
     }
     $this->instagramWrapper->setAccessToken($this->sessionContainer->data);
 }
 /**
  * @param MvcEvent $e
  * @return mixed|void
  */
 public function onDispatch(MvcEvent $e)
 {
     $viewModel = new ViewModel();
     $viewModel->setVariable('loginUri', $this->instagramWrapper->getLoginUrl());
     $viewModel->setTemplate('frontend/index');
     $e->setResult($viewModel);
 }
 /**
  * @param string $code
  * @throws InvalidDataException | BadAuthenticationException | InstagramException
  * @return bool
  */
 public function authenticate($code)
 {
     if (!$this->codeValidator->isValid($code)) {
         throw new InvalidCodeException('Invalid code param');
     }
     $data = $this->instagramWrapper->getOAuthToken($code);
     if (!empty($data->error_message)) {
         throw new BadAuthenticationException('Could not connect to the Instagram');
     }
     $this->sessionContainer->data = $data;
 }
 /**
  * @param LimitHexQualityInterface $limitHexQualityData
  *
  * @return array
  */
 protected function searchForImagesByHash(LimitHexQualityInterface $limitHexQualityData)
 {
     $rgb = $this->hex2rgb($limitHexQualityData->getHex());
     $images = array();
     $getLimit = $limitHexQualityData->getLimit() * 3;
     $fetchAll = function ($getLimit, &$fetchedImages = null) use(&$fetchAll) {
         if (is_null($fetchedImages)) {
             $images = $this->fetch($getLimit);
         } else {
             $images = $this->instagramWrapper->pagination($fetchedImages, $getLimit);
         }
         if (!is_null($images) || !empty($images) && !empty($images->meta) && $images->meta->code == '200') {
             if (is_null($fetchedImages)) {
                 $fetchedImages = $images;
             } else {
                 $fetchedImages->pagination = $images->pagination;
                 $fetchedImages->data = array_merge($fetchedImages->data, $images->data);
             }
         } else {
             return $fetchedImages;
         }
         if (count($fetchedImages->data) < $getLimit && !empty($images->pagination)) {
             return $fetchAll($getLimit, $fetchedImages);
         }
         return $fetchedImages;
     };
     $quality = $limitHexQualityData->getQuality();
     $count = 0;
     $rec = function ($limit, $rgb, $fetchedImages) use(&$rec, &$images, $quality, &$count, $fetchAll, $getLimit) {
         if (is_object($fetchedImages) && !empty($fetchedImages->data) && is_array($fetchedImages->data)) {
             $sorted = [];
             foreach ($fetchedImages->data as $item) {
                 $count++;
                 $diff = $this->colorDiff($rgb, ColorThief::getColor($item->images->thumbnail->url, 1));
                 if ($item->type != 'image' || $diff >= 300 + ceil($count / 10) * 100) {
                     continue;
                 }
                 $sorted[] = ['i' => $item->images->{$quality}, 'diff' => $diff];
             }
             usort($sorted, function ($a, $b) {
                 if ($a['diff'] == $b['diff']) {
                     return 0;
                 }
                 return $a['diff'] < $b['diff'] ? -1 : 1;
             });
             foreach ($sorted as $item) {
                 if (count($images) >= $limit) {
                     return null;
                 }
                 $images[] = $item;
             }
             $count = 0;
             unset($fetchedImages->data);
             return $rec($limit, $rgb, $fetchAll($getLimit, $fetchedImages));
         }
         return null;
     };
     $rec($limitHexQualityData->getLimit(), $rgb, $fetchAll($getLimit));
     usort($images, function ($a, $b) {
         if ($a['diff'] == $b['diff']) {
             return 0;
         }
         return $a['diff'] < $b['diff'] ? -1 : 1;
     });
     return array_map(function ($item) {
         return $item['i'];
     }, $images);
 }