Completed
Push — develop ( e59ead...daf4b7 )
by Timothy
07:58
created

Anime   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 15
Bugs 4 Features 3
Metric Value
wmc 17
c 15
b 4
f 3
lcom 1
cbo 8
dl 0
loc 250
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A add_form() 0 22 2
A __construct() 0 13 1
B index() 0 44 3
A add() 0 21 3
B edit() 0 26 2
A search() 0 5 1
B form_update() 0 27 3
A update() 0 8 1
A details() 0 9 1
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\Controller;
15
16
use Aviat\Ion\Di\ContainerInterface;
17
use Aviat\AnimeClient\Controller as BaseController;
18
use Aviat\AnimeClient\Hummingbird\Enum\AnimeWatchingStatus;
19
use Aviat\AnimeClient\Model\Anime as AnimeModel;
20
use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer;
21
22
/**
23
 * Controller for Anime-related pages
24
 */
25
class Anime extends BaseController {
26
27
	use \Aviat\Ion\StringWrapper;
28
29
	/**
30
	 * The anime list model
31
	 * @var object $model
32
	 */
33
	protected $model;
34
35
	/**
36
	 * Data to ve sent to all routes in this controller
37
	 * @var array $base_data
38
	 */
39
	protected $base_data;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param ContainerInterface $container
45
	 */
46
	public function __construct(ContainerInterface $container)
47
	{
48
		parent::__construct($container);
49
50
		$this->model = $container->get('anime-model');
51
52
		$this->base_data = array_merge($this->base_data, [
53
			'menu_name' => 'anime_list',
54
			'url_type' => 'anime',
55
			'other_type' => 'manga',
56
			'config' => $this->config,
57
		]);
58
	}
59
60
	/**
61
	 * Show a portion, or all of the anime list
62
	 *
63
	 * @param string $type - The section of the list
64
	 * @param string $view - List or cover view
65
	 * @return void
66
	 */
67
	public function index($type = "watching", $view = '')
68
	{
69
		$type_title_map = [
70
			'all' => 'All',
71
			'watching' => 'Currently Watching',
72
			'plan_to_watch' => 'Plan to Watch',
73
			'on_hold' => 'On Hold',
74
			'dropped' => 'Dropped',
75
			'completed' => 'Completed'
76
		];
77
78
		$model_map = [
79
			'watching' => AnimeWatchingStatus::WATCHING,
80
			'plan_to_watch' => AnimeWatchingStatus::PLAN_TO_WATCH,
81
			'on_hold' => AnimeWatchingStatus::ON_HOLD,
82
			'all' => 'all',
83
			'dropped' => AnimeWatchingStatus::DROPPED,
84
			'completed' => AnimeWatchingStatus::COMPLETED
85
		];
86
87
		if (array_key_exists($type, $type_title_map))
88
		{
89
			$title = $this->config->get('whose_list') .
90
				"'s Anime List &middot; {$type_title_map[$type]}";
91
		}
92
		else
93
		{
94
			$title = '';
95
		}
96
97
		$view_map = [
98
			'' => 'cover',
99
			'list' => 'list'
100
		];
101
102
		$data = ($type != 'all')
103
			? $this->model->get_list($model_map[$type])
104
			: $this->model->get_all_lists();
105
106
		$this->outputHTML('anime/' . $view_map[$view], [
107
			'title' => $title,
108
			'sections' => $data
109
		]);
110
	}
111
112
	/**
113
	 * Form to add an anime
114
	 *
115
	 * @return void
116
	 */
117
	public function add_form()
118
	{
119
		$raw_status_list = AnimeWatchingStatus::getConstList();
120
121
		$statuses = [];
122
123
		foreach ($raw_status_list as $status_item)
124
		{
125
			$statuses[$status_item] = (string)$this->string($status_item)
126
				->underscored()
127
				->humanize()
128
				->titleize();
129
		}
130
131
		$this->set_session_redirect();
132
		$this->outputHTML('anime/add', [
133
			'title' => $this->config->get('whose_list') .
134
				"'s Anime List &middot; Add",
135
			'action_url' => $this->urlGenerator->url('anime/add'),
136
			'status_list' => $statuses
137
		]);
138
	}
139
140
	/**
141
	 * Add an anime to the list
142
	 *
143
	 * @return void
144
	 */
145
	public function add()
146
	{
147
		$data = $this->request->post->get();
148
		if ( ! array_key_exists('id', $data))
149
		{
150
			$this->redirect("anime/add", 303);
151
		}
152
153
		$result = $this->model->update($data);
154
155
		if ($result['statusCode'] == 201)
156
		{
157
			$this->set_flash_message('Added new anime to list', 'success');
158
		}
159
		else
160
		{
161
			$this->set_flash_message('Failed to add new anime to list', 'error');
162
		}
163
164
		$this->session_redirect();
165
	}
166
167
	/**
168
	 * Form to edit details about a series
169
	 *
170
	 * @param int $id
171
	 * @param string $status
172
	 * @return void
173
	 */
174
	public function edit($id, $status = "all")
175
	{
176
		$item = $this->model->get_library_item($id, $status);
177
		$raw_status_list = AnimeWatchingStatus::getConstList();
178
179
		$statuses = [];
180
181
		foreach ($raw_status_list as $status_item)
182
		{
183
			$statuses[$status_item] = (string)$this->string($status_item)
184
				->underscored()
185
				->humanize()
186
				->titleize();
187
		}
188
189
		$this->set_session_redirect($this->request->server->get('HTTP_REFERRER'));
190
191
		$this->outputHTML('anime/edit', [
192
			'title' => $this->config->get('whose_list') .
193
				"'s Anime List &middot; Edit",
194
			'item' => $item,
195
			'statuses' => $statuses,
196
			'action' => $this->container->get('url-generator')
197
				->url('/anime/update_form'),
198
		]);
199
	}
200
201
	/**
202
	 * Search for anime
203
	 *
204
	 * @return void
205
	 */
206
	public function search()
207
	{
208
		$query = $this->request->query->get('query');
209
		$this->outputJSON($this->model->search($query));
210
	}
211
212
	/**
213
	 * Update an anime item via a form submission
214
	 *
215
	 * @return void
216
	 */
217
	public function form_update()
218
	{
219
		$post_data = $this->request->post->get();
220
221
		// Do some minor data manipulation for
222
		// large form-based updates
223
		$transformer = new AnimeListTransformer();
224
		$post_data = $transformer->untransform($post_data);
225
226
		$full_result = $this->model->update($post_data);
227
		$result = $full_result['body'];
228
229
		if (array_key_exists('anime', $result))
230
		{
231
			$title = ( ! empty($result['anime']['alternate_title']))
232
				? "{$result['anime']['title']} ({$result['anime']['alternate_title']})"
233
				: "{$result['anime']['title']}";
234
235
			$this->set_flash_message("Successfully updated {$title}.", 'success');
236
		}
237
		else
238
		{
239
			$this->set_flash_message('Failed to update anime.', 'error');
240
		}
241
242
		$this->session_redirect();
243
	}
244
245
	/**
246
	 * Update an anime item
247
	 *
248
	 * @return boolean|null
249
	 */
250
	public function update()
251
	{
252
		$this->outputJSON(
253
			$this->model->update(
254
				$this->request->post->get()
255
			)
256
		);
257
	}
258
259
	/**
260
	 * View details of an anime
261
	 *
262
	 * @param string anime_id
263
	 * @return void
264
	 */
265
	public function details($anime_id)
266
	{
267
		$data = $this->model->get_anime($anime_id);
268
269
		$this->outputHTML('anime/details', [
270
			'title' => $data['title'],
271
			'data' => $data,
272
		]);
273
	}
274
}
275
// End of AnimeController.php