/**
  * @param Operation $operation
  * @return \Symfony\Component\HttpFoundation\Response
  * @throws \Exception
  */
 public function readAction(Operation $operation)
 {
     $newsItem = $this->contentService->getNewsItemByOperation($operation);
     $activity = $operation->getActivity();
     $locationModuleIdentifier = $activity->getLocation()->getLocationModule()->getIdentifier();
     $isCompanyPageShare = 'campaignchain-linkedin-page' == $locationModuleIdentifier;
     $isLive = true;
     if (!$newsItem->getLinkedinData()) {
         $connection = $this->restClient->getConnectionByActivity($activity);
         /*
          * Below calls to the Linkedin REST API will throw errors if the
          * Linkedin App has not been approved by Linkedin as part of their
          * partner program.
          */
         try {
             if ($isCompanyPageShare) {
                 $response = $connection->getCompanyUpdate($activity, $newsItem);
             } else {
                 $response = $connection->getUserUpdate($activity, $newsItem);
             }
             if (!is_null($response)) {
                 $newsItem->setLinkedinData($response);
                 $this->em->persist($newsItem);
                 $this->em->flush();
             } else {
                 $isLive = false;
             }
         } catch (\Exception $e) {
             $isLive = true;
         }
     }
     return $this->templating->renderResponse('CampaignChainOperationLinkedInBundle::read.html.twig', array('page_title' => $activity->getName(), 'news_item' => $newsItem, 'activity' => $activity, 'is_live' => $isLive, 'is_company' => $isCompanyPageShare));
 }
 /**
  * @param string $operationId
  * @return string
  * @throws \Exception
  */
 public function execute($operationId)
 {
     /** @var NewsItem $newsItem */
     $newsItem = $this->em->getRepository('CampaignChainOperationLinkedInBundle:NewsItem')->findOneByOperation($operationId);
     if (!$newsItem) {
         throw new \Exception('No news item found for an operation with ID: ' . $operationId);
     }
     //have images?
     $images = $this->em->getRepository('CampaignChainHookImageBundle:Image')->getImagesForOperation($newsItem->getOperation());
     // if the message does not contain a url, we need to skip the content block
     if (is_null($newsItem->getLinkUrl())) {
         // LinkedIn accepts an image link only if we also provided a submitted URL.
         if ($images) {
             throw new \Exception('To include an image, you must also provide a URL in the text message.');
             return self::STATUS_WARNING;
         }
         $content = ['comment' => $newsItem->getMessage(), 'visibility' => ['code' => 'anyone']];
     } else {
         /*
          * process urls and add tracking
          * important: both the urls in the message and submitted url field must be identical
          */
         $newsItem->setLinkUrl($this->ctaService->processCTAs($newsItem->getLinkUrl(), $newsItem->getOperation())->getContent());
         $newsItem->setMessage($this->ctaService->processCTAs($newsItem->getMessage(), $newsItem->getOperation())->getContent());
         $content = ['comment' => $newsItem->getMessage(), 'content' => ['title' => $newsItem->getLinkTitle(), 'description' => $newsItem->getLinkDescription(), 'submitted-url' => $newsItem->getLinkUrl()], 'visibility' => ['code' => 'anyone']];
         if ($images) {
             //Linkedin can handle only 1 image
             $content['content']['submitted-image-url'] = $this->cacheManager->getBrowserPath($images[0]->getPath(), "campaignchain_linkedin_news_item");
         }
     }
     $activity = $newsItem->getOperation()->getActivity();
     $locationModuleIdentifier = $activity->getLocation()->getLocationModule()->getIdentifier();
     $isCompanyPageShare = 'campaignchain-linkedin-page' == $locationModuleIdentifier;
     $connection = $this->client->getConnectionByActivity($activity);
     if ($isCompanyPageShare) {
         $response = $connection->shareOnCompanyPage($activity, $content);
     } else {
         $response = $connection->shareOnUserPage($content);
     }
     $newsItem->setUrl($response['updateUrl']);
     $newsItem->setUpdateKey($response['updateKey']);
     // Set Operation to closed.
     $newsItem->getOperation()->setStatus(Action::STATUS_CLOSED);
     $location = $newsItem->getOperation()->getLocations()[0];
     $location->setIdentifier($response['updateKey']);
     $location->setUrl($response['updateUrl']);
     $location->setName($newsItem->getOperation()->getName());
     $location->setStatus(Medium::STATUS_ACTIVE);
     // Schedule data collection for report
     $this->reportShareNewsItem->schedule($newsItem->getOperation());
     $this->em->flush();
     $this->message = 'The message "' . $newsItem->getMessage() . '" with the ID "' . $newsItem->getUpdateKey() . '" has been posted on LinkedIn. See it on LinkedIn: <a href="' . $newsItem->getUrl() . '">' . $newsItem->getUrl() . '</a>';
     return self::STATUS_OK;
 }
 /**
  * @param string $operationId
  * @return string
  * @throws \Exception
  */
 public function execute($operationId)
 {
     $this->newsitem = $this->em->getRepository('CampaignChainOperationLinkedInBundle:NewsItem')->findOneByOperation($operationId);
     if (!$this->newsitem) {
         throw new \Exception('No Linkedin news item found for an operation with ID: ' . $operationId);
     }
     $activity = $this->newsitem->getOperation()->getActivity();
     $connection = $this->client->getConnectionByActivity($activity);
     $this->message = $connection->getCompanyUpdate($activity, $this->newsitem);
     $likes = 0;
     if (isset($response['numLikes'])) {
         $likes = $response['numLikes'];
     }
     $comments = 0;
     if (isset($response['updateComments']) && isset($response['updateComments']['_total'])) {
         $comments = $response['updateComments']['_total'];
     }
     // Add report data.
     $facts[self::METRIC_LIKES] = $likes;
     $facts[self::METRIC_COMMENTS] = $comments;
     $this->factService->addFacts('activity', self::BUNDLE_NAME, $this->newsitem->getOperation(), $facts);
     return self::STATUS_OK;
 }
 /**
  * @return Location[]
  */
 public function getParsedLocationsFromLinkedIn()
 {
     $channel = $this->channelWizard->getChannel();
     $profile = $this->channelWizard->get('profile');
     $locations = [];
     $locationName = $profile->displayName;
     if (!empty($profile->username)) {
         $locationName .= ' (' . $profile->username . ')';
     }
     // Get the location module for the user stream.
     $locationModuleUser = $this->locationService->getLocationModule('campaignchain/location-linkedin', 'campaignchain-linkedin-user');
     // Create the location instance for the user stream.
     $locationUser = new Location();
     $locationUser->setIdentifier($profile->identifier);
     $locationUser->setName($locationName);
     $locationUser->setLocationModule($locationModuleUser);
     if (!$profile->photoURL || strlen($profile->photoURL) == 0) {
         $locationUser->setImage($this->assetsHelper->getUrl('/bundles/campaignchainchannellinkedin/ghost_person.png'));
     } else {
         $locationUser->setImage($profile->photoURL);
     }
     $locationUser->setChannel($channel);
     $locationModuleUser->addLocation($locationUser);
     $locations[$profile->identifier] = $locationUser;
     $tokens = $this->channelWizard->get('tokens');
     /** @var Token $userToken */
     $userToken = array_values($tokens)[0];
     $connection = $this->client->getConnectionByToken($userToken);
     $companies = $connection->getCompanies();
     //there is only a user page
     if (empty($companies)) {
         return $locations;
     }
     // Get the location module for the page stream.
     $locationModulePage = $this->locationService->getLocationModule('campaignchain/location-linkedin', 'campaignchain-linkedin-page');
     $wizardPages = [];
     foreach ($companies as $company) {
         $newToken = new Token();
         $newToken->setAccessToken($userToken->getAccessToken());
         $newToken->setApplication($userToken->getApplication());
         $newToken->setTokenSecret($userToken->getTokenSecret());
         $tokens[$company['id']] = $newToken;
         $this->channelWizard->set('tokens', $tokens);
         $companyData = $connection->getCompanyProfile($company['id']);
         $locationPage = new Location();
         $locationPage->setChannel($channel);
         $locationPage->setName($companyData['name']);
         $locationPage->setIdentifier($companyData['id']);
         if (isset($companyData['squareLogoUrl'])) {
             $locationPage->setImage($companyData['squareLogoUrl']);
         } else {
             $locationPage->setImage($this->assetsHelper->getUrl('/bundles/campaignchainchannellinkedin/ghost_person.png'));
         }
         $locationPage->setLocationModule($locationModulePage);
         $locationModulePage->addLocation($locationPage);
         $locations[$companyData['id']] = $locationPage;
         $wizardPages[$companyData['id']] = $companyData;
     }
     $this->channelWizard->set('pagesData', $wizardPages);
     return $locations;
 }