Manga   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 249
Duplicated Lines 36.55 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 91
loc 249
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
B index() 0 27 2
A add_form() 0 22 2
A add() 22 22 3
A edit() 0 14 1
A search() 0 5 1
A form_update() 23 23 2
A update() 16 16 2
A delete() 18 18 2
A details() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
/**
3
 * Anime List Client
4
 *
5
 * An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
6
 *
7
 * PHP version 7
8
 *
9
 * @package     AnimeListClient
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2015 - 2017  Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @version     4.0
14
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
15
 */
16
17
namespace Aviat\AnimeClient\Controller;
18
19
use Aviat\AnimeClient\Controller;
20
use Aviat\AnimeClient\API\Kitsu;
21
use Aviat\AnimeClient\API\Kitsu\Enum\MangaReadingStatus;
22
use Aviat\AnimeClient\API\Kitsu\Transformer\MangaListTransformer;
23
use Aviat\AnimeClient\Model\Manga as MangaModel;
24
use Aviat\Ion\Di\ContainerInterface;
25
use Aviat\Ion\{Json, StringWrapper};
26
27
/**
28
 * Controller for manga list
29
 */
30
class Manga extends Controller {
31
32
	use StringWrapper;
33
34
	/**
35
	 * The manga model
36
	 * @var MangaModel $model
37
	 */
38
	protected $model;
39
40
	/**
41
	 * Data to ve sent to all routes in this controller
42
	 * @var array $base_data
43
	 */
44
	protected $base_data;
45
46
	/**
47
	 * Constructor
48
	 *
49
	 * @param ContainerInterface $container
50
	 */
51 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...
52
	{
53
		parent::__construct($container);
54
55
		$this->model = $container->get('manga-model');
56
		$this->base_data = array_merge($this->base_data, [
57
			'menu_name' => 'manga_list',
58
			'config' => $this->config,
59
			'url_type' => 'manga',
60
			'other_type' => 'anime'
61
		]);
62
	}
63
64
	/**
65
	 * Get a section of the manga list
66
	 *
67
	 * @param string $status
68
	 * @param string $view
69
	 * @return void
70
	 */
71
	public function index($status = "all", $view = "")
72
	{
73
		$map = [
74
			'all' => 'All',
75
			'plan_to_read' => MangaModel::PLAN_TO_READ,
76
			'reading' => MangaModel::READING,
77
			'completed' => MangaModel::COMPLETED,
78
			'dropped' => MangaModel::DROPPED,
79
			'on_hold' => MangaModel::ON_HOLD
80
		];
81
82
		$title = $this->config->get('whose_list') . "'s Manga List &middot; {$map[$status]}";
83
84
		$view_map = [
85
			'' => 'cover',
86
			'list' => 'list'
87
		];
88
89
		$data = ($status !== 'all')
90
			? [$map[$status] => $this->model->getList($map[$status]) ]
91
			: $this->model->getList('All');
92
93
		$this->outputHTML('manga/' . $view_map[$view], [
94
			'title' => $title,
95
			'sections' => $data,
96
		]);
97
	}
98
99
	/**
100
	 * Form to add an manga
101
	 *
102
	 * @return void
103
	 */
104
	public function add_form()
105
	{
106
		$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...
107
108
		$statuses = [];
109
110
		foreach ($raw_status_list as $status_item)
111
		{
112
			$statuses[$status_item] = (string)$this->string($status_item)
113
				->underscored()
114
				->humanize()
115
				->titleize();
116
		}
117
118
		$this->set_session_redirect();
119
		$this->outputHTML('manga/add', [
120
			'title' => $this->config->get('whose_list') .
121
				"'s Manga List &middot; Add",
122
			'action_url' => $this->urlGenerator->url('manga/add'),
123
			'status_list' => $statuses
124
		]);
125
	}
126
127
	/**
128
	 * Add an manga to the list
129
	 *
130
	 * @return void
131
	 */
132 View Code Duplication
	public function add()
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...
133
	{
134
		$data = $this->request->getParsedBody();
135
		if ( ! array_key_exists('id', $data))
136
		{
137
			$this->redirect("manga/add", 303);
138
		}
139
140
		$result = $this->model->createLibraryItem($data);
141
142
		if ($result)
143
		{
144
			$this->set_flash_message('Added new manga to list', 'success');
145
			$this->cache->clear();
146
		}
147
		else
148
		{
149
			$this->set_flash_message('Failed to add new manga to list' . $result['body'], 'error');
150
		}
151
152
		$this->session_redirect();
153
	}
154
155
	/**
156
	 * Show the manga edit form
157
	 *
158
	 * @param string $id
159
	 * @param string $status
160
	 * @return void
161
	 */
162
	public function edit($id, $status = "All")
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
	{
164
		$this->set_session_redirect();
165
		$item = $this->model->getLibraryItem($id);
166
		$title = $this->config->get('whose_list') . "'s Manga List &middot; Edit";
167
168
		$this->outputHTML('manga/edit', [
169
			'title' => $title,
170
			'status_list' => Kitsu::getStatusToMangaSelectMap(),
171
			'item' => $item,
172
			'action' => $this->container->get('url-generator')
173
				->url('/manga/update_form'),
174
		]);
175
	}
176
177
	/**
178
	 * Search for a manga to add to the list
179
	 *
180
	 * @return void
181
	 */
182
	public function search()
183
	{
184
		$query_data = $this->request->getQueryParams();
185
		$this->outputJSON($this->model->search($query_data['query']));
186
	}
187
188
	/**
189
	 * Update an manga item via a form submission
190
	 *
191
	 * @return void
192
	 */
193 View Code Duplication
	public function form_update()
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...
194
	{
195
		$data = $this->request->getParsedBody();
196
197
		// Do some minor data manipulation for
198
		// large form-based updates
199
		$transformer = new MangaListTransformer();
200
		$post_data = $transformer->untransform($data);
201
		$full_result = $this->model->updateLibraryItem($post_data);
202
203
		if ($full_result['statusCode'] === 200)
204
		{
205
			$this->set_flash_message("Successfully updated manga.", 'success');
206
			$this->cache->clear();
207
		}
208
		else
209
		{
210
			$this->set_flash_message('Failed to update manga.', 'error');
211
212
		}
213
214
		$this->session_redirect();
215
	}
216
217
	/**
218
	 * Update a manga item
219
	 *
220
	 * @return void
221
	 */
222 View Code Duplication
	public function update()
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...
223
	{
224
		if ($this->request->getHeader('content-type')[0] === 'application/json')
225
		{
226
			$data = JSON::decode((string)$this->request->getBody());
227
		}
228
		else
229
		{
230
			$data = $this->request->getParsedBody();
231
		}
232
233
		$response = $this->model->updateLibraryItem($data);
234
235
		$this->cache->clear();
236
		$this->outputJSON($response['body'], $response['statusCode']);
237
	}
238
239
	/**
240
	 * Remove an manga from the list
241
	 *
242
	 * @return void
243
	 */
244 View Code Duplication
	public function delete()
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...
245
	{
246
		$body = $this->request->getParsedBody();
247
		$id = $body['id'];
248
		$response = $this->model->deleteLibraryItem($id);
249
250
		if ($response)
251
		{
252
			$this->set_flash_message("Successfully deleted manga.", 'success');
253
			$this->cache->clear();
254
		}
255
		else
256
		{
257
			$this->set_flash_message('Failed to delete manga.', 'error');
258
		}
259
260
		$this->session_redirect();
261
	}
262
263
	/**
264
	 * View details of an manga
265
	 *
266
	 * @param string $manga_id
267
	 * @return void
268
	 */
269
	public function details($manga_id)
270
	{
271
		$data = $this->model->getManga($manga_id);
272
273
		$this->outputHTML('manga/details', [
274
			'title' => 'Manga &middot; ' . $data['title'],
275
			'data' => $data,
276
		]);
277
	}
278
}
279
// End of MangaController.php