Skip to content

Commit 3a688d0

Browse files
committed
Use model constants for error codes
1 parent 8cde8d7 commit 3a688d0

File tree

9 files changed

+71
-50
lines changed

9 files changed

+71
-50
lines changed

src/Controllers/News/Create.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function invoke(?array $args): bool
2828
if (!$this->model->acl_allowed)
2929
{
3030
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
31-
$this->model->error = CreateModel::ACL_NOT_SET;
31+
$this->model->error = $this->model->active_user ? CreateModel::ERROR_ACL_NOT_SET : CreateModel::ERROR_NOT_LOGGED_IN;
3232
return true;
3333
}
3434

@@ -68,11 +68,11 @@ protected function handlePost(): void
6868

6969
if (empty($this->model->title))
7070
{
71-
$this->model->error = CreateModel::EMPTY_TITLE;
71+
$this->model->error = CreateModel::ERROR_EMPTY_TITLE;
7272
}
7373
else if (empty($this->model->content))
7474
{
75-
$this->model->error = CreateModel::EMPTY_CONTENT;
75+
$this->model->error = CreateModel::ERROR_EMPTY_CONTENT;
7676
}
7777
else
7878
{
@@ -86,7 +86,7 @@ protected function handlePost(): void
8686
$this->model->news_post->setTitle($this->model->title);
8787
$this->model->news_post->setUserId($this->model->active_user->getId());
8888

89-
$this->model->error = $this->model->news_post->commit() ? false : CreateModel::INTERNAL_ERROR;
89+
$this->model->error = $this->model->news_post->commit() ? false : CreateModel::ERROR_INTERNAL;
9090
}
9191

9292
if ($this->model->error !== false) return;

src/Controllers/News/Delete.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use \BNETDocs\Libraries\Core\HttpCode;
66
use \BNETDocs\Libraries\Core\Router;
77
use \BNETDocs\Libraries\EventLog\Logger;
8+
use \BNETDocs\Models\News\Delete as DeleteModel;
89

910
class Delete extends \BNETDocs\Controllers\Base
1011
{
1112
public function __construct()
1213
{
13-
$this->model = new \BNETDocs\Models\News\Delete();
14+
$this->model = new DeleteModel();
1415
}
1516

1617
public function invoke(?array $args): bool
@@ -21,7 +22,7 @@ public function invoke(?array $args): bool
2122
if (!$this->model->acl_allowed)
2223
{
2324
$this->model->_responseCode = HttpCode::HTTP_UNAUTHORIZED;
24-
$this->model->error = 'ACL_NOT_SET';
25+
$this->model->error = $this->model->active_user ? DeleteModel::ERROR_ACL_NOT_SET : DeleteModel::ERROR_NOT_LOGGED_IN;
2526
return true;
2627
}
2728

@@ -34,7 +35,7 @@ public function invoke(?array $args): bool
3435
if (!$this->model->news_post)
3536
{
3637
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
37-
$this->model->error = 'NOT_FOUND';
38+
$this->model->error = DeleteModel::ERROR_NOT_FOUND;
3839
return true;
3940
}
4041

@@ -47,7 +48,7 @@ public function invoke(?array $args): bool
4748

4849
protected function tryDelete(): void
4950
{
50-
$this->model->error = $this->model->news_post->deallocate() ? false : 'INTERNAL_ERROR';
51+
$this->model->error = $this->model->news_post->deallocate() ? false : DeleteModel::ERROR_INTERNAL;
5152
if ($this->model->error !== false) return;
5253

5354
$event = Logger::initEvent(

src/Controllers/News/Edit.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
use \BNETDocs\Libraries\Core\HttpCode;
77
use \BNETDocs\Libraries\Core\Router;
88
use \BNETDocs\Libraries\EventLog\Logger;
9+
use \BNETDocs\Models\News\Edit as EditModel;
910

1011
class Edit extends \BNETDocs\Controllers\Base
1112
{
1213
public function __construct()
1314
{
14-
$this->model = new \BNETDocs\Models\News\Edit();
15+
$this->model = new EditModel();
1516
}
1617

1718
public function invoke(?array $args): bool
@@ -22,7 +23,7 @@ public function invoke(?array $args): bool
2223
if (!$this->model->acl_allowed)
2324
{
2425
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
25-
$this->model->error = 'ACL_NOT_SET';
26+
$this->model->error = $this->model->active_user ? EditModel::ERROR_ACL_NOT_SET : EditModel::ERROR_NOT_LOGGED_IN;
2627
return true;
2728
}
2829

@@ -35,7 +36,7 @@ public function invoke(?array $args): bool
3536
if (!$this->model->news_post)
3637
{
3738
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
38-
$this->model->error = 'NOT_FOUND';
39+
$this->model->error = EditModel::ERROR_NOT_FOUND;
3940
return true;
4041
}
4142

@@ -77,7 +78,8 @@ protected function handlePost(): void
7778
$this->model->content = $content;
7879
$this->model->rss_exempt = (bool) $rss_exempt;
7980

80-
$this->model->error = empty($title) ? 'EMPTY_TITLE' : (empty($content) ? 'EMPTY_CONTENT' : null);
81+
$this->model->error = empty($title) ? EditModel::ERROR_EMPTY_TITLE
82+
: (empty($content) ? EditModel::ERROR_EMPTY_CONTENT : null);
8183
if ($this->model->error) return;
8284

8385
$this->model->news_post->setCategoryId($this->model->category);
@@ -88,7 +90,7 @@ protected function handlePost(): void
8890
$this->model->news_post->setPublished($publish);
8991
$this->model->news_post->incrementEdited();
9092

91-
$this->model->error = $this->model->news_post->commit() ? false : 'INTERNAL_ERROR';
93+
$this->model->error = $this->model->news_post->commit() ? false : EditModel::ERROR_INTERNAL;
9294

9395
$event = Logger::initEvent(
9496
\BNETDocs\Libraries\EventLog\EventTypes::NEWS_EDITED,

src/Models/News/Create.php

+25-24
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,31 @@
44

55
class Create extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
66
{
7-
public const ACL_NOT_SET = 'ACL_NOT_SET';
8-
public const EMPTY_CONTENT = 'EMPTY_CONTENT';
9-
public const EMPTY_TITLE = 'EMPTY_TITLE';
10-
public const INTERNAL_ERROR = 'INTERNAL_ERROR';
7+
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
8+
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
9+
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
10+
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
11+
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';
1112

12-
public ?int $category_id = null;
13-
public string $content = '';
14-
public mixed $error = 'INTERNAL_ERROR';
15-
public bool $markdown = false;
16-
public ?\BNETDocs\Libraries\News\Post $news_post = null;
17-
public ?array $news_categories = null;
18-
public bool $rss_exempt = false;
19-
public string $title = '';
13+
public ?int $category_id = null;
14+
public string $content = '';
15+
public mixed $error = 'INTERNAL_ERROR';
16+
public bool $markdown = false;
17+
public ?\BNETDocs\Libraries\News\Post $news_post = null;
18+
public ?array $news_categories = null;
19+
public bool $rss_exempt = false;
20+
public string $title = '';
2021

21-
public function jsonSerialize(): mixed
22-
{
23-
return \array_merge(parent::jsonSerialize(), [
24-
'category' => $this->category_id,
25-
'content' => $this->content,
26-
'error' => $this->error,
27-
'markdown' => $this->markdown,
28-
'news_categories' => $this->news_categories,
29-
'rss_exempt' => $this->rss_exempt,
30-
'title' => $this->title,
31-
]);
32-
}
22+
public function jsonSerialize(): mixed
23+
{
24+
return \array_merge(parent::jsonSerialize(), [
25+
'category' => $this->category_id,
26+
'content' => $this->content,
27+
'error' => $this->error,
28+
'markdown' => $this->markdown,
29+
'news_categories' => $this->news_categories,
30+
'rss_exempt' => $this->rss_exempt,
31+
'title' => $this->title,
32+
]);
33+
}
3334
}

src/Models/News/Delete.php

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
class Delete extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
66
{
7+
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
8+
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
9+
public const ERROR_NOT_FOUND = 'NOT_FOUND';
10+
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';
11+
712
public ?int $id = null;
813
public ?\BNETDocs\Libraries\News\Post $news_post = null;
914
public string $title = '';

src/Models/News/Edit.php

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
class Edit extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
66
{
7+
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
8+
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
9+
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
10+
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
11+
public const ERROR_NOT_FOUND = 'NOT_FOUND';
12+
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';
13+
714
public ?int $category = null;
815
public ?array $comments = null;
916
public ?string $content = null;

src/Templates/News/Create.phtml

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
22
namespace BNETDocs\Templates\News;
3+
use \BNETDocs\Models\News\Create as CreateModel;
34
$title = 'Create News Post';
45
$description = 'This page enables a user to create news posts on the site.';
56
$url = '/news/create';
67
$error = $this->getContext()->error;
78
switch ($error)
89
{
9-
case 'ACL_NOT_SET': $message = 'You do not have the privilege to create news posts.'; break;
10-
case 'EMPTY_TITLE': $message = 'The title of the news post is required.'; break;
11-
case 'EMPTY_CONTENT': $message = 'The content of the news post is required.'; break;
12-
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
10+
case CreateModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to create news posts.'; break;
11+
case CreateModel::ERROR_EMPTY_TITLE: $message = 'The title of the news post is required.'; break;
12+
case CreateModel::ERROR_EMPTY_CONTENT: $message = 'The content of the news post is required.'; break;
13+
case CreateModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to create news posts.'; break;
14+
case CreateModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Try again later.'; break;
1315
default: $message = $error;
1416
}
1517
$form_content = filter_var($this->getContext()->content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

src/Templates/News/Delete.phtml

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
22
namespace BNETDocs\Templates\News;
3+
use \BNETDocs\Models\News\Delete as DeleteModel;
34
$title = 'Delete News Post';
45
$description = 'This form allows an individual to delete a news post.';
56
$url = '/news/delete';
@@ -8,10 +9,10 @@ $id = $this->getContext()->id;
89
$news_post_title = $this->getContext()->title;
910
switch ($error)
1011
{
11-
case 'ACL_NOT_SET': $message = 'You do not have the privilege to delete news posts.'; break;
12-
case 'NOT_FOUND': $message = 'Cannot find news post by that id.'; break;
13-
case 'NOT_LOGGED_IN': $message = 'You must be logged in to delete news posts.'; break;
14-
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
12+
case DeleteModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to delete news posts.'; break;
13+
case DeleteModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Try again later.'; break;
14+
case DeleteModel::ERROR_NOT_FOUND: $message = 'Cannot find news post by that id.'; break;
15+
case DeleteModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to delete news posts.'; break;
1516
default: $message = $error;
1617
}
1718
require('./Includes/header.inc.phtml'); ?>

src/Templates/News/Edit.phtml

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
22
namespace BNETDocs\Templates\News;
33
use \BNETDocs\Libraries\Comment;
4+
use \BNETDocs\Models\News\Edit as EditModel;
45
$title = 'Edit News Post';
56
$description = 'This page enables a user to edit a news post on the site.';
67
$url = '/news/edit';
@@ -9,11 +10,12 @@ $comments = $this->getContext()->comments;
910
$error = $this->getContext()->error;
1011
switch ($error)
1112
{
12-
case 'ACL_NOT_SET': $message = 'You do not have the privilege to edit news posts.'; break;
13-
case 'NOT_FOUND': $message = 'Cannot find news post by that id.'; break;
14-
case 'EMPTY_TITLE': $message = 'The title of the news post is required.'; break;
15-
case 'EMPTY_CONTENT': $message = 'The content of the news post is required.'; break;
16-
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
13+
case EditModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to edit news posts.'; break;
14+
case EditModel::ERROR_EMPTY_CONTENT: $message = 'The content of the news post is required.'; break;
15+
case EditModel::ERROR_EMPTY_TITLE: $message = 'The title of the news post is required.'; break;
16+
case EditModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Try again later.'; break;
17+
case EditModel::ERROR_NOT_FOUND: $message = 'Cannot find news post by that id.'; break;
18+
case EditModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to edit news posts.'; break;
1719
default: $message = $error;
1820
}
1921
$form_category = $this->getContext()->category;

0 commit comments

Comments
 (0)