Client::catalogQueryModified()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Client class for getting Gospel Library JSON
7
 *
8
 * The following API calls are no longer valid Gospel Library calls:
9
 *   * `catalog.query.folder`
10
 *   * `book.download`
11
 *
12
 * Low-level class to ingest API data: nothing to test
13
 *
14
 * @link      https://tech.lds.org/wiki/Gospel_Library_Catalog_Web_Service
15
 *
16
 * @copyright Copyright (c) 2018 Jared Howland
17
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
18
 * @author    Jared Howland <[email protected]>
19
 */
20
21
namespace Gospel;
22
23
use GuzzleHttp\Client as GuzzleClient;
24
25
/**
26
 * Client class for getting Gospel Library JSON
27
 */
28
class Client
29
{
30
    /** @var object Guzzle client */
31
    private $client;
32
33
    /**
34
     * Construct Client class
35
     *
36
     * @param null
37
     */
38
    public function __construct()
39
    {
40
        $this->client = new GuzzleClient();
41
    }
42
43
    /**
44
     * Languages query
45
     *
46
     * @param null
47
     *
48
     * @return \stdClass Object containing languages for which there is content in the Gospel Library
49
     */
50
    public function languagesQuery(): \stdClass
51
    {
52
        return $this->getApiArray('languages.query');
53
    }
54
55
    /**
56
     * Platforms query
57
     *
58
     * @param null
59
     *
60
     * @return \stdClass Object containing platforms for which there is content in the Gospel Library
61
     */
62
    public function platformsQuery(): \stdClass
63
    {
64
        return $this->getApiArray('platforms.query');
65
    }
66
67
    /**
68
     * Catalog query
69
     *
70
     * @param int $languageid ID of language to get catalog content in. Available languages: use `languagesQuery()`
71
     * @param int $platformid ID of platform to get catalog content from. Available platforms: use `platformsQuery()`
72
     *
73
     * @return \stdClass Object containing metadata about requested Gospel Library catalog
74
     */
75
    public function catalogQuery(int $languageid, int $platformid): \stdClass
76
    {
77
        return $this->getApiArray('catalog.query', ['languageid' => $languageid, 'platformid' => $platformid]);
78
    }
79
80
    /**
81
     * Catalog query modified
82
     *
83
     * @param int $languageid ID of language to get catalog content in. Available languages: use `languagesQuery()`
84
     * @param int $platformid ID of platform to get catalog content from. Available platforms: use `platformsQuery()`
85
     *
86
     * @return \stdClass Object containing metadata about requested Gospel Library catalog, including the last modified date
87
     */
88
    public function catalogQueryModified(int $languageid, int $platformid): \stdClass
89
    {
90
        return $this->getApiArray('catalog.query.modified', ['languageid' => $languageid, 'platformid' => $platformid]);
91
    }
92
93
    /**
94
     * Book versions
95
     *
96
     * @param int    $languageid ID of language to get catalog content in. Available languages: use `languagesQuery()`
97
     * @param int    $platformid ID of platform to get catalog content from. Available platforms: use `platformsQuery()`
98
     * @param string $lastdate   List content updated since `$lastdate`
99
     *
100
     * @return \stdClass Object with all material IDs updated since `$lastdate`
101
     */
102
    public function bookVersions(int $languageid, int $platformid, string $lastdate): \stdClass
103
    {
104
        $lastdate = date('Y-m-d', strtotime($lastdate));
105
106
        return $this->getApiArray(
107
            'book.versions',
108
            ['languageid' => $languageid, 'platformid' => $platformid, 'lastdate' => $lastdate]
109
        );
110
    }
111
112
    /**
113
     * Get File
114
     *
115
     * @param string $url  URL of file to get
116
     * @param array  $args Associative array of arguments to pass as HTTP GET query parameters. Default: `null`
117
     *
118
     * @return string Data returned from downloaded URL
119
     */
120
    public function getUrl(string $url, array $args = []): string
121
    {
122
        return (string)$this->client
123
            ->request('GET', $url, ['query' => $args])
124
            ->getBody();
125
    }
126
127
    /**
128
     * Get array from returned API JSON
129
     *
130
     * @param string $action API call to make
131
     * @param array  $args   Associative array of arguments to pass as HTTP GET query parameters. Default: `null`
132
     *
133
     * @return \stdClass Object containing the API call results
134
     */
135
    private function getApiArray(string $action, array $args = []): \stdClass
136
    {
137
        $args = array_merge(['action' => $action], $args, ['format' => 'json']);
138
        $json = $this->getUrl(Config::get('base'), $args);
139
140
        return (object)['args' => $args, 'results' => json_decode($json)];
141
    }
142
}
143