Skip to content

Commit b926730

Browse files
committed
Refactoring Comment/{Create,Delete,Edit}
1 parent 6116aad commit b926730

File tree

9 files changed

+111
-56
lines changed

9 files changed

+111
-56
lines changed

src/Controllers/Comment/Create.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
use \BNETDocs\Libraries\Core\HttpCode;
77
use \BNETDocs\Libraries\Core\Router;
88
use \BNETDocs\Libraries\EventLog\Logger;
9+
use \BNETDocs\Models\Comment\Create as CreateModel;
910

1011
class Create extends \BNETDocs\Controllers\Base
1112
{
12-
public const EMPTY_CONTENT = 'EMPTY_CONTENT';
13-
public const INTERNAL_ERROR = 'INTERNAL_ERROR';
14-
1513
public function __construct()
1614
{
17-
$this->model = new \BNETDocs\Models\Comment\Create();
15+
$this->model = new CreateModel();
1816
}
1917

2018
public function invoke(?array $args): bool
@@ -25,6 +23,7 @@ public function invoke(?array $args): bool
2523
if (!$this->model->acl_allowed)
2624
{
2725
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
26+
$this->model->error = $this->model->active_user ? CreateModel::ERROR_ACL_NOT_SET : CreateModel::ERROR_NOT_LOGGED_IN;
2827
$this->model->response = ['error' => 'Unauthorized'];
2928
}
3029
else if (Router::requestMethod() !== Router::METHOD_POST)
@@ -53,7 +52,8 @@ protected function createComment(): int
5352

5453
if (empty($content))
5554
{
56-
$this->model->error = self::EMPTY_CONTENT;
55+
$this->model->error = CreateModel::ERROR_EMPTY_CONTENT;
56+
$this->model->response = ['error' => 'Empty Content'];
5757
return HttpCode::HTTP_BAD_REQUEST;
5858
}
5959

@@ -68,7 +68,7 @@ protected function createComment(): int
6868

6969
if (!$this->model->comment->commit())
7070
{
71-
$this->model->error = self::INTERNAL_ERROR;
71+
$this->model->error = CreateModel::ERROR_INTERNAL;
7272
return HttpCode::HTTP_INTERNAL_SERVER_ERROR;
7373
}
7474

src/Controllers/Comment/Delete.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
use \BNETDocs\Libraries\Core\HttpCode;
66
use \BNETDocs\Libraries\Core\Router;
77
use \BNETDocs\Libraries\EventLog\Logger;
8+
use \BNETDocs\Models\Comment\Delete as DeleteModel;
89

910
class Delete extends \BNETDocs\Controllers\Base
1011
{
11-
public const ACL_NOT_SET = 'ACL_NOT_SET';
12-
public const INTERNAL_ERROR = 'INTERNAL_ERROR';
13-
public const NOT_FOUND = 'NOT_FOUND';
14-
1512
public function __construct()
1613
{
17-
$this->model = new \BNETDocs\Models\Comment\Delete();
14+
$this->model = new DeleteModel();
1815
}
1916

2017
public function invoke(?array $args): bool
@@ -32,14 +29,14 @@ public function invoke(?array $args): bool
3229
if (!$this->model->acl_allowed)
3330
{
3431
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
35-
$this->model->error = self::ACL_NOT_SET;
32+
$this->model->error = $this->model->active_user ? DeleteModel::ERROR_ACL_NOT_SET : DeleteModel::ERROR_NOT_LOGGED_IN;
3633
return true;
3734
}
3835

3936
if (!$this->model->comment)
4037
{
4138
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
42-
$this->model->error = self::NOT_FOUND;
39+
$this->model->error = DeleteModel::ERROR_NOT_FOUND;
4340
return true;
4441
}
4542

@@ -49,15 +46,15 @@ public function invoke(?array $args): bool
4946

5047
if (Router::requestMethod() == Router::METHOD_POST) $this->tryDelete();
5148

52-
$this->model->_responseCode = HttpCode::HTTP_OK;
49+
$this->model->_responseCode = !$this->model->error ? HttpCode::HTTP_OK : HttpCode::HTTP_INTERNAL_SERVER_ERROR;
5350
return true;
5451
}
5552

5653
protected function tryDelete(): void
5754
{
5855
if (!$this->model->comment->deallocate())
5956
{
60-
$this->model->error = self::INTERNAL_ERROR;
57+
$this->model->error = DeleteModel::ERROR_INTERNAL;
6158
return;
6259
}
6360

src/Controllers/Comment/Edit.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
use \BNETDocs\Libraries\Core\HttpCode;
66
use \BNETDocs\Libraries\Core\Router;
77
use \BNETDocs\Libraries\EventLog\Logger;
8+
use \BNETDocs\Models\Comment\Edit as EditModel;
89

910
class Edit extends \BNETDocs\Controllers\Base
1011
{
11-
public const ACL_NOT_SET = 'ACL_NOT_SET';
12-
public const NOT_FOUND = 'NOT_FOUND';
13-
public const NOT_LOGGED_IN = 'NOT_LOGGED_IN';
14-
public const INTERNAL_ERROR = 'INTERNAL_ERROR';
15-
1612
public function __construct()
1713
{
18-
$this->model = new \BNETDocs\Models\Comment\Edit();
14+
$this->model = new EditModel();
1915
}
2016

2117
public function invoke(?array $args): bool
@@ -35,24 +31,35 @@ public function invoke(?array $args): bool
3531
if (!$this->model->acl_allowed)
3632
{
3733
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
38-
$this->model->error = $this->model->active_user ? self::ACL_NOT_SET : self::NOT_LOGGED_IN;
34+
$this->model->error = $this->model->active_user ? EditModel::ERROR_ACL_NOT_SET : EditModel::ERROR_NOT_LOGGED_IN;
3935
return true;
4036
}
4137

4238
if (!$this->model->comment)
4339
{
4440
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
45-
$this->model->error = self::NOT_FOUND;
41+
$this->model->error = EditModel::ERROR_NOT_FOUND;
4642
return true;
4743
}
4844

49-
$this->model->_responseCode = HttpCode::HTTP_OK;
5045
$this->model->parent_id = $this->model->comment->getParentId();
5146
$this->model->parent_type = $this->model->comment->getParentType();
5247
$this->model->return_url = $this->model->comment->getParentUrl();
53-
if (is_null($this->model->content)) $this->model->content = $this->model->comment->getContent(false);
48+
49+
if (is_null($this->model->content))
50+
{
51+
$this->model->content = $this->model->comment->getContent(false);
52+
}
53+
54+
if (empty($this->model->content))
55+
{
56+
$this->model->_responseCode = HttpCode::HTTP_BAD_REQUEST;
57+
$this->model->error = EditModel::ERROR_EMPTY_CONTENT;
58+
return true;
59+
}
5460

5561
if (Router::requestMethod() == Router::METHOD_POST) $this->tryEdit();
62+
$this->model->_responseCode = !$this->model->error ? HttpCode::HTTP_OK : HttpCode::HTTP_INTERNAL_SERVER_ERROR;
5663
return true;
5764
}
5865

@@ -63,7 +70,7 @@ protected function tryEdit(): void
6370

6471
if (!$this->model->comment->commit())
6572
{
66-
$this->model->error = self::INTERNAL_ERROR;
73+
$this->model->error = EditModel::ERROR_INTERNAL;
6774
return;
6875
}
6976

src/Models/Comment/Create.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@
22

33
namespace BNETDocs\Models\Comment;
44

5-
class Create extends \BNETDocs\Models\ActiveUser
5+
class Create extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public $acl_allowed;
8-
public $origin;
9-
public $response;
10-
public $user;
7+
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
8+
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
9+
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
10+
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';
11+
12+
public bool $acl_allowed = false;
13+
public ?\BNETDocs\Libraries\Comment $comment = null;
14+
public ?string $origin = null;
15+
public ?array $response = null;
16+
17+
public function jsonSerialize(): mixed
18+
{
19+
return \array_merge(parent::jsonSerialize(), [
20+
'acl_allowed' => $this->acl_allowed,
21+
'origin' => $this->origin,
22+
'response' => $this->response,
23+
]);
24+
}
1125
}

src/Models/Comment/Delete.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,29 @@
22

33
namespace BNETDocs\Models\Comment;
44

5-
class Delete extends \BNETDocs\Models\ActiveUser
5+
class Delete extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public bool $acl_allowed = false;
8-
public ?\BNETDocs\Libraries\Comment $comment = null;
9-
public ?string $content = null;
10-
public ?int $id = null;
11-
public ?int $parent_id = null;
12-
public ?int $parent_type = null;
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+
12+
public bool $acl_allowed = false;
13+
public ?\BNETDocs\Libraries\Comment $comment = null;
14+
public ?string $content = null;
15+
public ?int $id = null;
16+
public ?int $parent_id = null;
17+
public ?int $parent_type = null;
18+
19+
public function jsonSerialize(): mixed
20+
{
21+
return \array_merge(parent::jsonSerialize(), [
22+
'acl_allowed' => $this->acl_allowed,
23+
'comment' => $this->comment,
24+
'content' => $this->content,
25+
'id' => $this->id,
26+
'parent_id' => $this->parent_id,
27+
'parent_type' => $this->parent_type,
28+
]);
29+
}
1330
}

src/Models/Comment/Edit.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,32 @@
22

33
namespace BNETDocs\Models\Comment;
44

5-
class Edit extends \BNETDocs\Models\ActiveUser
5+
class Edit extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public $acl_allowed;
8-
public $comment;
9-
public $content;
10-
public $id;
11-
public $parent_id;
12-
public $parent_type;
13-
public $return_url;
14-
public $user;
7+
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
8+
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
9+
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
10+
public const ERROR_NOT_FOUND = 'NOT_FOUND';
11+
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';
12+
13+
public bool $acl_allowed = false;
14+
public ?\BNETDocs\Libraries\Comment $comment = null;
15+
public ?string $content = null;
16+
public ?int $id = null;
17+
public ?int $parent_id = null;
18+
public ?int $parent_type = null;
19+
public ?string $return_url = null;
20+
21+
public function jsonSerialize(): mixed
22+
{
23+
return \array_merge(parent::jsonSerialize(), [
24+
'acl_allowed' => $this->acl_allowed,
25+
'comment' => $this->comment,
26+
'content' => $this->content,
27+
'id' => $this->id,
28+
'parent_id' => $this->parent_id,
29+
'parent_type' => $this->parent_type,
30+
'return_url' => $this->return_url,
31+
]);
32+
}
1533
}

src/Templates/Comment/Delete.phtml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
22
namespace BNETDocs\Templates\Comment;
3+
use \BNETDocs\Models\Comment\Delete as DeleteModel;
34
use \CarlBennett\MVC\Libraries\Pair;
45
$title = 'Delete Comment';
56
$description = 'This page confirms if the user wishes to delete a comment.';
67
$this->opengraph->attach(new Pair('url', '/comment/delete'));
78
$this->opengraph->attach(new Pair('type', 'article'));
89
switch ($this->getContext()->error)
910
{
10-
case 'ACL_NOT_SET': $message = 'You do not have the privilege to delete comments.'; break;
11-
case 'NOT_FOUND': $message = 'Cannot find comment by that id.'; break;
12-
case 'NOT_LOGGED_IN': $message = 'You must be logged in to delete comments.'; break;
13-
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
11+
case DeleteModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to delete comments.'; break;
12+
case DeleteModel::ERROR_NOT_FOUND: $message = 'Cannot find comment by that id.'; break;
13+
case DeleteModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to delete comments.'; break;
14+
case DeleteModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
1415
default: $message = $this->getContext()->error;
1516
}
1617
$c = $this->getContext()->comment;

src/Templates/Comment/Edit.phtml

Lines changed: 6 additions & 4 deletions
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\Comment;
3+
use \BNETDocs\Models\Comment\Edit as EditModel;
34
use \CarlBennett\MVC\Libraries\Common;
45
use \CarlBennett\MVC\Libraries\Pair;
56
$title = 'Edit Comment';
@@ -8,10 +9,11 @@ $this->opengraph->attach(new Pair('url', '/comment/edit'));
89
$this->opengraph->attach(new Pair('type', 'article'));
910
switch ($this->getContext()->error)
1011
{
11-
case 'ACL_NOT_SET': $message = 'You do not have the privilege to edit this comment.'; break;
12-
case 'NOT_FOUND': $message = 'Cannot find comment by that id.'; break;
13-
case 'NOT_LOGGED_IN': $message = 'You must be logged in to edit comments.'; 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 EditModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to edit this comment.'; break;
13+
case EditModel::ERROR_EMPTY_CONTENT: $message = 'You must submit a comment with at least 1 character.'; break;
14+
case EditModel::ERROR_NOT_FOUND: $message = 'Cannot find comment by that id.'; break;
15+
case EditModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to edit comments.'; break;
16+
case EditModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
1517
default: $message = $this->getContext()->error;
1618
}
1719
$c = $this->getContext()->comment;

src/Templates/Comment/Section.inc.phtml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
22
namespace BNETDocs\Templates\Comment;
3-
use \BNETDocs\Libraries\Comment;
43
use \BNETDocs\Libraries\User\Authentication;
54
use \BNETDocs\Libraries\User\User;
65
use \CarlBennett\MVC\Libraries\Common;

0 commit comments

Comments
 (0)