Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit f2ca27a

Browse files
committed
Merge pull request #510 from EmanueleMinotto/complexity
Complexity reduction
2 parents 6cc4852 + 2b4776b commit f2ca27a

File tree

9 files changed

+502
-80
lines changed

9 files changed

+502
-80
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Version 5 of the Facebook PHP SDK is a complete refactor of version 4. It comes
3434
- Renamed `FacebookHttpable` to `FacebookHttpClientInterface`
3535
- Added `FacebookApp` entity that contains info about the Facebook app
3636
- Updated the API for the helpers
37+
- Added `HttpClients`, `PersistentData` and `PseudoRandomString` factories to reduce main class' complexity
3738
- Tests
3839
- Added namespaces to the tests
3940
- Grouped functional tests under `functional` group

src/Facebook/Facebook.php

Lines changed: 40 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,11 @@
3232
use Facebook\GraphNodes\GraphEdge;
3333
use Facebook\Url\UrlDetectionInterface;
3434
use Facebook\Url\FacebookUrlDetectionHandler;
35+
use Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory;
3536
use Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface;
36-
use Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator;
37-
use Facebook\PseudoRandomString\OpenSslPseudoRandomStringGenerator;
38-
use Facebook\PseudoRandomString\UrandomPseudoRandomStringGenerator;
39-
use Facebook\HttpClients\FacebookHttpClientInterface;
40-
use Facebook\HttpClients\FacebookCurlHttpClient;
41-
use Facebook\HttpClients\FacebookStreamHttpClient;
42-
use Facebook\HttpClients\FacebookGuzzleHttpClient;
37+
use Facebook\HttpClients\HttpClientsFactory;
38+
use Facebook\PersistentData\PersistentDataFactory;
4339
use Facebook\PersistentData\PersistentDataInterface;
44-
use Facebook\PersistentData\FacebookSessionPersistentDataHandler;
45-
use Facebook\PersistentData\FacebookMemoryPersistentDataHandler;
4640
use Facebook\Helpers\FacebookCanvasHelper;
4741
use Facebook\Helpers\FacebookJavaScriptHelper;
4842
use Facebook\Helpers\FacebookPageTabHelper;
@@ -130,80 +124,43 @@ class Facebook
130124
*/
131125
public function __construct(array $config = [])
132126
{
133-
$appId = isset($config['app_id']) ? $config['app_id'] : getenv(static::APP_ID_ENV_NAME);
134-
if (!$appId) {
127+
$config = array_merge([
128+
'app_id' => getenv(static::APP_ID_ENV_NAME),
129+
'app_secret' => getenv(static::APP_SECRET_ENV_NAME),
130+
'default_graph_version' => static::DEFAULT_GRAPH_VERSION,
131+
'enable_beta_mode' => false,
132+
'http_client_handler' => null,
133+
'persistent_data_handler' => null,
134+
'pseudo_random_string_generator' => null,
135+
'url_detection_handler' => null,
136+
], $config);
137+
138+
if (!$config['app_id']) {
135139
throw new FacebookSDKException('Required "app_id" key not supplied in config and could not find fallback environment variable "' . static::APP_ID_ENV_NAME . '"');
136140
}
137-
138-
$appSecret = isset($config['app_secret']) ? $config['app_secret'] : getenv(static::APP_SECRET_ENV_NAME);
139-
if (!$appSecret) {
141+
if (!$config['app_secret']) {
140142
throw new FacebookSDKException('Required "app_secret" key not supplied in config and could not find fallback environment variable "' . static::APP_SECRET_ENV_NAME . '"');
141143
}
142144

143-
$this->app = new FacebookApp($appId, $appSecret);
144-
145-
$httpClientHandler = null;
146-
if (isset($config['http_client_handler'])) {
147-
if ($config['http_client_handler'] instanceof FacebookHttpClientInterface) {
148-
$httpClientHandler = $config['http_client_handler'];
149-
} elseif ($config['http_client_handler'] === 'curl') {
150-
$httpClientHandler = new FacebookCurlHttpClient();
151-
} elseif ($config['http_client_handler'] === 'stream') {
152-
$httpClientHandler = new FacebookStreamHttpClient();
153-
} elseif ($config['http_client_handler'] === 'guzzle') {
154-
$httpClientHandler = new FacebookGuzzleHttpClient();
155-
} else {
156-
throw new \InvalidArgumentException('The http_client_handler must be set to "curl", "stream", "guzzle", or be an instance of Facebook\HttpClients\FacebookHttpClientInterface');
157-
}
158-
}
159-
160-
$enableBeta = isset($config['enable_beta_mode']) && $config['enable_beta_mode'] === true;
161-
$this->client = new FacebookClient($httpClientHandler, $enableBeta);
162-
163-
if (isset($config['url_detection_handler'])) {
164-
if ($config['url_detection_handler'] instanceof UrlDetectionInterface) {
165-
$this->urlDetectionHandler = $config['url_detection_handler'];
166-
} else {
167-
throw new \InvalidArgumentException('The url_detection_handler must be an instance of Facebook\Url\UrlDetectionInterface');
168-
}
169-
}
170-
171-
if (isset($config['pseudo_random_string_generator'])) {
172-
if ($config['pseudo_random_string_generator'] instanceof PseudoRandomStringGeneratorInterface) {
173-
$this->pseudoRandomStringGenerator = $config['pseudo_random_string_generator'];
174-
} elseif ($config['pseudo_random_string_generator'] === 'mcrypt') {
175-
$this->pseudoRandomStringGenerator = new McryptPseudoRandomStringGenerator();
176-
} elseif ($config['pseudo_random_string_generator'] === 'openssl') {
177-
$this->pseudoRandomStringGenerator = new OpenSslPseudoRandomStringGenerator();
178-
} elseif ($config['pseudo_random_string_generator'] === 'urandom') {
179-
$this->pseudoRandomStringGenerator = new UrandomPseudoRandomStringGenerator();
180-
} else {
181-
throw new \InvalidArgumentException('The pseudo_random_string_generator must be set to "mcrypt", "openssl", or "urandom", or be an instance of Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface');
182-
}
183-
}
184-
185-
if (isset($config['persistent_data_handler'])) {
186-
if ($config['persistent_data_handler'] instanceof PersistentDataInterface) {
187-
$this->persistentDataHandler = $config['persistent_data_handler'];
188-
} elseif ($config['persistent_data_handler'] === 'session') {
189-
$this->persistentDataHandler = new FacebookSessionPersistentDataHandler();
190-
} elseif ($config['persistent_data_handler'] === 'memory') {
191-
$this->persistentDataHandler = new FacebookMemoryPersistentDataHandler();
192-
} else {
193-
throw new \InvalidArgumentException('The persistent_data_handler must be set to "session", "memory", or be an instance of Facebook\PersistentData\PersistentDataInterface');
194-
}
195-
}
145+
$this->app = new FacebookApp($config['app_id'], $config['app_secret']);
146+
$this->client = new FacebookClient(
147+
HttpClientsFactory::createHttpClient($config['http_client_handler']),
148+
$config['enable_beta_mode']
149+
);
150+
$this->pseudoRandomStringGenerator = PseudoRandomStringGeneratorFactory::createPseudoRandomStringGenerator(
151+
$config['pseudo_random_string_generator']
152+
);
153+
$this->setUrlDetectionHandler($config['url_detection_handler'] ?: new FacebookUrlDetectionHandler());
154+
$this->persistentDataHandler = PersistentDataFactory::createPersistentDataHandler(
155+
$config['persistent_data_handler']
156+
);
196157

197158
if (isset($config['default_access_token'])) {
198159
$this->setDefaultAccessToken($config['default_access_token']);
199160
}
200161

201-
if (isset($config['default_graph_version'])) {
202-
$this->defaultGraphVersion = $config['default_graph_version'];
203-
} else {
204-
// @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set
205-
$this->defaultGraphVersion = static::DEFAULT_GRAPH_VERSION;
206-
}
162+
// @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set
163+
$this->defaultGraphVersion = $config['default_graph_version'];
207164
}
208165

209166
/**
@@ -259,13 +216,19 @@ public function getLastResponse()
259216
*/
260217
public function getUrlDetectionHandler()
261218
{
262-
if (!$this->urlDetectionHandler instanceof UrlDetectionInterface) {
263-
$this->urlDetectionHandler = new FacebookUrlDetectionHandler();
264-
}
265-
266219
return $this->urlDetectionHandler;
267220
}
268221

222+
/**
223+
* Changes the URL detection handler.
224+
*
225+
* @param UrlDetectionInterface $urlDetectionHandler
226+
*/
227+
private function setUrlDetectionHandler(UrlDetectionInterface $urlDetectionHandler)
228+
{
229+
$this->urlDetectionHandler = $urlDetectionHandler;
230+
}
231+
269232
/**
270233
* Returns the default AccessToken entity.
271234
*
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright 2014 Facebook, Inc.
4+
*
5+
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
6+
* use, copy, modify, and distribute this software in source code or binary
7+
* form for use in connection with the web services and APIs provided by
8+
* Facebook.
9+
*
10+
* As with any software that integrates with the Facebook platform, your use
11+
* of this software is subject to the Facebook Developer Principles and
12+
* Policies [http://developers.facebook.com/policy/]. This copyright notice
13+
* shall be included in all copies or substantial portions of the software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*
23+
*/
24+
namespace Facebook\HttpClients;
25+
26+
use GuzzleHttp\Client;
27+
use InvalidArgumentException;
28+
29+
class HttpClientsFactory
30+
{
31+
private function __construct()
32+
{
33+
// a factory constructor should never be invoked
34+
}
35+
36+
/**
37+
* HTTP client generation.
38+
*
39+
* @param FacebookHttpClientInterface|Client|string|null $handler
40+
*
41+
* @throws Exception If the cURL extension or the Guzzle client aren't available (if required).
42+
* @throws InvalidArgumentException If the http client handler isn't "curl", "stream", "guzzle", or an instance of Facebook\HttpClients\FacebookHttpClientInterface.
43+
*
44+
* @return FacebookHttpClientInterface
45+
*/
46+
public static function createHttpClient($handler)
47+
{
48+
if (!$handler) {
49+
return self::detectDefaultClient();
50+
}
51+
52+
if ($handler instanceof FacebookHttpClientInterface) {
53+
return $handler;
54+
}
55+
56+
if ('stream' === $handler) {
57+
return new FacebookStreamHttpClient();
58+
}
59+
if ('curl' === $handler) {
60+
if (!extension_loaded('curl')) {
61+
throw new Exception('The cURL extension must be loaded in order to use the "curl" handler.');
62+
}
63+
64+
return new FacebookCurlHttpClient();
65+
}
66+
67+
if ('guzzle' === $handler && !class_exists('GuzzleHttp\Client')) {
68+
throw new Exception('The Guzzle HTTP client must be included in order to use the "guzzle" handler.');
69+
}
70+
71+
if ($handler instanceof Client) {
72+
return new FacebookGuzzleHttpClient($handler);
73+
}
74+
if ('guzzle' === $handler) {
75+
return new FacebookGuzzleHttpClient();
76+
}
77+
78+
throw new InvalidArgumentException('The http client handler must be set to "curl", "stream", "guzzle", be an instance of GuzzleHttp\Client or an instance of Facebook\HttpClients\FacebookHttpClientInterface');
79+
}
80+
81+
/**
82+
* Detect default HTTP client.
83+
*
84+
* @return FacebookHttpClientInterface
85+
*/
86+
private static function detectDefaultClient()
87+
{
88+
if (extension_loaded('curl')) {
89+
return new FacebookCurlHttpClient();
90+
}
91+
92+
if (class_exists('GuzzleHttp\Client')) {
93+
return new FacebookGuzzleHttpClient();
94+
}
95+
96+
return new FacebookStreamHttpClient();
97+
}
98+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright 2014 Facebook, Inc.
4+
*
5+
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
6+
* use, copy, modify, and distribute this software in source code or binary
7+
* form for use in connection with the web services and APIs provided by
8+
* Facebook.
9+
*
10+
* As with any software that integrates with the Facebook platform, your use
11+
* of this software is subject to the Facebook Developer Principles and
12+
* Policies [http://developers.facebook.com/policy/]. This copyright notice
13+
* shall be included in all copies or substantial portions of the software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*
23+
*/
24+
namespace Facebook\PersistentData;
25+
26+
use InvalidArgumentException;
27+
28+
class PersistentDataFactory
29+
{
30+
private function __construct()
31+
{
32+
// a factory constructor should never be invoked
33+
}
34+
35+
/**
36+
* PersistentData generation.
37+
*
38+
* @param PersistentDataInterface|string|null $handler
39+
*
40+
* @throws InvalidArgumentException If the persistent data handler isn't "session", "memory", or an instance of Facebook\PersistentData\PersistentDataInterface.
41+
*
42+
* @return PersistentDataInterface
43+
*/
44+
public static function createPersistentDataHandler($handler)
45+
{
46+
if (!$handler) {
47+
return session_status() === PHP_SESSION_ACTIVE
48+
? new FacebookSessionPersistentDataHandler()
49+
: new FacebookMemoryPersistentDataHandler();
50+
}
51+
52+
if ($handler instanceof PersistentDataInterface) {
53+
return $handler;
54+
}
55+
56+
if ('session' === $handler) {
57+
new FacebookSessionPersistentDataHandler();
58+
}
59+
if ('memory' === $handler) {
60+
return new FacebookMemoryPersistentDataHandler();
61+
}
62+
63+
throw new InvalidArgumentException('The persistent data handler must be set to "session", "memory", or be an instance of Facebook\PersistentData\PersistentDataInterface');
64+
}
65+
}

0 commit comments

Comments
 (0)