Skip to content

Commit

Permalink
Use model constants for error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Dec 14, 2024
1 parent 8cde8d7 commit 3a688d0
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 50 deletions.
8 changes: 4 additions & 4 deletions src/Controllers/News/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function invoke(?array $args): bool
if (!$this->model->acl_allowed)
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = CreateModel::ACL_NOT_SET;
$this->model->error = $this->model->active_user ? CreateModel::ERROR_ACL_NOT_SET : CreateModel::ERROR_NOT_LOGGED_IN;
return true;
}

Expand Down Expand Up @@ -68,11 +68,11 @@ protected function handlePost(): void

if (empty($this->model->title))
{
$this->model->error = CreateModel::EMPTY_TITLE;
$this->model->error = CreateModel::ERROR_EMPTY_TITLE;
}
else if (empty($this->model->content))
{
$this->model->error = CreateModel::EMPTY_CONTENT;
$this->model->error = CreateModel::ERROR_EMPTY_CONTENT;
}
else
{
Expand All @@ -86,7 +86,7 @@ protected function handlePost(): void
$this->model->news_post->setTitle($this->model->title);
$this->model->news_post->setUserId($this->model->active_user->getId());

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

if ($this->model->error !== false) return;
Expand Down
9 changes: 5 additions & 4 deletions src/Controllers/News/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\Core\Router;
use \BNETDocs\Libraries\EventLog\Logger;
use \BNETDocs\Models\News\Delete as DeleteModel;

class Delete extends \BNETDocs\Controllers\Base
{
public function __construct()
{
$this->model = new \BNETDocs\Models\News\Delete();
$this->model = new DeleteModel();
}

public function invoke(?array $args): bool
Expand All @@ -21,7 +22,7 @@ public function invoke(?array $args): bool
if (!$this->model->acl_allowed)
{
$this->model->_responseCode = HttpCode::HTTP_UNAUTHORIZED;
$this->model->error = 'ACL_NOT_SET';
$this->model->error = $this->model->active_user ? DeleteModel::ERROR_ACL_NOT_SET : DeleteModel::ERROR_NOT_LOGGED_IN;
return true;
}

Expand All @@ -34,7 +35,7 @@ public function invoke(?array $args): bool
if (!$this->model->news_post)
{
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
$this->model->error = 'NOT_FOUND';
$this->model->error = DeleteModel::ERROR_NOT_FOUND;
return true;
}

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

protected function tryDelete(): void
{
$this->model->error = $this->model->news_post->deallocate() ? false : 'INTERNAL_ERROR';
$this->model->error = $this->model->news_post->deallocate() ? false : DeleteModel::ERROR_INTERNAL;
if ($this->model->error !== false) return;

$event = Logger::initEvent(
Expand Down
12 changes: 7 additions & 5 deletions src/Controllers/News/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\Core\Router;
use \BNETDocs\Libraries\EventLog\Logger;
use \BNETDocs\Models\News\Edit as EditModel;

class Edit extends \BNETDocs\Controllers\Base
{
public function __construct()
{
$this->model = new \BNETDocs\Models\News\Edit();
$this->model = new EditModel();
}

public function invoke(?array $args): bool
Expand All @@ -22,7 +23,7 @@ public function invoke(?array $args): bool
if (!$this->model->acl_allowed)
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = 'ACL_NOT_SET';
$this->model->error = $this->model->active_user ? EditModel::ERROR_ACL_NOT_SET : EditModel::ERROR_NOT_LOGGED_IN;
return true;
}

Expand All @@ -35,7 +36,7 @@ public function invoke(?array $args): bool
if (!$this->model->news_post)
{
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
$this->model->error = 'NOT_FOUND';
$this->model->error = EditModel::ERROR_NOT_FOUND;
return true;
}

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

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

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

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

$event = Logger::initEvent(
\BNETDocs\Libraries\EventLog\EventTypes::NEWS_EDITED,
Expand Down
49 changes: 25 additions & 24 deletions src/Models/News/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@

class Create extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ACL_NOT_SET = 'ACL_NOT_SET';
public const EMPTY_CONTENT = 'EMPTY_CONTENT';
public const EMPTY_TITLE = 'EMPTY_TITLE';
public const INTERNAL_ERROR = 'INTERNAL_ERROR';
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?int $category_id = null;
public string $content = '';
public mixed $error = 'INTERNAL_ERROR';
public bool $markdown = false;
public ?\BNETDocs\Libraries\News\Post $news_post = null;
public ?array $news_categories = null;
public bool $rss_exempt = false;
public string $title = '';
public ?int $category_id = null;
public string $content = '';
public mixed $error = 'INTERNAL_ERROR';
public bool $markdown = false;
public ?\BNETDocs\Libraries\News\Post $news_post = null;
public ?array $news_categories = null;
public bool $rss_exempt = false;
public string $title = '';

public function jsonSerialize(): mixed
{
return \array_merge(parent::jsonSerialize(), [
'category' => $this->category_id,
'content' => $this->content,
'error' => $this->error,
'markdown' => $this->markdown,
'news_categories' => $this->news_categories,
'rss_exempt' => $this->rss_exempt,
'title' => $this->title,
]);
}
public function jsonSerialize(): mixed
{
return \array_merge(parent::jsonSerialize(), [
'category' => $this->category_id,
'content' => $this->content,
'error' => $this->error,
'markdown' => $this->markdown,
'news_categories' => $this->news_categories,
'rss_exempt' => $this->rss_exempt,
'title' => $this->title,
]);
}
}
5 changes: 5 additions & 0 deletions src/Models/News/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class Delete extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_FOUND = 'NOT_FOUND';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?int $id = null;
public ?\BNETDocs\Libraries\News\Post $news_post = null;
public string $title = '';
Expand Down
7 changes: 7 additions & 0 deletions src/Models/News/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

class Edit extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable
{
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
public const ERROR_NOT_FOUND = 'NOT_FOUND';
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';

public ?int $category = null;
public ?array $comments = null;
public ?string $content = null;
Expand Down
10 changes: 6 additions & 4 deletions src/Templates/News/Create.phtml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
namespace BNETDocs\Templates\News;
use \BNETDocs\Models\News\Create as CreateModel;
$title = 'Create News Post';
$description = 'This page enables a user to create news posts on the site.';
$url = '/news/create';
$error = $this->getContext()->error;
switch ($error)
{
case 'ACL_NOT_SET': $message = 'You do not have the privilege to create news posts.'; break;
case 'EMPTY_TITLE': $message = 'The title of the news post is required.'; break;
case 'EMPTY_CONTENT': $message = 'The content of the news post is required.'; break;
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
case CreateModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to create news posts.'; break;
case CreateModel::ERROR_EMPTY_TITLE: $message = 'The title of the news post is required.'; break;
case CreateModel::ERROR_EMPTY_CONTENT: $message = 'The content of the news post is required.'; break;
case CreateModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to create news posts.'; break;
case CreateModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Try again later.'; break;
default: $message = $error;
}
$form_content = filter_var($this->getContext()->content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Expand Down
9 changes: 5 additions & 4 deletions src/Templates/News/Delete.phtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
namespace BNETDocs\Templates\News;
use \BNETDocs\Models\News\Delete as DeleteModel;
$title = 'Delete News Post';
$description = 'This form allows an individual to delete a news post.';
$url = '/news/delete';
Expand All @@ -8,10 +9,10 @@ $id = $this->getContext()->id;
$news_post_title = $this->getContext()->title;
switch ($error)
{
case 'ACL_NOT_SET': $message = 'You do not have the privilege to delete news posts.'; break;
case 'NOT_FOUND': $message = 'Cannot find news post by that id.'; break;
case 'NOT_LOGGED_IN': $message = 'You must be logged in to delete news posts.'; break;
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
case DeleteModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to delete news posts.'; break;
case DeleteModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Try again later.'; break;
case DeleteModel::ERROR_NOT_FOUND: $message = 'Cannot find news post by that id.'; break;
case DeleteModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to delete news posts.'; break;
default: $message = $error;
}
require('./Includes/header.inc.phtml'); ?>
Expand Down
12 changes: 7 additions & 5 deletions src/Templates/News/Edit.phtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
namespace BNETDocs\Templates\News;
use \BNETDocs\Libraries\Comment;
use \BNETDocs\Models\News\Edit as EditModel;
$title = 'Edit News Post';
$description = 'This page enables a user to edit a news post on the site.';
$url = '/news/edit';
Expand All @@ -9,11 +10,12 @@ $comments = $this->getContext()->comments;
$error = $this->getContext()->error;
switch ($error)
{
case 'ACL_NOT_SET': $message = 'You do not have the privilege to edit news posts.'; break;
case 'NOT_FOUND': $message = 'Cannot find news post by that id.'; break;
case 'EMPTY_TITLE': $message = 'The title of the news post is required.'; break;
case 'EMPTY_CONTENT': $message = 'The content of the news post is required.'; break;
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
case EditModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to edit news posts.'; break;
case EditModel::ERROR_EMPTY_CONTENT: $message = 'The content of the news post is required.'; break;
case EditModel::ERROR_EMPTY_TITLE: $message = 'The title of the news post is required.'; break;
case EditModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Try again later.'; break;
case EditModel::ERROR_NOT_FOUND: $message = 'Cannot find news post by that id.'; break;
case EditModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to edit news posts.'; break;
default: $message = $error;
}
$form_category = $this->getContext()->category;
Expand Down

0 comments on commit 3a688d0

Please sign in to comment.