Anime::getList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * Anime List Client
4
 *
5
 * An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
6
 *
7
 * PHP version 7
8
 *
9
 * @package     AnimeListClient
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2015 - 2017  Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @version     4.0
14
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
15
 */
16
17
namespace Aviat\AnimeClient\Model;
18
19
use function Amp\some;
20
use function Amp\wait;
21
use Amp\Artax\Client;
22
use Aviat\AnimeClient\API\Kitsu\Enum\AnimeWatchingStatus;
23
use Aviat\Ion\Di\ContainerInterface;
24
use Aviat\Ion\Json;
25
26
/**
27
 * Model for handling requests dealing with the anime list
28
 */
29
class Anime extends API {
30
31
	// Display constants
32
	const WATCHING = 'Watching';
33
	const PLAN_TO_WATCH = 'Plan to Watch';
34
	const DROPPED = 'Dropped';
35
	const ON_HOLD = 'On Hold';
36
	const COMPLETED = 'Completed';
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
	protected $kitsuModel;
51
52
	protected $malModel;
53
54
	protected $useMALAPI;
55
56
	/**
57
	 * Anime constructor.
58
	 * @param ContainerInterface $container
59
	 */
60
	public function __construct(ContainerInterface $container) {
61
		parent::__construct($container);
62
63
		$config = $container->get('config');
64
		$this->kitsuModel = $container->get('kitsu-model');
65
		$this->malModel = $container->get('mal-model');
66
67
		$this->useMALAPI = $config->get(['use_mal_api']) === TRUE;
68
	}
69
70
	/**
71
	 * Get a category out of the full list
72
	 *
73
	 * @param string $status
74
	 * @return array
75
	 */
76
	public function getList($status)
77
	{
78
		$data = $this->kitsuModel->getAnimeList($status);
79
		$this->sortByName($data, 'anime');
80
81
		$output = [];
82
		$output[$this->const_map[$status]] = $data;
83
84
		return $output;
85
	}
86
87
	/**
88
	 * Get information about an anime from its slug
89
	 *
90
	 * @param string $slug
91
	 * @return array
92
	 */
93
	public function getAnime($slug)
94
	{
95
		return $this->kitsuModel->getAnime($slug);
96
	}
97
98
	/**
99
	 * Get anime by its kitsu id
100
	 *
101
	 * @param string $animeId
102
	 * @return array
103
	 */
104
	public function getAnimeById($animeId)
105
	{
106
		return $this->kitsuModel->getAnimeById($animeId);
107
	}
108
109
	/**
110
	 * Search for anime by name
111
	 *
112
	 * @param string $name
113
	 * @return array
114
	 */
115
	public function search($name)
116
	{
117
		return $this->kitsuModel->search('anime', $name);
118
	}
119
120
	/**
121
	 * Get information about a specific list item
122
	 * for editing/updating that item
123
	 *
124
	 * @param string $itemId
125
	 * @return array
126
	 */
127
	public function getLibraryItem(string $itemId): array
128
	{
129
		return $this->kitsuModel->getListItem($itemId);
130
	}
131
132
	/**
133
	 * Add an anime to your list
134
	 *
135
	 * @param array $data
136
	 * @return bool
137
	 */
138
	public function createLibraryItem(array $data): bool
139
	{
140
		$requests = [];
141
142
		if ($this->useMALAPI)
143
		{
144
			$malData = $data;
145
			$malId = $this->kitsuModel->getMalIdForAnime($malData['id']);
146
147
			if ( ! is_null($malId))
148
			{
149
				$malData['id'] = $malId;
150
				$requests['mal'] = $this->malModel->createListItem($malData);
151
			}
152
		}
153
154
		$requests['kitsu'] = $this->kitsuModel->createListItem($data);
155
156
		$promises = (new Client)->requestMulti($requests);
157
158
		$results = wait(some($promises));
159
160
		return count($results[1]) > 0;
161
	}
162
163
	/**
164
	 * Update a list entry
165
	 *
166
	 * @param array $data
167
	 * @return array
168
	 */
169
	public function updateLibraryItem(array $data): array
170
	{
171
		$requests = []; 
172
		
173
		if ($this->useMALAPI)
174
		{
175
			$requests['mal'] = $this->malModel->updateListItem($data);
176
		}
177
178
		$requests['kitsu'] = $this->kitsuModel->updateListItem($data);
179
		
180
		$promises = (new Client)->requestMulti($requests);
181
182
		$results = wait(some($promises));
183
		
184
		return [
185
			'body' => Json::decode($results[1]['kitsu']->getBody()),
186
			'statusCode' => $results[1]['kitsu']->getStatus()
187
		];
188
	}
189
190
	/**
191
	 * Delete a list entry
192
	 *
193
	 * @param string $id
194
	 * @param string|null $malId
195
	 * @return bool
196
	 */
197
	public function deleteLibraryItem(string $id, string $malId = null): bool
198
	{
199
		$requests = [];
200
201
		if ($this->useMALAPI && ! is_null($malId))
202
		{
203
			$requests['mal'] = $this->malModel->deleteListItem($malId);
204
		}
205
206
		$requests['kitsu'] = $this->kitsuModel->deleteListItem($id);
207
208
		$results = wait(some((new Client)->requestMulti($requests)));
209
210
		return count($results[1]) > 0;
211
	}
212
}
213
// End of AnimeModel.php