1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vssl\Render; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use GuzzleHttp\Exception\ConnectException; |
8
|
|
|
use Journey\Cache\CacheAdapterInterface; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
|
11
|
|
|
class PageApi |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Guzzle HTTP interface. |
15
|
|
|
* |
16
|
|
|
* @var \GuzzleHTTP\Client |
17
|
|
|
*/ |
18
|
|
|
protected $http; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* A cache adapter for storing results. |
22
|
|
|
* |
23
|
|
|
* @var \Journey\Cache\CacheAdapterInterface |
24
|
|
|
*/ |
25
|
|
|
protected $cache; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Number of seconds responses should be cached for. |
29
|
|
|
* |
30
|
|
|
* @var integer |
31
|
|
|
*/ |
32
|
|
|
protected $ttl; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize the page api methods. |
36
|
|
|
*/ |
37
|
10 |
|
public function __construct(RequestInterface $request, array $config) |
38
|
|
|
{ |
39
|
10 |
|
$this->ttl = $config['cache_ttl']; |
40
|
10 |
|
$this->cache = $config['cache']; |
41
|
10 |
|
$this->http = new Client([ |
42
|
10 |
|
'base_uri' => $config['base_uri'], |
43
|
|
|
'headers' => [ |
44
|
10 |
|
'X-Render-Host' => $request->getUri()->getHost(), |
45
|
|
|
] |
46
|
10 |
|
]); |
47
|
10 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get a particular page from the api. |
51
|
|
|
* |
52
|
|
|
* @param string $path path of the page |
53
|
|
|
* @return array|false |
54
|
|
|
*/ |
55
|
4 |
|
public function getPage($path) |
56
|
|
|
{ |
57
|
4 |
|
return $this->call("get", "/api/pages?slug=" . $path); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get a particular page from the api. |
62
|
|
|
* |
63
|
|
|
* @param array $ids unique ids of pages to fetch data for. |
64
|
|
|
* @return array|false |
65
|
|
|
*/ |
66
|
|
|
public function getPagesById($ids) |
67
|
|
|
{ |
68
|
2 |
|
$ids = array_filter(array_map(function ($id) { |
69
|
2 |
|
return is_numeric($id) ? (integer) $id : null; |
70
|
2 |
|
}, $ids)); |
71
|
2 |
|
return $this->call("get", "/api/pages?ids=" . implode(",", $ids)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Clear the cache of a given endpoint immediately. |
76
|
|
|
* |
77
|
|
|
* @param string $method get/post/put |
78
|
|
|
* @param string $url endpoint to hit |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
1 |
|
public function clearEndpoint($method, $url) |
82
|
|
|
{ |
83
|
1 |
|
$cacheKey = md5(strtoupper($method) . "::" . $url); |
84
|
1 |
|
$this->cache->delete($cacheKey); |
85
|
1 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Call a particular api endpoint. |
90
|
|
|
* |
91
|
|
|
* @return \Psr\Http\Message\ResponseInterface|false |
92
|
|
|
*/ |
93
|
8 |
|
public function call($method, $url) |
94
|
|
|
{ |
95
|
8 |
|
$cacheKey = md5(strtoupper($method) . "::" . $url); |
96
|
|
|
try { |
97
|
8 |
|
if (!$value = $this->cache->get($cacheKey)) { |
98
|
8 |
|
$response = $this->http->request(strtoupper($method), $url); |
99
|
5 |
|
} |
100
|
8 |
|
} catch (ClientException $e) { |
101
|
2 |
|
$response = $e->getResponse(); |
102
|
3 |
|
} catch (ConnectException $e) { |
103
|
|
|
// Suppress network errors at this level. |
104
|
|
|
} |
105
|
8 |
|
if (!isset($response) && $value) { |
106
|
1 |
|
$response = \GuzzleHttp\Psr7\parse_response($value); |
|
|
|
|
107
|
8 |
|
} elseif ($this->ttl) { |
108
|
4 |
|
$this->cache->set($cacheKey, \GuzzleHttp\Psr7\str($response), time() + $this->ttl); |
|
|
|
|
109
|
4 |
|
} |
110
|
8 |
|
return isset($response) ? $response : false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: