summaryrefslogtreecommitdiff
path: root/vendor/OAuth/ServiceFactory.php
blob: 2be9c463319370e751f2ac1a34b5ceafe0a504c1 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php

/**
 * OAuth service factory.
 *
 * PHP version 5.4
 *
 * @category   OAuth
 * @author     David Desberg <david@daviddesberg.com>
 * @author     Pieter Hordijk <info@pieterhordijk.com>
 * @copyright  Copyright (c) 2013 The authors
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 */

namespace OAuth;

use OAuth\Common\Service\ServiceInterface;
use OAuth\Common\Consumer\CredentialsInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Http\Client\StreamClient;
use OAuth\Common\Http\Uri\UriInterface;
use OAuth\Common\Exception\Exception;
use OAuth\OAuth1\Signature\Signature;

class ServiceFactory
{
    /**
     *@var ClientInterface
     */
    protected $httpClient;

    /**
     * @var array
     */
    protected $serviceClassMap = array(
        'OAuth1' => array(),
        'OAuth2' => array()
    );

    /**
     * @var array
     */
    protected $serviceBuilders = array(
        'OAuth2' => 'buildV2Service',
        'OAuth1' => 'buildV1Service',
    );

    /**
     * @param ClientInterface $httpClient
     *
     * @return ServiceFactory
     */
    public function setHttpClient(ClientInterface $httpClient)
    {
        $this->httpClient = $httpClient;

        return $this;
    }

    /**
     * Register a custom service to classname mapping.
     *
     * @param string $serviceName Name of the service
     * @param string $className   Class to instantiate
     *
     * @return ServiceFactory
     *
     * @throws Exception If the class is nonexistent or does not implement a valid ServiceInterface
     */
    public function registerService($serviceName, $className)
    {
        if (!class_exists($className)) {
            throw new Exception(sprintf('Service class %s does not exist.', $className));
        }

        $reflClass = new \ReflectionClass($className);

        foreach (array('OAuth2', 'OAuth1') as $version) {
            if ($reflClass->implementsInterface('OAuth\\' . $version . '\\Service\\ServiceInterface')) {
                $this->serviceClassMap[$version][ucfirst($serviceName)] = $className;

                return $this;
            }
        }

        throw new Exception(sprintf('Service class %s must implement ServiceInterface.', $className));
    }

    /**
     * Builds and returns oauth services
     *
     * It will first try to build an OAuth2 service and if none found it will try to build an OAuth1 service
     *
     * @param string                $serviceName Name of service to create
     * @param CredentialsInterface  $credentials
     * @param TokenStorageInterface $storage
     * @param array|null            $scopes      If creating an oauth2 service, array of scopes
     * @param UriInterface|null     $baseApiUri
     *
     * @return ServiceInterface
     */
    public function createService(
        $serviceName,
        CredentialsInterface $credentials,
        TokenStorageInterface $storage,
        $scopes = array(),
        UriInterface $baseApiUri = null
    ) {
        if (!$this->httpClient) {
            // for backwards compatibility.
            $this->httpClient = new StreamClient();
        }

        foreach ($this->serviceBuilders as $version => $buildMethod) {
            $fullyQualifiedServiceName = $this->getFullyQualifiedServiceName($serviceName, $version);

            if (class_exists($fullyQualifiedServiceName)) {
                return $this->$buildMethod($fullyQualifiedServiceName, $credentials, $storage, $scopes, $baseApiUri);
            }
        }

        return null;
    }

    /**
     * Gets the fully qualified name of the service
     *
     * @param string $serviceName The name of the service of which to get the fully qualified name
     * @param string $type        The type of the service to get (either OAuth1 or OAuth2)
     *
     * @return string The fully qualified name of the service
     */
    private function getFullyQualifiedServiceName($serviceName, $type)
    {
        $serviceName = ucfirst($serviceName);

        if (isset($this->serviceClassMap[$type][$serviceName])) {
            return $this->serviceClassMap[$type][$serviceName];
        }

        return '\\OAuth\\' . $type . '\\Service\\' . $serviceName;
    }

    /**
     * Builds v2 services
     *
     * @param string                $serviceName The fully qualified service name
     * @param CredentialsInterface  $credentials
     * @param TokenStorageInterface $storage
     * @param array|null            $scopes      Array of scopes for the service
     * @param UriInterface|null     $baseApiUri
     *
     * @return ServiceInterface
     *
     * @throws Exception
     */
    private function buildV2Service(
        $serviceName,
        CredentialsInterface $credentials,
        TokenStorageInterface $storage,
        array $scopes,
        UriInterface $baseApiUri = null
    ) {
        return new $serviceName(
            $credentials,
            $this->httpClient,
            $storage,
            $this->resolveScopes($serviceName, $scopes),
            $baseApiUri
        );
    }

    /**
     * Resolves scopes for v2 services
     *
     * @param string  $serviceName The fully qualified service name
     * @param array   $scopes      List of scopes for the service
     *
     * @return array List of resolved scopes
     */
    private function resolveScopes($serviceName, array $scopes)
    {
        $reflClass = new \ReflectionClass($serviceName);
        $constants = $reflClass->getConstants();

        $resolvedScopes = array();
        foreach ($scopes as $scope) {
            $key = strtoupper('SCOPE_' . $scope);

            if (array_key_exists($key, $constants)) {
                $resolvedScopes[] = $constants[$key];
            } else {
                $resolvedScopes[] = $scope;
            }
        }

        return $resolvedScopes;
    }

    /**
     * Builds v1 services
     *
     * @param string                $serviceName The fully qualified service name
     * @param CredentialsInterface  $credentials
     * @param TokenStorageInterface $storage
     * @param array                 $scopes
     * @param UriInterface          $baseApiUri
     *
     * @return ServiceInterface
     *
     * @throws Exception
     */
    private function buildV1Service(
        $serviceName,
        CredentialsInterface $credentials,
        TokenStorageInterface $storage,
        $scopes,
        UriInterface $baseApiUri = null
    ) {
        if (!empty($scopes)) {
            throw new Exception(
                'Scopes passed to ServiceFactory::createService but an OAuth1 service was requested.'
            );
        }

        return new $serviceName($credentials, $this->httpClient, $storage, new Signature($credentials), $baseApiUri);
    }
}