Completed
Push — master ( 125148...6e4e8e )
by Timothy
16:08
created

Anime::form_update()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.8571
cc 3
eloc 14
nc 3
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
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();
1 ignored issue
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...
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_anime($id, $status);
177
		$raw_status_list = AnimeWatchingStatus::getConstList();
1 ignored issue
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...
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);
0 ignored issues
show
Unused Code introduced by
$full_result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
227
		$result = $result['body'];
0 ignored issues
show
Bug introduced by
The variable $result seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
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
// End of AnimeController.php