socialPostRepository = $socialPostRepository; $this->exportRepository = $exportRepository; $this->linkedin = $linkedin; $this->facebook = $facebook; $this->mediaProvider = $mediaProvider; $this->story = $story; $this->params = $params; } /** * @param int $exportId [description] * * @return array|null [description] */ public function save(array $post, Exports $export, ?SocialPost $socialPost) { try { $result = $this->publish($post['socialNetwork'], $post['message'], $post['mediasId']); if(!empty($socialPost)){ $entityPost = $socialPost; } else{ $entityPost = new SocialPost(); } $entityPost->setTitle($post['title']); $entityPost->setSocialNetwork($post['socialNetwork']); $entityPost->setMessage($post['message']); $entityPost->setHashtag($post['hashtag']); $dateEnd = null; if(!empty($post['dateEndPost'])){ $dateEnd = new \DateTime($post['dateEndPost']); } $entityPost->setDateEndPost($dateEnd); $dateBegin = null; if(!empty($post['dateBeginPost'])){ $dateBegin = new \DateTime($post['dateBeginPost']); } $entityPost->setDateBeginPost($dateBegin); $entityPost->setMediasId($post['mediasId']); $entityPost->setCanLike($post['canLike']); $entityPost->setCanComment($post['canComment']); $entityPost->setCanShare($post['canShare']); $entityPost->setPostDiffusion($post['postDiffusion']); $entityPost->setPostResponse($post['postResponse']); $entityPost->setExport($export); $entityPost = $this->socialPostRepository->save($entityPost); $postId = $entityPost->getId(); if(isset($postId)){ $this->story->execute('post',['id' => $postId, 'title'=> $entityPost->getTitle()], $postId,"SocialPost", 0); } } catch (Exception $e) { $result = $e; } return $result; } private function publish(array $socials, string $message, array $medias) { $result = []; if(in_array('linkedin', $socials)){ $result['linkedin'] = $this->publishLinkedin($message, $medias); } if(in_array('instagram', $socials)){ $result['instagram'] = $this->instagram(); } /*if(in_array('facebook', $socials)){ $result['facebook'] = $this->publishFacebook($message, $medias); }*/ $result; } private function publishLinkedin(string $message, array $medias) { $result = "success"; $info = $this->linkedin->get(); if($info instanceof Exception) { $result = "failed"; } else{ $filePath = []; if(!empty($medias)){ $mediasId = []; foreach ($medias as $media) { $mediasId[] = $media['id']; } $filePath = $this->createFileToPost($mediasId); } $post = $this->linkedin->post($info, $message, $filePath); if($post instanceof Exception) { $result = "failed"; } } return $result; } private function instagram() { $cachePool = new FilesystemAdapter('Instagram', 0,$this->params->get('arborescence_base') . '/var/cache'); $api = new Api($cachePool); $api->login($this->params->get('instagram_login'), $this->params->get('instagram_password')); /*$profile = $api->getProfile('lordrak972'); $profile->getMedias(); // 12 first medias do { $profile = $api->getMoreMedias($profile); // $profile->getMedias()); // 12 more medias // avoid 429 Rate limit from Instagram sleep(1); } while ($profile->hasMoreMedias()); $profile = $api->getProfile('starwars'); // we need instagram username sleep(1); $feedStories = $api->getStories($profile->getId()); $stories = $feedStories->getStories(); $stories;*/ } private function publishFacebook(string $message, array $medias) { $mediasId = []; foreach ($medias as $media) { $mediasId[] = $media['id']; } $filePath = $this->createFileToPost($mediasId); $this->facebook->getToken(); } private function createFileToPost($medias) { $filePath = []; $files = $this->mediaProvider->collectMediaByIds(['medias' => $medias, "def" => "bd"]); if(!$files instanceof Exception && !empty($files)){ foreach ($files as $file) { # code... $data = base64_decode($file['file']); $path = $this->params->get('arborescence_base') . '/var/cache/' . $file['originalName']; file_put_contents($path, $data); $filePath[] = $path; } } return $filePath; } }