Completed
Push — develop ( e59ead...daf4b7 )
by Timothy
07:58
created

src/Aviat/AnimeClient/Controller/Manga.php (1 issue)

Upgrade to new PHP Analysis Engine

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
namespace Aviat\AnimeClient\Controller;
14
15
use Aviat\Ion\Di\ContainerInterface;
16
use Aviat\AnimeClient\Controller;
17
use Aviat\AnimeClient\Config;
18
use Aviat\AnimeClient\Model\Manga as MangaModel;
19
use Aviat\AnimeClient\Hummingbird\Enum\MangaReadingStatus;
20
use Aviat\AnimeClient\Hummingbird\Transformer\MangaListTransformer;
21
22
/**
23
 * Controller for manga list
24
 */
25
class Manga extends Controller {
26
27
	use \Aviat\Ion\StringWrapper;
28
29
	/**
30
	 * The manga 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 View Code Duplication
	public function __construct(ContainerInterface $container)
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.

Loading history...
47
	{
48
		parent::__construct($container);
49
50
		$this->model = $container->get('manga-model');
51
		$this->base_data = array_merge($this->base_data, [
52
			'menu_name' => 'manga_list',
53
			'config' => $this->config,
54
			'url_type' => 'manga',
55
			'other_type' => 'anime'
56
		]);
57
	}
58
59
	/**
60
	 * Get a section of the manga list
61
	 *
62
	 * @param string $status
63
	 * @param string $view
64
	 * @return void
65
	 */
66
	public function index($status = "all", $view = "")
67
	{
68
		$map = [
69
			'all' => 'All',
70
			'plan_to_read' => MangaModel::PLAN_TO_READ,
71
			'reading' => MangaModel::READING,
72
			'completed' => MangaModel::COMPLETED,
73
			'dropped' => MangaModel::DROPPED,
74
			'on_hold' => MangaModel::ON_HOLD
75
		];
76
77
		$title = $this->config->get('whose_list') . "'s Manga List &middot; {$map[$status]}";
78
79
		$view_map = [
80
			'' => 'cover',
81
			'list' => 'list'
82
		];
83
84
		$data = ($status !== 'all')
85
			? [$map[$status] => $this->model->get_list($map[$status])]
86
			: $this->model->get_all_lists();
87
88
		$this->outputHTML('manga/' . $view_map[$view], [
89
			'title' => $title,
90
			'sections' => $data,
91
		]);
92
	}
93
94
	/**
95
	 * Show the manga edit form
96
	 *
97
	 * @param string $id
98
	 * @param string $status
99
	 * @return void
100
	 */
101
	public function edit($id, $status = "All")
102
	{
103
		$this->set_session_redirect();
104
		$item = $this->model->get_library_item($id, $status);
105
		$title = $this->config->get('whose_list') . "'s Manga List &middot; Edit";
106
107
		$this->outputHTML('manga/edit', [
108
			'title' => $title,
109
			'status_list' => MangaReadingStatus::getConstList(),
110
			'item' => $item,
111
			'action' => $this->container->get('url-generator')
112
				->url('/manga/update_form'),
113
		]);
114
	}
115
116
	/**
117
	 * Update an anime item via a form submission
118
	 *
119
	 * @return void
120
	 */
121
	public function form_update()
122
	{
123
		$post_data = $this->request->post->get();
124
125
		// Do some minor data manipulation for
126
		// large form-based updates
127
		$transformer = new MangaListTransformer();
128
		$post_data = $transformer->untransform($post_data);
129
		$full_result = $this->model->update($post_data);
130
131
		$result = $full_result['body'];
132
133
		if (array_key_exists('manga', $result))
134
		{
135
			$m =& $result['manga'][0];
136
			$title = ( ! empty($m['english_title']))
137
				? "{$m['romaji_title']} ({$m['english_title']})"
138
				: "{$m['romaji_title']}";
139
140
			$this->set_flash_message("Successfully updated {$title}.", 'success');
141
		}
142
		else
143
		{
144
			$this->set_flash_message('Failed to update anime.', 'error');
145
		}
146
147
		$this->session_redirect();
148
	}
149
150
	/**
151
	 * Update an anime item
152
	 *
153
	 * @return boolean|null
154
	 */
155
	public function update()
156
	{
157
		$this->outputJSON($this->model->update($this->request->post->get()));
158
	}
159
}
160
// End of MangaController.php