Skip to content

Commit 031b3d8

Browse files
committed
Slight refactoring, use certainly to ensure cacert.pem is up-to-date
1 parent 075056c commit 031b3d8

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/Controllers/CompareController.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,35 @@
1010

1111
use \Psr\Http\Message\ServerRequestInterface as Request;
1212
use \Psr\Http\Message\ResponseInterface as Response;
13+
use \ParagonIE\Certainty\RemoteFetch;
14+
use \GuzzleHttp\Client as GuzzleClient;
15+
use \GuzzleHttp\Exception\ClientException as GuzzleClientException;
1316

1417
class CompareController extends Controller {
1518
const CACHE_EXPIRE_AFTER = 60 * 60 * 6;
1619

1720
const GUZZLE_DEFAULT_OPTIONS = [
18-
'verify' => false, // TODO: Make this true once cacert.pem stops being annoying
1921
'http_errors' => true,
2022
];
2123

2224
const PARAMETER_MISSING = "<b>Um...</b> Seems like you're missing user %d's ID.";
2325
const COULDNT_FIND_USER = "<b>Whoops!</b> Looks like we couldn't find user %d! Verify that the channel ID is correct, then try again.";
2426
const USER_SUBS_PRIVATE = "<b>Whoops!</b> Looks like user %d's subscriptions settings are set to <b>private</b>! They'll have to <a href=\"https://imgur.com/a/P6Dcm\" class=\"alert-link\">set their subscriptions to <b>public</b></a> before they can be compared here.";
2527

28+
private $guzzle;
29+
private $guzzleParams;
30+
31+
public function __construct($container) {
32+
parent::__construct($container);
33+
34+
$fetcher = new RemoteFetch();
35+
$latestCACertBundle = $fetcher->getLatestBundle();
36+
$this->guzzle = new GuzzleClient();
37+
$this->guzzleParams = array_merge(self::GUZZLE_DEFAULT_OPTIONS, [
38+
'verify' => $latestCACertBundle->getFilePath(),
39+
]);
40+
}
41+
2642
public function compare(Request $request, Response $response) {
2743
$user1 = $request->getParam('user1');
2844
$user2 = $request->getParam('user2');
@@ -64,14 +80,14 @@ public function compare(Request $request, Response $response) {
6480

6581
try {
6682
$user1Subs = $this->getUserSubscriptions($user1);
67-
} catch (\GuzzleHttp\Exception\ClientException $e) {
83+
} catch (GuzzleClientException $e) {
6884
$this->container->flash->addMessage('danger', sprintf(self::USER_SUBS_PRIVATE, 1));
6985
return $response->withRedirect($this->container->router->pathFor('home'));
7086
}
7187

7288
try {
7389
$user2Subs = $this->getUserSubscriptions($user2);
74-
} catch (\GuzzleHttp\Exception\ClientException $e) {
90+
} catch (GuzzleClientException $e) {
7591
$this->container->flash->addMessage('danger', sprintf(self::COULDNT_FIND_USER, 2));
7692
return $response->withRedirect($this->container->router->pathFor('home'));
7793
}
@@ -103,16 +119,15 @@ private function getChannelNameFromID($channelID) {
103119
if ($this->container->cache->hasItem($cacheName) && $this->container->cache->getItem($cacheName)->isHit()) {
104120
return $this->container->cache->getItem($cacheName)->get();
105121
} else {
106-
$client = new \GuzzleHttp\Client();
107-
$params = array_merge(self::GUZZLE_DEFAULT_OPTIONS, [
122+
$params = array_merge($this->guzzleParams, [
108123
'query' => [
109124
'part' => 'snippet',
110125
'id' => $channelID,
111126
'key' => getenv('YT_API_KEY'),
112127
],
113128
]);
114129

115-
$response = $client->get('https://www.googleapis.com/youtube/v3/channels', $params);
130+
$response = $this->guzzle->get('https://www.googleapis.com/youtube/v3/channels', $params);
116131

117132
if ($response->getStatusCode() == 200) {
118133
$body = json_decode((string) $response->getBody(), true);
@@ -140,22 +155,20 @@ private function getSubscriptionsOnPage($channelID, $page = null) {
140155
if ($this->container->cache->hasItem($cacheName) && $this->container->cache->getItem($cacheName)->isHit()) {
141156
return $this->container->cache->getItem($cacheName)->get();
142157
} else {
143-
$client = new \GuzzleHttp\Client();
144-
$params = array_merge(self::GUZZLE_DEFAULT_OPTIONS, [
158+
$params = array_merge($this->guzzleParams, [
145159
'query' => [
146160
'part' => 'snippet',
147161
'channelId' => $channelID,
148162
'maxResults' => 50,
149163
'key' => getenv('YT_API_KEY'),
150164
],
151-
'verify' => false,
152165
]);
153166

154167
if ($page) {
155168
$params['query']['pageToken'] = $page;
156169
}
157170

158-
$response = $client->get('https://www.googleapis.com/youtube/v3/subscriptions', $params);
171+
$response = $this->guzzle->get('https://www.googleapis.com/youtube/v3/subscriptions', $params);
159172
if ($response->getStatusCode() == 200) {
160173
$body = json_decode((string) $response->getBody(), true);
161174

0 commit comments

Comments
 (0)