1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
Loader::load('utility', [ |
4
|
|
|
'Request', |
5
|
|
|
'Validate', |
6
|
|
|
]); |
7
|
|
|
|
8
|
|
|
use Jacobemerick\Web\Domain\Comment\Comment\ServiceCommentRepository; |
9
|
|
|
|
10
|
|
|
class CommentSubmitModule |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private $site; |
14
|
|
|
private $path; |
15
|
|
|
private $fullPath; |
16
|
|
|
|
17
|
|
|
public function __construct($site, $path, $fullPath, $pageTitle) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->site = $site; |
20
|
|
|
$this->path = $path; |
21
|
|
|
$this->fullPath = $fullPath; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function activate() |
25
|
|
|
{ |
26
|
|
|
// todo why is this responsible for checking on valid calls |
27
|
|
|
if (!Request::hasPost()) { |
28
|
|
|
return false; |
29
|
|
|
} |
30
|
|
|
if (!Request::getPost('submit') == 'Submit Comment') { |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
if (!Request::getPost('catch') !== '') { |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$errors = $this->checkValidation(); |
38
|
|
|
if (count($errors) > 0) { |
39
|
|
|
return $errors; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$commentId = $this->save(Request::getPost()); |
43
|
|
|
// todo broken notifications |
44
|
|
|
$this->redirectToComment($commentId); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function checkValidation() |
48
|
|
|
{ |
49
|
|
|
$errors = array(); |
50
|
|
|
if (!Validate::checkRequest('post', 'name', 'name')) { |
51
|
|
|
$errors['name'] = 'You must include a valid name'; |
52
|
|
|
} |
53
|
|
|
if (!Validate::checkRequest('post', 'email', 'email')) { |
54
|
|
|
$errors['email'] = 'You must include a valid email'; |
55
|
|
|
} |
56
|
|
|
if (Request::getPost('website') && !Validate::checkRequest('post', 'website', 'url')) { |
57
|
|
|
$errors['website'] = 'Please enter a valid website'; |
58
|
|
|
} |
59
|
|
|
if (!Validate::checkRequest('post', 'comment', 'string')) { |
60
|
|
|
$errors['comment'] = 'You must enter a comment'; |
61
|
|
|
} |
62
|
|
|
if (Request::getPost('notify') && Request::getPost('notify') != 'check') { |
63
|
|
|
$errors['notify'] = 'You entered an invalid notify request'; |
64
|
|
|
} |
65
|
|
|
if (Request::getPost('reply') && !Validate::checkRequest('post', 'reply', 'integer')) { |
66
|
|
|
$errors['reply'] = 'You entered an invalid reply request'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $errors; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function save(array $data) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$path = $_SERVER['REQUEST_URI']; |
75
|
|
|
$path = explode('/', $path); |
76
|
|
|
$path = array_filter($path); |
77
|
|
|
$path = array_slice($path, 0, 2); |
78
|
|
|
$path = implode('/', $path); |
79
|
|
|
|
80
|
|
|
$body = [ |
81
|
|
|
'commenter' => [ |
82
|
|
|
'name' => $data['name'], |
83
|
|
|
'email' => $data['email'], |
84
|
|
|
'website' => $data['website'], |
85
|
|
|
], |
86
|
|
|
'body' => $data['comment'], |
87
|
|
|
'should_notify' => (isset($data['notify']) && $data['notify'] == 'check'), |
88
|
|
|
'domain' => (URLDecode::getSite() == 'blog' ? 'blog.jacobemerick.com' : 'waterfallsofthekeweenaw.com'), |
89
|
|
|
'path' => $path, |
90
|
|
|
'url' => "{$this->fullPath}#comment-{{id}}", |
91
|
|
|
'thread' => 'comments', |
92
|
|
|
'reply_to' => ($data['type'] == 'new' ? 0 : $data['type']), |
93
|
|
|
'ip_address' => $_SERVER['REMOTE_ADDR'], |
94
|
|
|
'user_agent' => $_SERVER['HTTP_USER_AGENT'], |
95
|
|
|
'referrer' => $_SERVER['HTTP_REFERER'], |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
global $container; |
|
|
|
|
99
|
|
|
$repository = new ServiceCommentRepository($container['comment_service_api']); |
100
|
|
|
try { |
101
|
|
|
$response = $repository->createComment($body); |
102
|
|
|
} catch (Exception $e) { |
103
|
|
|
$container['logger']->warning("CommentService | Create | {$e->getMessage()}"); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $response->getId(); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function redirectToComment($commentId) |
110
|
|
|
{ |
111
|
|
|
$url = ''; |
112
|
|
|
$url .= $this->fullPath; |
113
|
|
|
$url .= "#comment-{$commentId}"; |
114
|
|
|
|
115
|
|
|
Loader::loadNew('controller', 'Error303Controller', array($url))->activate(); |
116
|
|
|
exit; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.