Manga::getManga()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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 Aviat\AnimeClient\API\Kitsu\Enum\MangaReadingStatus;
20
use Aviat\AnimeClient\API\Kitsu\Transformer;
21
use Aviat\Ion\Di\ContainerInterface;
22
23
/**
24
 * Model for handling requests dealing with the manga list
25
 */
26
class Manga extends API
27
{
28
29
	const READING = 'Reading';
30
	const PLAN_TO_READ = 'Plan to Read';
31
	const DROPPED = 'Dropped';
32
	const ON_HOLD = 'On Hold';
33
	const COMPLETED = 'Completed';
34
35
	/**
36
	 * Map API constants to display constants
37
	 * @var array
38
	 */
39
	protected $const_map = [
40
		MangaReadingStatus::READING => self::READING,
41
		MangaReadingStatus::PLAN_TO_READ => self::PLAN_TO_READ,
42
		MangaReadingStatus::ON_HOLD => self::ON_HOLD,
43
		MangaReadingStatus::DROPPED => self::DROPPED,
44
		MangaReadingStatus::COMPLETED => self::COMPLETED
45
	];
46
47
	protected $status_map = [
48
		'current' => self::READING,
49
		'planned' => self::PLAN_TO_READ,
50
		'completed' => self::COMPLETED,
51
		'on_hold' => self::ON_HOLD,
52
		'dropped' => self::DROPPED
53
	];
54
55
	/**
56
	 * @var Aviat\AnimeClient\API\Kitsu\KitsuModel
57
	 */
58
	protected $kitsuModel;
59
60
	public function __construct(ContainerInterface $container)
61
	{
62
		parent::__construct($container);
63
64
		$this->kitsuModel = $container->get('kitsu-model');
65
	}
66
67
	/**
68
	 * Get a category out of the full list
69
	 *
70
	 * @param string $status
71
	 * @return array
72
	 */
73
	public function getList($status)
74
	{
75
		$APIstatus = array_flip($this->const_map)[$status];
76
		$data = $this->kitsuModel->getMangaList($APIstatus);
77
		return $this->mapByStatus($data)[$status];
78
	}
79
80
	/**
81
	 * Get the details of a manga
82
	 *
83
	 * @param string $manga_id
84
	 * @return array
85
	 */
86
	public function getManga($manga_id)
87
	{
88
		return $this->kitsuModel->getManga($manga_id);
89
	}
90
91
	/**
92
	 * Create a new manga list item
93
	 *
94
	 * @param array $data
95
	 * @return bool
96
	 */
97
	public function createLibraryItem(array $data): bool
98
	{
99
		return $this->kitsuModel->createListItem($data);
100
	}
101
102
	/**
103
	 * Get information about a specific list item
104
	 * for editing/updating that item
105
	 *
106
	 * @param string $itemId
107
	 * @return array
108
	 */
109
	public function getLibraryItem(string $itemId): array
110
	{
111
		return $this->kitsuModel->getListItem($itemId);
112
	}
113
114
	/**
115
	 * Update a list entry
116
	 *
117
	 * @param array $data
118
	 * @return array
119
	 */
120
	public function updateLibraryItem(array $data): array
121
	{
122
		return $this->kitsuModel->updateListItem($data);
123
	}
124
125
	/**
126
	 * Remove a list entry
127
	 *
128
	 * @param string $itemId
129
	 * @return bool
130
	 */
131
	public function deleteLibraryItem(string $itemId): bool
132
	{
133
		return $this->kitsuModel->deleteListItem($itemId);
134
	}
135
136
	/**
137
	 * Search for anime by name
138
	 *
139
	 * @param string $name
140
	 * @return array
141
	 */
142
	public function search($name)
143
	{
144
		return $this->kitsuModel->search('manga', $name);
145
	}
146
147
	/**
148
	 * Map transformed anime data to be organized by reading status
149
	 *
150
	 * @param array $data
151
	 * @return array
152
	 */
153
	private function mapByStatus($data)
154
	{
155
		$output = [
156
			self::READING => [],
157
			self::PLAN_TO_READ => [],
158
			self::ON_HOLD => [],
159
			self::DROPPED => [],
160
			self::COMPLETED => [],
161
		];
162
163
		foreach ($data as &$entry) {
164
			$key = $this->status_map[$entry['reading_status']];
165
			$output[$key][] = $entry;
166
		}
167
168
		foreach ($output as &$val) {
169
			$this->sortByName($val, 'manga');
170
		}
171
172
		return $output;
173
	}
174
}
175
// End of MangaModel.php