Skip to content

Instantly share code, notes, and snippets.

@jerronimo
Created November 20, 2017 13:57
Show Gist options
  • Save jerronimo/6c664c5819db20b3aa0cd70be820cee8 to your computer and use it in GitHub Desktop.
Save jerronimo/6c664c5819db20b3aa0cd70be820cee8 to your computer and use it in GitHub Desktop.
<?php
namespace Acted\ProfileBundle\Services;
use Acted\CoreBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Abraham\TwitterOAuth\TwitterOAuth;
use Acted\CoreBundle\Entity\UserOAuth;
use Facebook\Facebook;
use MetzWeb\Instagram\Instagram;
use Facebook\Helpers\FacebookRedirectLoginHelper;
use Symfony\Component\HttpFoundation\Request;
/**
* Class SocialMediaManager
* @package Acted\ProfileBundle\Services
*/
class SocialMediaManager
{
const INSTAGRAM_MEDIA_PER_PAGE_NUMBER = 20;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var Session
*/
protected $session;
/**
* @var string $twitterAccessToken
*/
protected $twitterAccessToken;
/**
* @var string $twitterAccessTokenSecret
*/
protected $twitterAccessTokenSecret;
/**
* @var string $twitterConsumerKey
*/
protected $twitterConsumerKey;
/**
* @var string $twitterConsumerSecret
*/
protected $twitterConsumerSecret;
/**
* @var string $twitterCallbackUrl
*/
protected $twitterCallbackUrl;
/**
* @var string $fbAppId
*/
protected $fbAppId;
/**
* @var string $fbAppSecret
*/
protected $fbAppSecret;
/**
* @var string $instagramClientid
*/
protected $instagramClientId;
/**
* @var string $instagramClientSecret
*/
protected $instagramClientSecret;
/**
* @var string $instagramRedirectUri
*/
protected $instagramRedirectUri;
/**
* SocialMediaManager constructor.
* @param EntityManagerInterface $entityManagerInterface
* @param Session $session
* @param string $twitterAccessToken
* @param string $twitterAccessTokenSecret
* @param string $twitterConsumerKey
* @param string $twitterConsumerSecret
* @param string $twitterCallbackUrl
* @param string $fbAppId
* @param string $fbAppSecret
* @param string $instagramClientId
* @param string $instagramClientSecret
* @param string $instagramRedirectUri
*/
public function __construct(
EntityManagerInterface $entityManagerInterface,
Session $session,
$twitterAccessToken,
$twitterAccessTokenSecret,
$twitterConsumerKey,
$twitterConsumerSecret,
$twitterCallbackUrl,
$fbAppId,
$fbAppSecret,
$instagramClientId,
$instagramClientSecret,
$instagramRedirectUri
)
{
$this->entityManager = $entityManagerInterface;
$this->session = $session;
$this->twitterAccessToken = $twitterAccessToken;
$this->twitterAccessTokenSecret = $twitterAccessTokenSecret;
$this->twitterConsumerKey = $twitterConsumerKey;
$this->twitterConsumerSecret = $twitterConsumerSecret;
$this->twitterCallbackUrl = $twitterCallbackUrl;
$this->fbAppId = $fbAppId;
$this->fbAppSecret = $fbAppSecret;
$this->instagramClientId = $instagramClientId;
$this->instagramClientSecret = $instagramClientSecret;
$this->instagramRedirectUri = $instagramRedirectUri;
}
public function getTwitterLoginUrl()
{
$twitteroauth = new TwitterOAuth($this->twitterConsumerKey, $this->twitterConsumerSecret,
$this->twitterAccessToken, $this->twitterAccessTokenSecret);
$request_token = $twitteroauth->oauth(
'oauth/request_token', [
'oauth_callback' => $this->twitterCallbackUrl,
]
);
$twitterUrl = $twitteroauth->url(
'oauth/authorize', [
'oauth_token' => $request_token['oauth_token']
]
);
$this->session->set('oauth_token', $request_token['oauth_token']);
$this->session->set('oauth_token_secret', $request_token['oauth_token_secret']);
return $twitterUrl;
}
/**
* @param Request $request
* @param User $user
* @throws \Abraham\TwitterOAuth\TwitterOAuthException
*/
public function getTwitterCallback(Request $request, User $user)
{
$connection = new TwitterOAuth($this->twitterConsumerKey, $this->twitterConsumerSecret,
$this->session->get('oauth_token'), $this->session->get('oauth_token_secret'));
$oauth_verifier = $request->query->get('oauth_verifier');
$token = $connection->oauth(
'oauth/access_token', [
'oauth_verifier' => $oauth_verifier
]
);
$userOAuth = $this->entityManager->getRepository('ActedCoreBundle:UserOAuth')->findOneBy([
'user' => $user->getArtist()->getUser()
]);
if ($userOAuth === null) {
$userOAuth = new UserOAuth();
$userOAuth->setActiveTwitter(true);
$userOAuth->setTwitterId($token['user_id']);
$userOAuth->setUser($user);
$userOAuth->setTwitterScreenName($token['screen_name']);
$userOAuth->setTwitterOauthToken($token['oauth_token']);
$userOAuth->setOauthTokenSecret($token['oauth_token_secret']);
} else {
$userOAuth->setTwitterId($token['user_id']);
$userOAuth->setUser($user);
$userOAuth->setActiveTwitter(true);
$userOAuth->setTwitterScreenName($token['screen_name']);
$userOAuth->setTwitterOauthToken($token['oauth_token']);
$userOAuth->setOauthTokenSecret($token['oauth_token_secret']);
}
$this->entityManager->persist($userOAuth);
$this->entityManager->flush();
}
public function getInstagramLoginUrl()
{
$instagram = new Instagram(array(
'apiKey' => $this->instagramClientId,
'apiSecret' => $this->instagramClientSecret,
'apiCallback' => $this->instagramRedirectUri
));
return $instagram->getLoginUrl(['basic']);
}
/**
* @param string $instagramToken
* @return mixed
*/
public function getInstagramUserMedia($instagramToken)
{
$instagram = new Instagram(array(
'apiKey' => $this->instagramClientId,
'apiSecret' => $this->instagramClientSecret,
'apiCallback' => $this->instagramRedirectUri
));
$instagram->setAccessToken($instagramToken);
$instagramMedia = $instagram->getUserMedia('self', SocialMediaManager::INSTAGRAM_MEDIA_PER_PAGE_NUMBER);
return $instagramMedia;
}
/**
* @param string $instagramToken
* @return mixed
*/
public function getInstagramUser($instagramToken)
{
$instagram = new Instagram(array(
'apiKey' => $this->instagramClientId,
'apiSecret' => $this->instagramClientSecret,
'apiCallback' => $this->instagramRedirectUri
));
$instagram->setAccessToken($instagramToken);
$instagramUser = $instagram->getUser();
return $instagramUser;
}
public function checkFacebookPage()
{
$facebook = new Facebook([
'app_id' => $this->fbAppId,
'app_secret' => $this->fbAppSecret,
]);
$signed_request = $facebook->getDefaultAccessToken();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment