Completed
Push — master ( 5e0489...1b8ed5 )
by Timothy
02:40
created

Collection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 153
Duplicated Lines 23.53 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 9
Bugs 4 Features 0
Metric Value
wmc 12
c 9
b 4
f 0
lcom 1
cbo 5
dl 36
loc 153
rs 10

7 Methods

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