/
TwitterServiceFactory.php
85 lines (69 loc) · 2.73 KB
/
TwitterServiceFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Concrete\Core\Authentication\Type\Twitter\Factory;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\Http\Request;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Storage\SymfonySession;
use OAuth\ServiceFactory;
use Symfony\Component\HttpFoundation\Session\Session;
class TwitterServiceFactory implements ApplicationAwareInterface
{
use ApplicationAwareTrait;
/**
* @var \Concrete\Core\Config\Repository\Repository
*/
protected $config;
/**
* @var \Symfony\Component\HttpFoundation\Session\Session
*/
protected $session;
/**
* @var \Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface
*/
protected $urlResolver;
/**
* @var \Concrete\Core\Http\Request
*/
private $request;
/**
* CommunityServiceFactory constructor.
* @param \Concrete\Core\Config\Repository\Repository $config
* @param \Symfony\Component\HttpFoundation\Session\Session $session
* @param \Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface $url
* @param \Concrete\Core\Http\Request $request
*/
public function __construct(Repository $config, Session $session, ResolverManagerInterface $url, Request $request)
{
$this->config = $config;
$this->session = $session;
$this->urlResolver = $url;
$this->request = $request;
}
/**
* Create a service object given a ServiceFactory object
*
* @return \OAuth\Common\Service\ServiceInterface
*/
public function createService()
{
$appId = $this->config->get('auth.twitter.appid');
$appSecret = $this->config->get('auth.twitter.secret');
/** @var ServiceFactory $factory */
$factory = $this->app->make('oauth/factory/service');
// Get the callback url
$callbackUrl = $this->urlResolver->resolve(['/ccm/system/authentication/oauth2/twitter/callback/']);
if ($callbackUrl->getHost() == '') {
$callbackUrl = $callbackUrl->setHost($this->request->getHost());
$callbackUrl = $callbackUrl->setScheme($this->request->getScheme());
}
// Create a credential object with our ID, Secret, and callback url
$credentials = new Credentials($appId, $appSecret, (string)$callbackUrl);
// Create a new session storage object and pass it the active session
$storage = new SymfonySession($this->session, false);
// Create the service using the oauth service factory
return $factory->createService('twitter', $credentials, $storage);
}
}