Collection   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 162
Duplicated Lines 35.19 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 57
loc 162
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 1
A search() 0 6 1
A index() 0 15 1
A form() 0 14 3
A edit() 15 15 2
A add() 15 15 2
A delete() 13 13 2

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 as BaseController;
20
use Aviat\AnimeClient\Model\Anime as AnimeModel;
21
use Aviat\AnimeClient\Model\AnimeCollection as AnimeCollectionModel;
22
use Aviat\AnimeClient\UrlGenerator;
23
use Aviat\Ion\Di\ContainerInterface;
24
25
/**
26
 * Controller for Anime collection pages
27
 */
28
class Collection extends BaseController {
29
30
	/**
31
	 * The anime collection model
32
	 * @var AnimeCollectionModel $anime_collection_model
33
	 */
34
	private $anime_collection_model;
35
36
	/**
37
	 * The anime API model
38
	 * @var AnimeModel $anime_model
39
	 */
40
	private $anime_model;
41
42
	/**
43
	 * Data to ve sent to all routes in this controller
44
	 * @var array $base_data
45
	 */
46
	protected $base_data;
47
48
	/**
49
	 * Url Generator class
50
	 * @var UrlGenerator
51
	 */
52
	protected $urlGenerator;
53
54
	/**
55
	 * Constructor
56
	 *
57
	 * @param ContainerInterface $container
58
	 */
59 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...
60
	{
61
		parent::__construct($container);
62
63
		$this->urlGenerator = $container->get('url-generator');
64
		$this->anime_model = $container->get('anime-model');
65
		$this->anime_collection_model = $container->get('anime-collection-model');
66
		$this->base_data = array_merge($this->base_data, [
67
			'menu_name' => 'collection',
68
			'url_type' => 'anime',
69
			'other_type' => 'manga',
70
			'config' => $this->config,
71
		]);
72
	}
73
74
	/**
75
	 * Search for anime
76
	 *
77
	 * @return void
78
	 */
79
	public function search()
80
	{
81
		$queryParams = $this->request->getQueryParams();
82
		$query = $queryParams['query'];
83
		$this->outputJSON($this->anime_model->search($query));
84
	}
85
86
	/**
87
	 * Show the anime collection page
88
	 *
89
	 * @param string $view
90
	 * @return void
91
	 */
92
	public function index($view)
93
	{
94
		$view_map = [
95
			'' => 'cover',
96
			'list' => 'list'
97
		];
98
99
		$data = $this->anime_collection_model->get_collection();
100
101
		$this->outputHTML('collection/' . $view_map[$view], [
102
			'title' => $this->config->get('whose_list') . "'s Anime Collection",
103
			'sections' => $data,
104
			'genres' => $this->anime_collection_model->get_genre_list()
105
		]);
106
	}
107
108
	/**
109
	 * Show the anime collection add/edit form
110
	 *
111
	 * @param integer|null $id
112
	 * @return void
113
	 */
114
	public function form($id = NULL)
115
	{
116
		$this->set_session_redirect();
117
118
		$action = (is_null($id)) ? "Add" : "Edit";
119
120
		$this->outputHTML('collection/' . strtolower($action), [
121
			'action' => $action,
122
			'action_url' => $this->urlGenerator->full_url('collection/' . strtolower($action)),
123
			'title' => $this->config->get('whose_list') . " Anime Collection &middot; {$action}",
124
			'media_items' => $this->anime_collection_model->get_media_type_list(),
125
			'item' => ($action === "Edit") ? $this->anime_collection_model->get($id) : []
126
		]);
127
	}
128
129
	/**
130
	 * Update a collection item
131
	 *
132
	 * @return void
133
	 */
134 View Code Duplication
	public function edit()
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...
135
	{
136
		$data = $this->request->getParsedBody();
137
		if (array_key_exists('hummingbird_id', $data))
138
		{
139
			$this->anime_collection_model->update($data);
140
			$this->set_flash_message('Successfully updated collection item.', 'success');
141
		}
142
		else
143
		{
144
			$this->set_flash_message('Failed to update collection item', 'error');
145
		}
146
147
		$this->session_redirect();
148
	}
149
150
	/**
151
	 * Add a collection item
152
	 *
153
	 * @return void
154
	 */
155 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...
156
	{
157
		$data = $this->request->getParsedBody();
158
		if (array_key_exists('id', $data))
159
		{
160
			$this->anime_collection_model->add($data);
161
			$this->set_flash_message('Successfully added collection item', 'success');
162
		}
163
		else
164
		{
165
			$this->set_flash_message('Failed to add collection item.', 'error');
166
		}
167
168
		$this->session_redirect();
169
	}
170
171
	/**
172
	 * Remove a collection item
173
	 *
174
	 * @return void
175
	 */
176 View Code Duplication
	public function delete()
177
	{
178
		$data = $this->request->getParsedBody();
179
		if ( ! array_key_exists('hummingbird_id', $data))
180
		{
181
			$this->redirect("/collection/view", 303);
182
		}
183
184
		$this->anime_collection_model->delete($data);
185
		$this->set_flash_message("Successfully removed anime from collection.", 'success');
186
187
		$this->redirect("/collection/view", 303);
188
	}
189
}
190
// End of CollectionController.php