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

Manga::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 1 Features 2
Metric Value
c 8
b 1
f 2
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 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
namespace Aviat\AnimeClient\Controller;
14
15
use Aviat\Ion\Json;
16
use Aviat\Ion\Di\ContainerInterface;
17
use Aviat\AnimeClient\Controller;
18
use Aviat\AnimeClient\Model\Manga as MangaModel;
19
use Aviat\AnimeClient\Hummingbird\Enum\MangaReadingStatus;
20
use Aviat\AnimeClient\Hummingbird\Transformer\MangaListTransformer;
21
22
/**
23
 * Controller for manga list
24
 */
25
class Manga extends Controller {
26
27
	use \Aviat\Ion\StringWrapper;
28
29
	/**
30
	 * The manga model
31
	 * @var MangaModel $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)
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...
47
	{
48
		parent::__construct($container);
49
50
		$this->model = $container->get('manga-model');
51
		$this->base_data = array_merge($this->base_data, [
52
			'menu_name' => 'manga_list',
53
			'config' => $this->config,
54
			'url_type' => 'manga',
55
			'other_type' => 'anime'
56
		]);
57
	}
58
59
	/**
60
	 * Get a section of the manga list
61
	 *
62
	 * @param string $status
63
	 * @param string $view
64
	 * @return void
65
	 */
66
	public function index($status = "all", $view = "")
67
	{
68
		$map = [
69
			'all' => 'All',
70
			'plan_to_read' => MangaModel::PLAN_TO_READ,
71
			'reading' => MangaModel::READING,
72
			'completed' => MangaModel::COMPLETED,
73
			'dropped' => MangaModel::DROPPED,
74
			'on_hold' => MangaModel::ON_HOLD
75
		];
76
77
		$title = $this->config->get('whose_list') . "'s Manga List &middot; {$map[$status]}";
78
79
		$view_map = [
80
			'' => 'cover',
81
			'list' => 'list'
82
		];
83
84
		$data = ($status !== 'all')
85
			? [$map[$status] => $this->model->get_list($map[$status]) ]
86
			: $this->model->get_list('All');
87
88
		$this->outputHTML('manga/' . $view_map[$view], [
89
			'title' => $title,
90
			'sections' => $data,
91
		]);
92
	}
93
94
	/**
95
	 * Form to add an manga
96
	 *
97
	 * @return void
98
	 */
99 View Code Duplication
	public function add_form()
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...
100
	{
101
		$raw_status_list = MangaReadingStatus::getConstList();
0 ignored issues
show
Bug introduced by
The method getConstList() cannot be called from this context as it is declared protected in class Aviat\Ion\Enum.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
102
103
		$statuses = [];
104
105
		foreach ($raw_status_list as $status_item)
106
		{
107
			$statuses[$status_item] = (string)$this->string($status_item)
108
				->underscored()
109
				->humanize()
110
				->titleize();
111
		}
112
113
		$this->set_session_redirect();
114
		$this->outputHTML('manga/add', [
115
			'title' => $this->config->get('whose_list') .
116
				"'s Manga List &middot; Add",
117
			'action_url' => $this->urlGenerator->url('manga/add'),
118
			'status_list' => $statuses
119
		]);
120
	}
121
122
	/**
123
	 * Add an manga to the list
124
	 *
125
	 * @return void
126
	 */
127
	public function add()
128
	{
129
		$data = $this->request->getParsedBody();
130
		if ( ! array_key_exists('id', $data))
131
		{
132
			$this->redirect("manga/add", 303);
133
		}
134
135
		$result = $this->model->add($data);
136
137
		if ($result['statusCode'] >= 200 && $result['statusCode'] < 300)
138
		{
139
			$this->set_flash_message('Added new manga to list', 'success');
140
			$this->cache->purge();
141
		}
142
		else
143
		{
144
			$this->set_flash_message('Failed to add new manga to list' . $result['body'], 'error');
145
		}
146
147
		$this->session_redirect();
148
	}
149
150
	/**
151
	 * Show the manga edit form
152
	 *
153
	 * @param string $id
154
	 * @param string $status
155
	 * @return void
156
	 */
157
	public function edit($id, $status = "All")
158
	{
159
		$this->set_session_redirect();
160
		$item = $this->model->get_library_item($id, $status);
161
		$title = $this->config->get('whose_list') . "'s Manga List &middot; Edit";
162
163
		$this->outputHTML('manga/edit', [
164
			'title' => $title,
165
			'status_list' => MangaReadingStatus::getConstList(),
166
			'item' => $item,
167
			'action' => $this->container->get('url-generator')
168
				->url('/manga/update_form'),
169
		]);
170
	}
171
172
	/**
173
	 * Search for a manga to add to the list
174
	 *
175
	 * @return void
176
	 */
177
 	public function search()
178
 	{
179
		$query_data = $this->request->getQueryParams();
180
 		$this->outputJSON($this->model->search($query_data['query']));
181
 	}
182
183
	/**
184
	 * Update an anime item via a form submission
185
	 *
186
	 * @return void
187
	 */
188
	public function form_update()
189
	{
190
		$post_data = $this->request->getParsedBody();
191
192
		// Do some minor data manipulation for
193
		// large form-based updates
194
		$transformer = new MangaListTransformer();
195
		$post_data = $transformer->untransform($post_data);
196
		$full_result = $this->model->update($post_data);
197
198
		$result = Json::decode((string)$full_result['body']);
199
200
		if ($full_result['statusCode'] == 200)
201
		{
202
			$m =& $result['manga'][0];
203
			$title = ( ! empty($m['english_title']))
204
				? "{$m['romaji_title']} ({$m['english_title']})"
205
				: "{$m['romaji_title']}";
206
207
			$this->set_flash_message("Successfully updated {$title}.", 'success');
208
			$this->cache->purge();
209
		}
210
		else
211
		{
212
			$this->set_flash_message('Failed to update manga.', 'error');
213
		}
214
215
		$this->session_redirect();
216
	}
217
218
	/**
219
	 * Update an anime item
220
	 *
221
	 * @return boolean|null
222
	 */
223
	public function update()
224
	{
225
		$result = $this->model->update($this->request->getParsedBody());
226
		$this->cache->purge();
227
		$this->outputJSON($result['body'], $result['statusCode']);
228
	}
229
230
	/**
231
	 * Remove an manga from the list
232
	 */
233
	public function delete()
234
	{
235
		$response = $this->model->delete($this->request->getParsedBody());
236
237
		if ($response['body'] == TRUE)
238
		{
239
			$this->set_flash_message("Successfully deleted manga.", 'success');
240
			$this->cache->purge();
241
		}
242
		else
243
		{
244
			$this->set_flash_message('Failed to delete manga.', 'error');
245
		}
246
247
		$this->session_redirect();
248
	}
249
250
	/**
251
	 * View details of an manga
252
	 *
253
	 * @param string $manga_id
254
	 * @return void
255
	 */
256
	public function details($manga_id)
257
	{
258
		$data = $this->model->get_manga($manga_id);
259
260
		$this->outputHTML('manga/details', [
261
			'title' => 'Manga &middot; ' . $data['manga']['romaji_title'],
262
			'data' => $data['manga'],
263
		]);
264
	}
265
}
266
// End of MangaController.php