/**
  * @param $resourceOwner
  * @param $applicationInfo
  * @param bool $sameToken
  *
  * @throws \Exception
  */
 public function authenticate($resourceOwner, $applicationInfo, $sameToken = false)
 {
     // Get application credentials
     $oauthApp = $this->container->get('campaignchain.security.authentication.client.oauth.application');
     $application = $oauthApp->getApplication($resourceOwner);
     $bundleParams = $this->container->getParameter('campaignchain_security_authentication_client_oauth');
     if (isset($_SERVER['HTTPS']) && (!empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'off')) {
         $hostScheme = 'https://';
     } else {
         $hostScheme = 'http://';
     }
     $config = array("base_url" => $hostScheme . $_SERVER['HTTP_HOST'] . $this->container->get('router')->generate('campaignchain_security_authentication_client_oauth_login'), "providers" => array($resourceOwner => array("enabled" => true, "keys" => array($applicationInfo['key_labels'][0] => $application->getKey(), $applicationInfo['secret_labels'][0] => $application->getSecret()))), "debug_mode" => $bundleParams['debug_mode'], "debug_file" => $bundleParams['debug_file']);
     $config['providers'][$resourceOwner] = array_merge($config['providers'][$resourceOwner], $applicationInfo['parameters']);
     if (isset($applicationInfo['wrapper'])) {
         $config['providers'][$resourceOwner]['wrapper']['class'] = $applicationInfo['wrapper']['class'];
         $config['providers'][$resourceOwner]['wrapper']['path'] = $this->container->get('kernel')->getRootDir() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $applicationInfo['wrapper']['path']);
     }
     try {
         $hybridauth = new Hybrid_Auth($config);
         $resource = $hybridauth->authenticate($resourceOwner);
         $this->profile = $resource->getUserProfile();
         $accessToken = $resource->getAccessToken();
         $hybridauth->logoutAllProviders();
         $token = new Token();
         $token->setApplication($application);
         $token->setAccessToken($accessToken["access_token"]);
         $token->setTokenSecret($accessToken["access_token_secret"]);
         $token->setRefreshToken($accessToken["refresh_token"]);
         $token->setExpiresIn($accessToken["expires_in"]);
         $token->setExpiresAt($accessToken["expires_at"]);
         if (isset($resource->adapter->api->api_base_url)) {
             $token->setEndpoint($resource->adapter->api->api_base_url);
         }
         if (isset($applicationInfo['parameters']['scope'])) {
             $token->setScope($applicationInfo['parameters']['scope']);
         }
         $this->oauthToken = $this->container->get('campaignchain.security.authentication.client.oauth.token');
         return $this->oauthToken->setToken($token, $sameToken);
     } catch (\Exception $e) {
         throw new ExternalApiException($e->getMessage(), $e->getCode(), $e);
     }
 }
 /**
  * @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;
 }