Completed
Branch master (1afb45)
by Timothy
04:13
created

Anime::get_raw_list()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * Hummingbird Anime Client
4
 *
5
 * An API client for Hummingbird to manage anime and manga watch lists
6
 *
7
 * @package     HummingbirdAnimeClient
8
 * @author      Timothy J. Warren
9
 * @copyright   Copyright (c) 2015 - 2016
10
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
11
 * @license     MIT
12
 */
13
14
namespace Aviat\AnimeClient\Model;
15
16
use Aviat\Ion\Json;
17
use Aviat\AnimeClient\Hummingbird\Enum\AnimeWatchingStatus;
18
use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer;
19
20
/**
21
 * Model for handling requests dealing with the anime list
22
 */
23
class Anime extends API {
24
25
	// Display constants
26
	const WATCHING = 'Watching';
27
	const PLAN_TO_WATCH = 'Plan to Watch';
28
	const DROPPED = 'Dropped';
29
	const ON_HOLD = 'On Hold';
30
	const COMPLETED = 'Completed';
31
32
	/**
33
	 * The base url for api requests
34
	 * @var string $base_url
35
	 */
36
	protected $base_url = "https://hummingbird.me/api/v1/";
37
38
	/**
39
	 * Map of API status constants to display constants
40
	 * @var array
41
	 */
42
	protected $const_map = [
43
		AnimeWatchingStatus::WATCHING => self::WATCHING,
44
		AnimeWatchingStatus::PLAN_TO_WATCH => self::PLAN_TO_WATCH,
45
		AnimeWatchingStatus::ON_HOLD => self::ON_HOLD,
46
		AnimeWatchingStatus::DROPPED => self::DROPPED,
47
		AnimeWatchingStatus::COMPLETED => self::COMPLETED,
48
	];
49
50
	/**
51
	 * Update the selected anime
52
	 *
53
	 * @param array $data
54
	 * @return array|false
55
	 */
56 View Code Duplication
	public function update($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
	{
58
		$auth = $this->container->get('auth');
59
		if ( ! $auth->is_authenticated() || ! array_key_exists('id', $data))
60
		{
61
			return FALSE;
62
		}
63
64
		$id = $data['id'];
65
		$data['auth_token'] = $auth->get_auth_token();
66
67
		$response = $this->client->post("libraries/{$id}", [
68
			'form_params' => $data
69
		]);
70
71
		return [
72
			'statusCode' => $response->getStatusCode(),
73
			'body' => Json::decode($response->getBody(), TRUE)
74
		];
75
	}
76
77
	/**
78
	 * Remove an anime from a list
79
	 *
80
	 * @param  array $data
81
	 * @return array|false
82
	 */
83 View Code Duplication
	public function delete($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
	{
85
		$auth = $this->container->get('auth');
86
		if ( ! $auth->is_authenticated() || ! array_key_exists('id', $data))
87
		{
88
			return FALSE;
89
		}
90
91
		$id = $data['id'];
92
		$data['auth_token'] = $auth->get_auth_token();
93
94
		$response = $this->client->post("libraries/{$id}/remove", [
95
			'form_params' => $data
96
		]);
97
98
		return [
99
			'statusCode' => $response->getStatusCode(),
100
			'body' => Json::decode($response->getBody(), TRUE)
101
		];
102
	}
103
104
	/**
105
	 * Get the full set of anime lists
106
	 *
107
	 * @return array
108
	 */
109
	public function get_all_lists()
110
	{
111
		$output = [
112
			self::WATCHING => [],
113
			self::PLAN_TO_WATCH => [],
114
			self::ON_HOLD => [],
115
			self::DROPPED => [],
116
			self::COMPLETED => [],
117
		];
118
119
		$data = $this->_get_list_from_api();
120
121
		foreach ($data as $datum)
122
		{
123
			$output[$this->const_map[$datum['watching_status']]][] = $datum;
124
		}
125
126
		// Sort anime by name
127
		foreach ($output as &$status_list)
128
		{
129
			$this->sort_by_name($status_list, 'anime');
130
		}
131
132
		return $output;
133
	}
134
135
	/**
136
	 * Get a category out of the full list
137
	 *
138
	 * @param string $status
139
	 * @return array
140
	 */
141
	public function get_list($status)
142
	{
143
		$data = $this->_get_list_from_api($status);
144
		$this->sort_by_name($data, 'anime');
145
146
		$output = [];
147
		$output[$this->const_map[$status]] = $data;
148
149
		return $output;
150
	}
151
152
	/**
153
	 * Get information about an anime from its id
154
	 *
155
	 * @param string $anime_id
156
	 * @return array
157
	 */
158
	public function get_anime($anime_id)
159
	{
160
		$config = [
161
			'query' => [
162
				'id' => $anime_id
163
			]
164
		];
165
166
		$response = $this->client->get("anime/{$anime_id}", $config);
167
168
		return Json::decode($response->getBody(), TRUE);
169
	}
170
171
	/**
172
	 * Search for anime by name
173
	 *
174
	 * @param string $name
175
	 * @return array
176
	 * @throws RuntimeException
177
	 */
178
	public function search($name)
179
	{
180
		$logger = $this->container->getLogger('default');
181
182
		$config = [
183
			'query' => [
184
				'query' => $name
185
			]
186
		];
187
188
		$response = $this->get('search/anime', $config);
189
190 View Code Duplication
		if ($response->getStatusCode() != 200)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
		{
192
			$logger->warning("Non 200 response for search api call");
193
			$logger->warning($response->getBody());
194
195
			throw new RuntimeException($response->getEffectiveUrl());
196
		}
197
198
		return Json::decode($response->getBody(), TRUE);
199
	}
200
201
	/**
202
	 * Retrieve data from the api
203
	 *
204
	 * @codeCoverageIgnore
205
	 * @param string $status
206
	 * @return array
207
	 */
208
	protected function _get_list_from_api($status = "all")
209
	{
210
		$config = [
211
			'allow_redirects' => FALSE
212
		];
213
214
		if ($status != "all")
215
		{
216
			$config['query']['status'] = $status;
217
		}
218
219
		$username = $this->config->get('hummingbird_username');
220
		$auth = $this->container->get('auth');
221
		if ($auth->is_authenticated())
222
		{
223
			$config['query']['auth_token'] = $auth->get_auth_token();
224
		}
225
226
		$response = $this->get("users/{$username}/library", $config);
227
		$output = $this->transform($status, $response);
228
229
		foreach ($output as &$row)
230
		{
231
			$row['anime']['image'] = $this->get_cached_image($row['anime']['image'], $row['anime']['slug'], 'anime');
232
		}
233
234
		return $output;
235
	}
236
237
	/**
238
	 * Get the full list from the api
239
	 *
240
	 * @return array
241
	 */
242
	public function get_raw_list()
243
	{
244
		$config = [
245
			'allow_redirects' => FALSE
246
		];
247
248
		$username = $this->config->get('hummingbird_username');
249
		/*$auth = $this->container->get('auth');
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
250
		if ($auth->is_authenticated())
251
		{
252
			$config['query']['auth_token'] = $auth->get_auth_token();
253
		}*/
254
255
		$response = $this->get("users/{$username}/library", $config);
256
		return Json::decode($response->getBody(), TRUE);
257
	}
258
259
	/**
260
	 * Handle transforming of api data
261
	 *
262
	 * @param string $status
263
	 * @param \GuzzleHttp\Message\Response
264
	 * @return array
265
	 */
266
	protected function transform($status, $response)
267
	{
268
		$api_data = Json::decode($response->getBody(), TRUE);
269
		$transformer = new AnimeListTransformer();
270
		$transformed = $transformer->transform_collection($api_data);
271
		return $transformed;
272
	}
273
}
274
// End of AnimeModel.php