Completed
Branch develop (d8e67e)
by Timothy
04:10
created

Manga::add()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 22
rs 8.9197
cc 4
eloc 11
nc 4
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
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 cache manager
31
	 * @var \Aviat\Ion\Cache\CacheInterface
32
	 */
33
	protected $cache;
34
35
	/**
36
	 * The manga model
37
	 * @var object $model
38
	 */
39
	protected $model;
40
41
	/**
42
	 * Data to ve sent to all routes in this controller
43
	 * @var array $base_data
44
	 */
45
	protected $base_data;
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param ContainerInterface $container
51
	 */
52 View Code Duplication
	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...
53
	{
54
		parent::__construct($container);
55
56
		$this->cache = $container->get('cache');
57
		$this->model = $container->get('manga-model');
58
		$this->base_data = array_merge($this->base_data, [
59
			'menu_name' => 'manga_list',
60
			'config' => $this->config,
61
			'url_type' => 'manga',
62
			'other_type' => 'anime'
63
		]);
64
	}
65
66
	/**
67
	 * Get a section of the manga list
68
	 *
69
	 * @param string $status
70
	 * @param string $view
71
	 * @return void
72
	 */
73
	public function index($status = "all", $view = "")
74
	{
75
		$map = [
76
			'all' => 'All',
77
			'plan_to_read' => MangaModel::PLAN_TO_READ,
78
			'reading' => MangaModel::READING,
79
			'completed' => MangaModel::COMPLETED,
80
			'dropped' => MangaModel::DROPPED,
81
			'on_hold' => MangaModel::ON_HOLD
82
		];
83
84
		$title = $this->config->get('whose_list') . "'s Manga List &middot; {$map[$status]}";
85
86
		$view_map = [
87
			'' => 'cover',
88
			'list' => 'list'
89
		];
90
91
		$data = ($status !== 'all')
92
			? [$map[$status] => $this->cache->get($this->model, 'get_list', ['status' => $map[$status]]) ]
93
			: $this->cache->get($this->model, 'get_all_lists');
94
95
		$this->outputHTML('manga/' . $view_map[$view], [
96
			'title' => $title,
97
			'sections' => $data,
98
		]);
99
	}
100
101
	/**
102
	 * Form to add an manga
103
	 *
104
	 * @return void
105
	 */
106 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...
107
	{
108
		$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...
109
110
		$statuses = [];
111
112
		foreach ($raw_status_list as $status_item)
113
		{
114
			$statuses[$status_item] = (string)$this->string($status_item)
115
				->underscored()
116
				->humanize()
117
				->titleize();
118
		}
119
120
		$this->set_session_redirect();
121
		$this->outputHTML('manga/add', [
122
			'title' => $this->config->get('whose_list') .
123
				"'s Manga List &middot; Add",
124
			'action_url' => $this->urlGenerator->url('manga/add'),
125
			'status_list' => $statuses
126
		]);
127
	}
128
129
	/**
130
	 * Add an manga to the list
131
	 *
132
	 * @return void
133
	 */
134
	public function add()
135
	{
136
		$data = $this->request->getParsedBody();
137
		if ( ! array_key_exists('id', $data))
138
		{
139
			$this->redirect("manga/add", 303);
140
		}
141
142
		$result = $this->model->add($data);
143
144
		if ($result['statusCode'] >= 200 && $result['statusCode'] < 300)
145
		{
146
			$this->set_flash_message('Added new manga to list', 'success');
147
			$this->cache->purge();
148
		}
149
		else
150
		{
151
			$this->set_flash_message('Failed to add new manga to list' . $result['body'], 'error');
152
		}
153
154
		$this->session_redirect();
155
	}
156
157
	/**
158
	 * Show the manga edit form
159
	 *
160
	 * @param string $id
161
	 * @param string $status
162
	 * @return void
163
	 */
164
	public function edit($id, $status = "All")
165
	{
166
		$this->set_session_redirect();
167
		$item = $this->model->get_library_item($id, $status);
168
		$title = $this->config->get('whose_list') . "'s Manga List &middot; Edit";
169
170
		$this->outputHTML('manga/edit', [
171
			'title' => $title,
172
			'status_list' => MangaReadingStatus::getConstList(),
173
			'item' => $item,
174
			'action' => $this->container->get('url-generator')
175
				->url('/manga/update_form'),
176
		]);
177
	}
178
179
	/**
180
	 * Search for a manga to add to the list
181
	 *
182
	 * @return void
183
	 */
184
 	public function search()
185
 	{
186
		$query_data = $this->request->getQueryParams();
187
 		$this->outputJSON($this->model->search($query_data['query']));
188
 	}
189
190
	/**
191
	 * Update an anime item via a form submission
192
	 *
193
	 * @return void
194
	 */
195
	public function form_update()
196
	{
197
		$post_data = $this->request->getParsedBody();
198
199
		// Do some minor data manipulation for
200
		// large form-based updates
201
		$transformer = new MangaListTransformer();
202
		$post_data = $transformer->untransform($post_data);
203
		$full_result = $this->model->update($post_data);
204
205
		$result = Json::decode((string)$full_result['body']);
206
207
		if ($full_result['statusCode'] == 200)
208
		{
209
			$m =& $result['manga'][0];
210
			$title = ( ! empty($m['english_title']))
211
				? "{$m['romaji_title']} ({$m['english_title']})"
212
				: "{$m['romaji_title']}";
213
214
			$this->set_flash_message("Successfully updated {$title}.", 'success');
215
			$this->cache->purge();
216
		}
217
		else
218
		{
219
			$this->set_flash_message('Failed to update manga.', 'error');
220
		}
221
222
		$this->session_redirect();
223
	}
224
225
	/**
226
	 * Update an anime item
227
	 *
228
	 * @return boolean|null
229
	 */
230
	public function update()
231
	{
232
		$result = $this->model->update($this->request->getParsedBody());
233
		$this->cache->purge();
234
		$this->outputJSON($result['body'], $result['statusCode']);
235
	}
236
}
237
// End of MangaController.php