Completed
Push — develop ( 49bd46...d746de )
by Timothy
03:17
created

Manga::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
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\Di\ContainerInterface;
16
use Aviat\AnimeClient\Controller;
17
use Aviat\AnimeClient\Config;
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 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 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...
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_all_lists();
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->post->get();
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
		}
141
		else
142
		{
143
			$this->set_flash_message('Failed to add new manga to list' . $result['body'] , 'error');
144
		}
145
146
		$this->session_redirect();
147
	}
148
149
	/**
150
	 * Show the manga edit form
151
	 *
152
	 * @param string $id
153
	 * @param string $status
154
	 * @return void
155
	 */
156
	public function edit($id, $status = "All")
157
	{
158
		$this->set_session_redirect();
159
		$item = $this->model->get_library_item($id, $status);
160
		$title = $this->config->get('whose_list') . "'s Manga List &middot; Edit";
161
162
		$this->outputHTML('manga/edit', [
163
			'title' => $title,
164
			'status_list' => MangaReadingStatus::getConstList(),
165
			'item' => $item,
166
			'action' => $this->container->get('url-generator')
167
				->url('/manga/update_form'),
168
		]);
169
	}
170
171
	/**
172
	 * Search for a manga to add to the list
173
	 *
174
	 * @param  string $query
0 ignored issues
show
Bug introduced by
There is no parameter named $query. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
175
	 * @return void
176
	 */
177
 	public function search()
178
 	{
179
 		$query = $this->request->query->get('query');
180
 		$this->outputJSON($this->model->search($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->post->get();
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 = $full_result['body'];
199
200
		if (array_key_exists('manga', $result))
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
		}
209
		else
210
		{
211
			$this->set_flash_message('Failed to update manga.', 'error');
212
		}
213
214
		$this->session_redirect();
215
	}
216
217
	/**
218
	 * Update an anime item
219
	 *
220
	 * @return boolean|null
221
	 */
222
	public function update()
223
	{
224
		$result = $this->model->update($this->request->post->get());
225
		$this->outputJSON($result['body'], $result['statusCode']);
226
	}
227
}
228
// End of MangaController.php