These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | |||
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 | 'url_type' => 'anime', |
||
67 | 'other_type' => 'manga', |
||
68 | 'config' => $this->config, |
||
69 | ]); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Search for anime |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | public function search() |
||
78 | { |
||
79 | $query = $this->request->query->get('query'); |
||
80 | $this->outputJSON($this->anime_model->search($query)); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Show the anime collection page |
||
85 | * |
||
86 | * @param string $view |
||
87 | * @return void |
||
88 | */ |
||
89 | public function index($view) |
||
90 | { |
||
91 | $view_map = [ |
||
92 | '' => 'cover', |
||
93 | 'list' => 'list' |
||
94 | ]; |
||
95 | |||
96 | $data = $this->anime_collection_model->get_collection(); |
||
97 | |||
98 | $this->outputHTML('collection/' . $view_map[$view], [ |
||
99 | 'title' => $this->config->get('whose_list') . "'s Anime Collection", |
||
100 | 'sections' => $data, |
||
101 | 'genres' => $this->anime_collection_model->get_genre_list() |
||
102 | ]); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Show the anime collection add/edit form |
||
107 | * |
||
108 | * @param integer|null $id |
||
109 | * @return void |
||
110 | */ |
||
111 | public function form($id = NULL) |
||
112 | { |
||
113 | $this->set_session_redirect(); |
||
114 | |||
115 | $action = (is_null($id)) ? "Add" : "Edit"; |
||
116 | |||
117 | $this->outputHTML('collection/' . strtolower($action), [ |
||
118 | 'action' => $action, |
||
119 | 'action_url' => $this->urlGenerator->full_url('collection/' . strtolower($action)), |
||
120 | 'title' => $this->config->get('whose_list') . " Anime Collection · {$action}", |
||
121 | 'media_items' => $this->anime_collection_model->get_media_type_list(), |
||
122 | 'item' => ($action === "Edit") ? $this->anime_collection_model->get($id) : [] |
||
123 | ]); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Update a collection item |
||
128 | * |
||
129 | * @return void |
||
130 | */ |
||
131 | View Code Duplication | public function edit() |
|
0 ignored issues
–
show
|
|||
132 | { |
||
133 | $data = $this->request->post->get(); |
||
134 | if (array_key_exists('hummingbird_id', $data)) |
||
135 | { |
||
136 | $this->anime_collection_model->update($data); |
||
137 | $this->set_flash_message('Successfully updated collection item.', 'success'); |
||
138 | } |
||
139 | else |
||
140 | { |
||
141 | $this->set_flash_message('Failed to update collection item', 'error'); |
||
142 | } |
||
143 | |||
144 | $this->session_redirect(); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Add a collection item |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | View Code Duplication | public function add() |
|
0 ignored issues
–
show
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. ![]() |
|||
153 | { |
||
154 | $data = $this->request->post->get(); |
||
155 | if (array_key_exists('id', $data)) |
||
156 | { |
||
157 | $this->anime_collection_model->add($data); |
||
158 | $this->set_flash_message('Successfully added collection item', 'success'); |
||
159 | } |
||
160 | else |
||
161 | { |
||
162 | $this->set_flash_message('Failed to add collection item.', 'error'); |
||
163 | } |
||
164 | |||
165 | $this->session_redirect(); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Remove a collection item |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | View Code Duplication | public function delete() |
|
174 | { |
||
175 | $data = $this->request->post->get(); |
||
176 | if ( ! array_key_exists('id', $data)) |
||
177 | { |
||
178 | $this->redirect("collection/view", 303); |
||
179 | } |
||
180 | |||
181 | $this->anime_collection_model->delete($data); |
||
182 | |||
183 | $this->redirect("collection/view", 303); |
||
184 | } |
||
185 | } |
||
186 | // End of CollectionController.php |
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.