MyQuran::getPrayerTimePerMonth()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 15
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
1
<?php
2
3
namespace Ianrizky\MoslemPray\Drivers;
4
5
use Exception;
6
use Ianrizky\MoslemPray\Contracts\Response\HasPrayerTime;
7
use Ianrizky\MoslemPray\Contracts\Response\HasPrayerTimeCollection;
8
use Ianrizky\MoslemPray\Response\MyQuran\City;
9
use Ianrizky\MoslemPray\Response\MyQuran\Collection\CityCollection;
10
use Ianrizky\MoslemPray\Response\MyQuran\Collection\PrayerTimeCollection;
11
use Ianrizky\MoslemPray\Response\MyQuran\Collection\TafsirCollection;
12
use Ianrizky\MoslemPray\Response\MyQuran\PrayerTime;
13
use Ianrizky\MoslemPray\Support\ParseDate;
14
use Illuminate\Http\Client\Response;
15
16
/**
17
 * @see https://documenter.getpostman.com/view/841292/Tz5p7yHS
18
 */
19
class MyQuran extends AbstractDriver
20
{
21 1
    use ParseDate;
22
23
    /**
24
     * List of configuration value.
25
     *
26
     * @var array
27
     */
28
    protected $config = [
29
        'url' => 'https://api.myquran.com/v1/',
30
    ];
31
32
    /**
33
     * Return city information based on the given identifier.
34
     *
35
     * @param  mixed  $identifier
36
     * @return \Ianrizky\MoslemPray\Response\MyQuran\City
37
     */
38 6
    public function getCity($identifier): City
39
    {
40 6
        if (is_numeric($identifier)) {
41 2
            return $this->getCityFromId($identifier);
42
        }
43
44 4
        return $this->getCityFromName($identifier);
45
    }
46
47
    /**
48
     * Return city information based on the given id.
49
     *
50
     * @param  mixed  $id
51
     * @return \Ianrizky\MoslemPray\Response\MyQuran\City
52
     *
53
     * @see https://api.myquran.com/v1/sholat/kota/id/{id}
54
     * @see https://documenter.getpostman.com/view/841292/Tz5p7yHS#88549bc5-cd70-4ba1-b565-f3eef882e060 (Sholat/Lokasi/ID Kota)
55
     */
56 4
    public function getCityFromId($id): City
57
    {
58 4
        $response = $this->throwJsonError(
59 4
            $this->request->get('/sholat/kota/id/' . $id)
60
        );
61
62 2
        return new City([
63 2
            'id' => data_get($response->json(), 'data.id'),
64 2
            'name' => data_get($response->json(), 'data.lokasi'),
65
        ]);
66
    }
67
68
    /**
69
     * Return city information based on the given name.
70
     *
71
     * @param  string  $name
72
     * @return \Ianrizky\MoslemPray\Response\MyQuran\City
73
     *
74
     * @see https://api.myquran.com/v1/sholat/kota/cari/{name}
75
     * @see https://documenter.getpostman.com/view/841292/Tz5p7yHS#ae4b237c-e97c-4353-9e94-67d155af06f8 (Sholat/Lokasi/Pencarian)
76
     */
77 6
    public function getCityFromName(string $name): City
78
    {
79 6
        $response = $this->throwJsonError(
80 6
            $this->request->get('/sholat/kota/cari/' . $name)
81
        );
82
83 4
        return new City([
84 4
            'id' => data_get($response->json(), 'data.0.id'),
85 4
            'name' => data_get($response->json(), 'data.0.lokasi'),
86
        ]);
87
    }
88
89
    /**
90
     * Return list of all city.
91
     *
92
     * @return \Ianrizky\MoslemPray\Response\MyQuran\Collection\CityCollection
93
     *
94
     * @see https://api.myquran.com/v1/sholat/kota/semua
95
     * @see https://documenter.getpostman.com/view/841292/Tz5p7yHS#145bcb30-dba6-4d24-9799-03ba878b5476 (Sholat/Lokasi/Semua Kota)
96
     */
97 1
    public function getCities(): CityCollection
98
    {
99 1
        $response = $this->throwJsonError(
100 1
            $this->request->get('/sholat/kota/semua')
101
        );
102
103 1
        return new CityCollection(array_map(function ($data) {
104
            return [
105 1
                'id' => $data['id'],
106 1
                'name' => $data['lokasi'],
107
            ];
108 1
        }, $response->json()));
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     *
114
     * @return \Ianrizky\MoslemPray\Response\MyQuran\PrayerTime
115
     *
116
     * @see https://api.myquran.com/v1/sholat/jadwal/{city_id}/{year}/{month}/{date}
117
     * @see https://documenter.getpostman.com/view/841292/Tz5p7yHS#534da562-3335-4a1f-bca2-d7ee2266f457 (Sholat/Jadwal/Per Hari)
118
     */
119 1
    public function getPrayerTime($city, $date = null): HasPrayerTime
120
    {
121 1
        $city = $this->getCity($city);
122 1
        $date = $this->parseDate($date);
123
124 1
        $response = $this->throwJsonError(
125 1
            $this->request->get(sprintf('/sholat/jadwal/%s/%s', $city->id, $date->format('Y/m/d')))
126
        );
127
128 1
        return PrayerTime::fromResponse($response);
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     *
134
     * @return \Ianrizky\MoslemPray\Response\MyQuran\Collection\PrayerTimeCollection
135
     *
136
     * @see https://api.myquran.com/v1/sholat/jadwal/{city_id}/{year}/{month}
137
     * @see https://documenter.getpostman.com/view/841292/Tz5p7yHS#b0b39104-8216-49fc-9d3b-ea53e5832e16 (Sholat/Jadwal/Per Bulan)
138
     */
139 1
    public function getPrayerTimePerMonth($city, $year = null, int $month = null): HasPrayerTimeCollection
140
    {
141 1
        $city = $this->getCity($city);
142
143 1
        if ($year && $month) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $month of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
144
            $date = $this->parseDate($year . '-' . $month . '-1');
145
        } else {
146 1
            $date = $this->parseDate($year);
147
        }
148
149 1
        $response = $this->throwJsonError(
150 1
            $this->request->get(sprintf('/sholat/jadwal/%s/%s', $city->id, $date->format('Y/m')))
151
        );
152
153 1
        return PrayerTimeCollection::fromResponse($response);
154
    }
155
156
    /**
157
     * Return list of tafsir based on the given ayat number.
158
     *
159
     * @param  int  $ayat
160
     * @return \Ianrizky\MoslemPray\Response\MyQuran\Collection\TafsirCollection
161
     *
162
     * @see https://api.myquran.com/v1/tafsir/quran/kemenag/id/{id}
163
     * @see https://documenter.getpostman.com/view/841292/Tz5p7yHS#9065c3f0-23b7-48b6-884a-a28da0826e03 (Tafsir/alQuran/Kemenag/id)
164
     */
165 1
    public function getTafsir(int $ayat): TafsirCollection
166
    {
167 1
        $response = $this->throwJsonError(
168 1
            $this->request->get('/tafsir/quran/kemenag/id/' . $ayat)
169
        );
170
171 1
        return TafsirCollection::fromResponse($response);
172
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177 12
    protected function throwJsonError(Response $response): Response
178
    {
179 12
        $status = data_get($response->json(), 'status', true);
180 12
        $message = data_get($response->json(), 'message', 'MyQuran API error');
181
182 12
        if (!$status) {
183 4
            throw new Exception($message);
184
        }
185
186 8
        return $response;
187
    }
188
}
189