ConnectorFactory::getFlatConfig()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 8.439
cc 5
eloc 11
nc 16
nop 1
1
<?php
2
namespace Borfast\Socializr\Connectors;
3
4
use OAuth\Common\Http\Uri\Uri;
5
use OAuth\Common\Storage\TokenStorageInterface;
6
use OAuth\Common\Http\Client\ClientInterface;
7
use OAuth\ServiceFactory;
8
use OAuth\Common\Consumer\CredentialsInterface;
9
use OAuth\Common\Http\Client\CurlClient;
10
use OAuth\Common\Consumer\Credentials;
11
use OAuth\Common\Service\ServiceInterface;
12
13
use Borfast\Socializr\Exceptions\InvalidProviderException;
14
use Borfast\Socializr\Exceptions\InvalidConfigurationException;
15
16
class ConnectorFactory
17
{
18
    /**
19
     * An array that will contain the configuration for the various providers
20
     * Socializr's connectors can use.
21
     * @var array
22
     */
23
    protected $config = [];
24
25
26
    /**
27
     * The ID on the social network we're connecting to.
28
     * @var string
29
     */
30
    protected $id;
31
32
33
    /**
34
     * The constructor for the ConnectorFactory.
35
     * @param array $config Contains the configuration for each provider.
36
     * @throws InvalidConfigurationException if $config has no 'providers' key.
37
     */
38
    public function __construct(array $config)
39
    {
40
        $this->config = $config;
41
42
        if (!array_key_exists('providers', $config)) {
43
            throw new InvalidConfigurationException;
44
        }
45
    }
46
47
48
    /**
49
     * Creates a Connector object for the given provider type. The $id parameter
50
     * may be null but should only be used like that when getting
51
     *
52
     * @param  string $provider The provider type you want.
53
     * @param  TokenStorageInterface $storage The storage for PHPoAuthLib.
54
     * @param  array $options
55
     * @param  string $id The ID we're connecting to.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
56
     * @param  null|ClientInterface $http_client The HTTP client for PHPoAuthLib.
57
     * @param  null|ServiceFactory $service_factory The PHPoAuthLib service factory.
58
     * @param  null|CredentialsInterface $credentials The credentials for PHPoAuthLib.
59
     * @return ConnectorInterface An instance of the requested connector type.
60
     * @throws InvalidProviderException
61
     */
62
    public function createConnector(
63
        $provider,
64
        TokenStorageInterface $storage,
65
        array $options = [],
66
        $id = null,
67
        ClientInterface $http_client = null,
68
        ServiceFactory $service_factory = null,
69
        CredentialsInterface $credentials = null
70
    ) {
71
        // Only allow configured providers.
72
        if (!array_key_exists($provider, $this->config['providers'])) {
73
            throw new InvalidProviderException($provider);
74
        }
75
76
        // Default to CurlClient (why isn't this the default? :( )
77
        if (is_null($http_client)) {
78
            $http_client = new CurlClient;
79
        }
80
81
        // Just if we want to be lazy and not pass this as an argument.
82
        if (is_null($service_factory)) {
83
            $service_factory = new ServiceFactory;
84
        }
85
86
        // Simplify config access for this provider.
87
        $config = $this->getFlatConfig($provider);
88
89
90
        // We're already getting the credentials via $this->config, we might not
91
        // want to always pass them as an argument.
92
        if (is_null($credentials)) {
93
            $credentials = new Credentials(
94
                $config['consumer_key'],
95
                $config['consumer_secret'],
96
                $config['callback']
97
            );
98
        }
99
100
        // Let's make use of CurlClient.
101
        $service_factory->setHttpClient($http_client);
102
103
        // Temporary (or so I hope) hack to overcome PHPoAuthLib not being ready
104
        // for Facebook's Graph API 1.0 deprecation.
105
106
        // If this is Facebook, let's specify we want API v2.2
107
//        $api_version = null;
108
//        if (strtolower($provider) == 'facebook') {
109
//            $api_version = '2.2';
110
//        }
111
        $uri = null;
112
        if (in_array($provider, ['Facebook', 'FacebookGroup', 'FacebookPage'])) {
113
            $uri = new Uri('https://graph.facebook.com/v2.8/');
114
        }
115
116
        // Finally, create the service already!
117
        $service = $service_factory->createService(
118
            $config['service'],
119
            $credentials,
120
            $storage,
121
            $config['scopes'],
122
            $uri
123
            //$api_version
124
        );
125
126
127
        $connector_class = '\\Borfast\\Socializr\\Connectors\\'.$provider;
128
        $connector = new $connector_class($config, $service, $options, $id);
129
130
        return $connector;
131
    }
132
133
134
    /**
135
     * Gets a config array for the given provider, taking care of a few checks
136
     * to make sure it has the needed data.
137
     * @param  string $provider The provider type we want the config for.
138
     * @return array            The config for the requested provider type.
139
     */
140
    protected function getFlatConfig($provider)
141
    {
142
        $config = $this->config['providers'][$provider];
143
144
        /*
145
         * Make sure we will create the correct PHPoAuthLib service. Each
146
         * configured provider can specify which service to use. If none is
147
         * specified, then the provider name is used.
148
         */
149
        if (empty($config['service'])) {
150
            $config['service'] = $provider;
151
        }
152
153
        // Cater for the possibility of having one single general callback URL.
154
        if (empty($config['callback'])) {
155
            $config['callback'] = $this->config['callback'];
156
        }
157
158
        // Cater for the possibility of no scope being defined
159
        if (empty($config['scopes'])) {
160
            $config['scopes'] = [];
161
        }
162
163
        // Make it possible to define the scopes as a comma separated string
164
        // instead of an array.
165
        if (!is_array($config['scopes'])) {
166
            $config['scopes'] = explode(', ', $config['scopes']);
167
        }
168
169
        return $config;
170
    }
171
}
172