1: <?php
2:
3: namespace Authoritarian\Flow;
4:
5: use Authoritarian\Credential\ClientCredential;
6: use Authoritarian\Exception\Flow\MissingTokenUrlException;
7: use Authoritarian\Exception\Flow\MissingClientCredentialException;
8:
9: /**
10: * Authorization Flow interface to generate Access Token Requests
11: */
12: abstract class AbstractFlow
13: {
14: protected $client;
15: protected $tokenUrl;
16: protected $clientId;
17: protected $clientSecret;
18: protected $scope;
19:
20: /**
21: * @param \Guzzle\Http\ClientInterface $client
22: */
23: public function setHttpClient(\Guzzle\Http\ClientInterface $client)
24: {
25: $this->client = $client;
26: }
27:
28: /**
29: * @param string $token_url The URL to request the Access Token
30: */
31: public function setTokenUrl($token_url)
32: {
33: $this->tokenUrl = $token_url;
34: }
35:
36: /**
37: * @param string $client_id The app's client id
38: * @param string $client_secret The app's client secret
39: */
40: public function setClientCredential($client_id, $client_secret)
41: {
42: $this->clientId = $client_id;
43: $this->clientSecret = $client_secret;
44: }
45:
46: /**
47: * @param string $scope The scope the app is requiring access
48: */
49: public function setScope($scope)
50: {
51: $this->scope = $scope;
52: }
53:
54: /**
55: * Get the request to the Access Token
56: *
57: * @throws MissingTokenUrlException When the OAuth token URL wasn't set
58: * @throws MissingClientCredentialException When the app's client
59: * credentials wasn't set
60: *
61: * @return \Guzzle\Http\Message\RequestInterface
62: */
63: public function getRequest() {
64: if (is_null($this->tokenUrl)) {
65: throw new MissingTokenUrlException(
66: 'No OAuth token URL given to generate a request'
67: );
68: }
69:
70: if (is_null($this->clientId) || is_null($this->clientSecret)) {
71: throw new MissingClientCredentialException(
72: 'No Client Id or Client Secret given to generate a request'
73: );
74: }
75: }
76:
77: protected function removeNullItems(array $parameters)
78: {
79: return array_filter(
80: $parameters,
81: function ($item) {
82: return !is_null($item);
83: }
84: );
85: }
86: }
87:
88: