/**
  * @param Url $url
  *
  * @return PharIoRepository
  * @throws DownloadFailedException
  */
 public function getRepository(Url $url)
 {
     $repositoryXml = $this->downloader->download($url);
     $filename = new Filename(tempnam('/tmp', 'repo_'));
     $repositoryXml->saveAs($filename);
     return new PharIoRepository($filename);
 }
 /**
  * @param Release $release
  *
  * @return File
  * @throws DownloadFailedException
  * @throws VerificationFailedException
  */
 public function download(Release $release)
 {
     $pharFile = $this->fileDownloader->download($release->getUrl());
     $signatureFile = $this->fileDownloader->download($this->getSignatureUrl($release->getUrl()));
     if (!$this->verifySignature($pharFile, $signatureFile)) {
         throw new VerificationFailedException('Signature could not be verified');
     }
     if ($release->hasExpectedHash() && !$this->checksumService->verify($release->getExpectedHash(), $pharFile)) {
         throw new VerificationFailedException(sprintf('Wrong checksum! Expected %s', $release->getExpectedHash()->asString()));
     }
     return $pharFile;
 }
 /**
  * @return FileDownloaderInterface
  */
 public function getFileDownloader()
 {
     if (!$this->fileDownloader instanceof FileDownloaderInterface) {
         $this->fileDownloader = FileDownloader::getInstance();
     }
     return $this->fileDownloader;
 }
 /**
  * @throws DownloadFailedException
  */
 public function downloadFromSource()
 {
     $this->output->writeInfo(sprintf('Downloading repository list from %s', $this->sourceUrl));
     $file = $this->fileDownloader->download($this->sourceUrl);
     $file->saveAs($this->filename);
 }
 public function manageMessages($entity)
 {
     $requester = new Requester();
     $publisher = new Publisher();
     $entityObject = new Entity();
     $downloader = new FileDownloader();
     $published = new Published();
     $summary = '';
     $title = '';
     $readmoreLabel = '';
     // get author id
     $author = $entity->author_id;
     // The entity is not active
     if (!$entity->activate) {
         return 0;
     }
     // Retrieve the entity categories
     $entityListCategory = [];
     $readmoreLabel = '';
     foreach ($entity->options as $key => $value) {
         if ($value->options_id == 1) {
             $entityListCategory[] = $value->value;
         }
         if ($value->options_id == 2) {
             $entityDisplayType = $value->value;
         }
         if ($value->options_id == 3) {
             $entityPublishType = $value->value;
         }
         if ($value->options_id == 5) {
             $entityImage = $value->value;
         }
         if ($value->options_id == 6) {
             $readmoreLabel = $value->value;
         }
     }
     // Try request to sociallymap on response
     try {
         if ($_ENV['ENVIRONNEMENT'] === 'dev') {
             $requester = new MockRequester();
             $jsonData = $requester->getMessages();
         } else {
             $_POST['entityId'] = esc_html($_POST['entityId']);
             $_POST['token'] = esc_html($_POST['token']);
             $_POST['environment'] = esc_html($_POST['environment']);
             $jsonData = $requester->launch($_POST['entityId'], $_POST['token'], $_POST['environment']);
         }
         if (empty($jsonData)) {
             throw new Exception('No data returned from request', 1);
             exit;
         }
         Logger::messageReceive('See return data', $jsonData);
         foreach ($jsonData as $key => $value) {
             $summary = '';
             $contentArticle = '';
             $readmore = '';
             // Check Link object existing
             if (isset($value->link)) {
                 // Check if Title existing
                 if (!empty($value->link->title)) {
                     $title = $value->link->title;
                 }
                 if (!empty($value->link->summary)) {
                     $summary = $value->link->summary;
                 }
                 // Check if Link URL existing
                 if (!empty($value->link->url)) {
                     $readmoreLabel = stripslashes($readmoreLabel);
                     $readmore = $this->templater->loadReadMore($value->link->url, $entityDisplayType, $entity->id, $readmoreLabel);
                 } else {
                     Logger::alert('This article not contain url');
                 }
             }
             $contentArticle = $summary;
             // add readmore to content if $readmore is not empty
             if ($readmore != '') {
                 $contentArticle .= $readmore;
             }
             $imageTag = '';
             $imageSrc = '';
             // Check if article posted
             $canBePublish = true;
             $messageId = $value->guid;
             if ($published->isPublished($messageId)) {
                 $contextMessageId = '(id message=' . $messageId . ')';
                 Logger::alert('Message of sociallymap existing, so he is not publish', $contextMessageId);
                 $canBePublish = false;
                 continue;
             }
             $pathTempory = plugin_dir_path(__FILE__) . 'tmp/';
             // Check if Media object exist
             if (isset($value->media) && $value->media->type == 'photo') {
                 try {
                     $returnDownload = $downloader->download($value->media->url, $pathTempory);
                     $filename = $returnDownload['filename'];
                     $fileExtension = $returnDownload['extension'];
                     $mediaManager = new MediaWordpressManager();
                     $imageSrc = $mediaManager->integrateMediaToWordpress($filename, $fileExtension);
                 } catch (fileDownloadException $e) {
                     Logger::error('error download' . $e);
                 }
                 // WHEN NO ERROR : FORMAT
                 if (gettype($imageSrc) == 'string') {
                     $imageTag = '<img class="aligncenter" src="' . $imageSrc . '" alt="">';
                 } else {
                     $imageTag = '';
                 }
             } elseif (isset($value->link) && !empty($value->link->thumbnail) && $value->link->thumbnail != ' ') {
                 // Check if Image thumbnail existing
                 try {
                     $returnDownload = $downloader->download($value->link->thumbnail, $pathTempory);
                     $filename = $returnDownload['filename'];
                     $fileExtension = $returnDownload['extension'];
                 } catch (fileDownloadException $e) {
                     Logger::error('error download' . $e);
                 }
                 $mediaManager = new MediaWordpressManager();
                 $imageSrc = $mediaManager->integrateMediaToWordpress($filename, $fileExtension);
                 // Create the img tag
                 if (gettype($imageSrc) == 'string') {
                     $imageTag = '<img class="aligncenter" src="' . $imageSrc . '" alt="">';
                 } else {
                     $imageTag = '';
                 }
             }
             // check if video exist
             $downloadVideo = false;
             if (isset($value->media) && $value->media->type == 'video') {
                 $returnDownload = $downloader->download($value->media->url, $pathTempory);
                 $filename = $returnDownload['filename'];
                 $fileExtension = $returnDownload['extension'];
                 $mediaManager = new MediaWordpressManager();
                 $videoSrc = $mediaManager->integrateMediaToWordpress($filename, $fileExtension);
                 $mediaVideo = '<video class="sm-video-display" controls>
                 <source src="' . $videoSrc . '" type="video/mp4">
                 <div class="sm-video-nosupport"></div>
                 </video>';
                 $contentArticle .= $mediaVideo;
                 Logger::info('download VIDEO', $videoSrc);
             }
             // If imageTag is '' so is false else $isDownload is true
             $isDownloaded = $imageTag !== '';
             // Attach image accordingly to options
             $imageAttachment = '';
             if ($isDownloaded) {
                 // Add image in the post content
                 if (in_array($entityImage, ['content', 'both'])) {
                     $contentArticle = $imageTag . $contentArticle;
                 }
                 // Add image as featured image
                 if (in_array($entityImage, ['thumbnail', 'both'])) {
                     $imageAttachment = $imageSrc;
                 }
             }
             // Publish the post
             $title = $value->content;
             if ($canBePublish == true) {
                 $dataPublish = [$title, $contentArticle, $author, $imageAttachment, $entityListCategory, $entityPublishType];
                 Logger::info('Try publish : ', $dataPublish);
                 $contentArticle = $this->prePosting($contentArticle);
                 $articlePublished = $publisher->publish($title, $contentArticle, $author, $imageAttachment, $entityListCategory, $entityPublishType);
                 if (!$articlePublished) {
                     // throw new Exception('Error from post publish', 1);
                     Logger::error('Error from post publish', [$title]);
                 } else {
                     $entityObject->updateHistoryPublisher($entity->id, $entity->counter);
                     // save published article
                     $published->add($messageId, $entity->id, $articlePublished);
                 }
             }
         }
     } catch (Exception $e) {
         Logger::alert('Error exeption', $e->getMessage());
     }
     return true;
 }