PageApi::getPagesById()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 5
nc 1
nop 1
crap 2
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 11
    public function __construct(RequestInterface $request, array $config)
38
    {
39 11
        $this->ttl = $config['cache_ttl'];
40 11
        $this->cache = $config['cache'];
41 11
        $this->http = new Client([
42 11
            'base_uri' => $config['base_uri'],
43
            'headers' => [
44 11
                'X-Render-Host' => $request->getUri()->getHost(),
45
            ]
46 11
        ]);
47 11
    }
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
     * Call a particular api endpoint.
76
     * @param string $method
77
     * @param string $url
78
     * @return \Psr\Http\Message\ResponseInterface|false
79
     */
80 7
    public function call($method, $url)
81
    {
82 7
        $cacheKey = strtoupper($method) . "::" . $url;
83
        try {
84 7
            if (!$value = $this->cache->get($cacheKey)) {
85 7
                $response = $this->http->request(strtoupper($method), $url);
86 5
            }
87 7
        } catch (ClientException $e) {
88 1
            $response = $e->getResponse();
89 2
        } catch (ConnectException $e) {
90
            // Suppress network errors at this level.
91
        }
92 7
        if (!isset($response) && !empty($value)) {
93 1
            $response = \GuzzleHttp\Psr7\parse_response($value);
94 7
        } elseif ($this->ttl !== false && !empty($response)) {
95 3
            $this->cache->set($cacheKey, \GuzzleHttp\Psr7\str($response), $this->ttl ? time() + $this->ttl : 0);
96 3
        }
97 7
        return !empty($response) ? $response : false;
98
    }
99
}
100