Browse Source

Simplify/clean up some base classes

pull/21/head
Timothy Warren 7 months ago
parent
commit
0503cad15f

+ 1
- 1
build/phpunit.xml View File

@@ -20,7 +20,7 @@
20 20
 		<log type="coverage-clover" target="logs/clover.xml"/>
21 21
 		<log type="coverage-crap4j" target="logs/crap4j.xml"/>
22 22
 		<log type="coverage-xml" target="logs/coverage" />
23
-		<log type="junit" target="logs/junit.xml" logIncompleteSkipped="false"/>
23
+		<log type="junit" target="logs/junit.xml" />
24 24
 	</logging>
25 25
 	<php>
26 26
 		<server name="HTTP_USER_AGENT" value="Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0" />

+ 1
- 1
src/API/Kitsu/Transformer/AnimeListTransformer.php View File

@@ -72,7 +72,7 @@ final class AnimeListTransformer extends AbstractTransformer {
72 72
 			}
73 73
 		}
74 74
 
75
-		$streamingLinks = (array_key_exists('streamingLinks', $anime['relationships']))
75
+		$streamingLinks = array_key_exists('streamingLinks', $anime['relationships'])
76 76
 			? Kitsu::parseListItemStreamingLinks($included, $animeId)
77 77
 			: [];
78 78
 

+ 26
- 52
src/Controller.php View File

@@ -79,11 +79,7 @@ class Controller {
79 79
 	 * Common data to be sent to views
80 80
 	 * @var array
81 81
 	 */
82
-	protected $baseData = [
83
-		'url_type' => 'anime',
84
-		'other_type' => 'manga',
85
-		'menu_name' => ''
86
-	];
82
+	protected $baseData = [];
87 83
 
88 84
 	/**
89 85
 	 * Controller constructor.
@@ -95,46 +91,29 @@ class Controller {
95 91
 	public function __construct(ContainerInterface $container)
96 92
 	{
97 93
 		$this->setContainer($container);
94
+
98 95
 		$auraUrlGenerator = $container->get('aura-router')->getGenerator();
96
+		$session = $container->get('session');
99 97
 		$urlGenerator = $container->get('url-generator');
98
+
100 99
 		$this->cache =  $container->get('cache');
101 100
 		$this->config = $container->get('config');
102 101
 		$this->request = $container->get('request');
103 102
 		$this->response = $container->get('response');
104
-
105
-		$this->baseData = array_merge($this->baseData, [
106
-			'url' => $auraUrlGenerator,
107
-			'urlGenerator' => $urlGenerator,
108
-			'auth' => $container->get('auth'),
109
-			'config' => $this->config
110
-		]);
111
-
103
+		$this->session = $session->getSegment(SESSION_SEGMENT);
112 104
 		$this->url = $auraUrlGenerator;
113 105
 		$this->urlGenerator = $urlGenerator;
114 106
 
115
-		$session = $container->get('session');
116
-		$this->session = $session->getSegment(SESSION_SEGMENT);
117
-
118
-		// Set a 'previous' flash value for better redirects
119
-		$serverParams = $this->request->getServerParams();
120
-		if (array_key_exists('HTTP_REFERER', $serverParams) && false === stripos($serverParams['HTTP_REFERER'], 'login'))
121
-		{
122
-			$this->session->setFlash('previous', $serverParams['HTTP_REFERER']);
123
-		}
124
-
125
-		// Set a message box if available
126
-		$this->baseData['message'] = $this->session->getFlash('message');
127
-	}
128
-
129
-	/**
130
-	 * Redirect to the previous page
131
-	 *
132
-	 * @return void
133
-	 */
134
-	public function redirectToPrevious(): void
135
-	{
136
-		$previous = $this->session->getFlash('previous');
137
-		$this->redirect($previous, 303);
107
+		$this->baseData = [
108
+			'auth' => $container->get('auth'),
109
+			'config' => $this->config,
110
+			'menu_name' => '',
111
+			'message' => $this->session->getFlash('message'), // Get message box data if it exists
112
+			'other_type' => 'manga',
113
+			'url' => $auraUrlGenerator,
114
+			'url_type' => 'anime',
115
+			'urlGenerator' => $urlGenerator,
116
+		];
138 117
 	}
139 118
 
140 119
 	/**
@@ -159,7 +138,7 @@ class Controller {
159 138
 
160 139
 		// Don't attempt to set the redirect url if
161 140
 		// the page is one of the form type pages,
162
-		// and the previous page is also a form type page_segments
141
+		// and the previous page is also a form type
163 142
 		if ($doubleFormPage || $isLoginPage)
164 143
 		{
165 144
 			return;
@@ -178,6 +157,8 @@ class Controller {
178 157
 	/**
179 158
 	 * Redirect to the url previously set in the  session
180 159
 	 *
160
+	 * If one is not set, redirect to default url
161
+	 *
181 162
 	 * @throws InvalidArgumentException
182 163
 	 * @throws \Aviat\Ion\Di\Exception\ContainerException
183 164
 	 * @throws \Aviat\Ion\Di\Exception\NotFoundException
@@ -185,16 +166,10 @@ class Controller {
185 166
 	 */
186 167
 	public function sessionRedirect(): void
187 168
 	{
188
-		$target = $this->session->get('redirect_url');
189
-		if (empty($target))
190
-		{
191
-			$this->notFound();
192
-		}
193
-		else
194
-		{
195
-			$this->redirect($target, 303);
196
-			$this->session->set('redirect_url', NULL);
197
-		}
169
+		$target = $this->session->get('redirect_url') ?? '/';
170
+
171
+		$this->redirect($target, 303);
172
+		$this->session->set('redirect_url', NULL);
198 173
 	}
199 174
 
200 175
 	/**
@@ -218,7 +193,7 @@ class Controller {
218 193
 		}
219 194
 
220 195
 		$route = $router->getRoute();
221
-		$data['route_path'] = $route ? $router->getRoute()->path : '';
196
+		$data['route_path'] = $route !== FALSE ? $route->path : '';
222 197
 
223 198
 
224 199
 		$templatePath = _dir($this->config->get('view_path'), "{$template}.php");
@@ -275,7 +250,7 @@ class Controller {
275 250
 	public function notFound(
276 251
 		string $title = 'Sorry, page not found',
277 252
 		string $message = 'Page Not Found'
278
-		): void
253
+	): void
279 254
 	{
280 255
 		$this->outputHTML('404', [
281 256
 			'title' => $title,
@@ -408,7 +383,6 @@ class Controller {
408 383
 		(new JsonView($this->container))
409 384
 			->setStatusCode($code)
410 385
 			->setOutput($data);
411
-			// ->send();
412 386
 		exit();
413 387
 	}
414 388
 
@@ -421,8 +395,8 @@ class Controller {
421 395
 	 */
422 396
 	protected function redirect(string $url, int $code): void
423 397
 	{
424
-		$http = new HttpView($this->container);
425
-		$http->redirect($url, $code);
398
+		(new HttpView($this->container))->redirect($url, $code);
399
+		exit();
426 400
 	}
427 401
 }
428 402
 // End of BaseController.php

+ 2
- 4
src/Controller/Anime.php View File

@@ -28,6 +28,7 @@ use Aviat\Ion\Json;
28 28
  * Controller for Anime-related pages
29 29
  */
30 30
 final class Anime extends BaseController {
31
+
31 32
 	/**
32 33
 	 * The anime list model
33 34
 	 * @var \Aviat\AnimeClient\Model\Anime $model
@@ -49,12 +50,9 @@ final class Anime extends BaseController {
49 50
 
50 51
 		$this->baseData = array_merge($this->baseData, [
51 52
 			'menu_name' => 'anime_list',
52
-			'url_type' => 'anime',
53 53
 			'other_type' => 'manga',
54
-			'config' => $this->config,
54
+			'url_type' => 'anime',
55 55
 		]);
56
-
57
-		$this->cache = $container->get('cache');
58 56
 	}
59 57
 
60 58
 	/**

+ 1
- 2
src/Controller/AnimeCollection.php View File

@@ -56,9 +56,8 @@ final class AnimeCollection extends BaseController {
56 56
 		$this->baseData = array_merge($this->baseData, [
57 57
 			'collection_type' => 'anime',
58 58
 			'menu_name' => 'collection',
59
-			'url_type' => 'anime',
60 59
 			'other_type' => 'manga',
61
-			'config' => $this->config,
60
+			'url_type' => 'anime',
62 61
 		]);
63 62
 	}
64 63
 

+ 0
- 2
src/Controller/Images.php View File

@@ -131,8 +131,6 @@ final class Images extends BaseController {
131 131
 
132 132
 		$data = wait($response->getBody());
133 133
 
134
-
135
-
136 134
 		[$origWidth] = getimagesizefromstring($data);
137 135
 		$gdImg = imagecreatefromstring($data);
138 136
 		$resizedImg = imagescale($gdImg, $width ?? $origWidth);

+ 1
- 4
src/Controller/Manga.php View File

@@ -29,8 +29,6 @@ use Aviat\Ion\{Json, StringWrapper};
29 29
  */
30 30
 final class Manga extends Controller {
31 31
 
32
-	use StringWrapper;
33
-
34 32
 	/**
35 33
 	 * The manga model
36 34
 	 * @var MangaModel $model
@@ -51,9 +49,8 @@ final class Manga extends Controller {
51 49
 		$this->model = $container->get('manga-model');
52 50
 		$this->baseData = array_merge($this->baseData, [
53 51
 			'menu_name' => 'manga_list',
54
-			'config' => $this->config,
52
+			'other_type' => 'anime',
55 53
 			'url_type' => 'manga',
56
-			'other_type' => 'anime'
57 54
 		]);
58 55
 	}
59 56
 

+ 1
- 2
src/Controller/MangaCollection.php View File

@@ -57,9 +57,8 @@ final class MangaCollection extends BaseController {
57 57
 		$this->baseData = array_merge($this->baseData, [
58 58
 			'collection_type' => 'manga',
59 59
 			'menu_name' => 'manga-collection',
60
-			'url_type' => 'manga',
61 60
 			'other_type' => 'anime',
62
-			'config' => $this->config,
61
+			'url_type' => 'manga',
63 62
 		]);
64 63
 	}
65 64
 

+ 4
- 7
src/Controller/Settings.php View File

@@ -23,6 +23,7 @@ use Aviat\Ion\Di\ContainerInterface;
23 23
  * Controller for user settings
24 24
  */
25 25
 final class Settings extends BaseController {
26
+
26 27
 	/**
27 28
 	 * @var \Aviat\AnimeClient\API\Anilist\Model
28 29
 	 */
@@ -144,13 +145,9 @@ final class Settings extends BaseController {
144 145
 
145 146
 		$saved = $this->settingsModel->saveSettingsFile($newSettings);
146 147
 
147
-		if ($saved)
148
-		{
149
-			$this->setFlashMessage('Linked Anilist Account', 'success');
150
-		} else
151
-		{
152
-			$this->setFlashMessage('Error Linking Anilist Account', 'error');
153
-		}
148
+		$saved
149
+			? $this->setFlashMessage('Linked Anilist Account', 'success')
150
+			: $this->setFlashMessage('Error Linking Anilist Account', 'error');
154 151
 
155 152
 		$this->redirect($this->url->generate('settings'), 303);
156 153
 	}

+ 2
- 3
src/Helper/Form.php View File

@@ -20,14 +20,14 @@ use Aviat\AnimeClient\FormGenerator;
20 20
 use Aviat\Ion\Di\ContainerAware;
21 21
 
22 22
 /**
23
- * MenuGenerator helper wrapper
23
+ * FormGenerator helper wrapper
24 24
  */
25 25
 final class Form {
26 26
 
27 27
 	use ContainerAware;
28 28
 
29 29
 	/**
30
-	 * Create the html for the selected menu
30
+	 * Create the html for the specified form
31 31
 	 *
32 32
 	 * @param string $name
33 33
 	 * @param array $form
@@ -38,4 +38,3 @@ final class Form {
38 38
 		return (new FormGenerator($this->container))->generate($name, $form);
39 39
 	}
40 40
 }
41
-// End of Menu.php

Loading…
Cancel
Save