1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Blog\Post; |
4
|
|
|
|
5
|
|
|
use App\Blog\Entity\Post; |
6
|
|
|
use App\Entity\User; |
7
|
|
|
use App\ViewRenderer; |
8
|
|
|
use Cycle\ORM\ORMInterface; |
9
|
|
|
use Cycle\ORM\Transaction; |
10
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
13
|
|
|
use Yiisoft\Access\AccessCheckerInterface; |
14
|
|
|
use Yiisoft\Http\Method; |
15
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
16
|
|
|
use Yiisoft\Yii\Web\User\User as UserComponent; |
17
|
|
|
|
18
|
|
|
final class PostController |
19
|
|
|
{ |
20
|
|
|
private ViewRenderer $viewRenderer; |
21
|
|
|
private ResponseFactoryInterface $responseFactory; |
22
|
|
|
|
23
|
|
|
public function __construct(ViewRenderer $viewRenderer, ResponseFactoryInterface $responseFactory) |
24
|
|
|
{ |
25
|
|
|
$this->viewRenderer = $viewRenderer->withControllerName('blog/post'); |
26
|
|
|
$this->responseFactory = $responseFactory; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function index(Request $request, PostRepository $postRepository): Response |
30
|
|
|
{ |
31
|
|
|
$slug = $request->getAttribute('slug', null); |
32
|
|
|
$item = $postRepository->fullPostPage($slug); |
33
|
|
|
if ($item === null) { |
34
|
|
|
return $this->responseFactory->createResponse(404); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->viewRenderer->render('index', ['item' => $item]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function add( |
41
|
|
|
Request $request, |
42
|
|
|
ORMInterface $orm, |
43
|
|
|
UrlGeneratorInterface $urlGenerator, |
44
|
|
|
UserComponent $userComponent |
45
|
|
|
): Response { |
46
|
|
|
$body = $request->getParsedBody(); |
47
|
|
|
$parameters = [ |
48
|
|
|
'body' => $body, |
49
|
|
|
'action' => ['blog/add'] |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
if ($request->getMethod() === Method::POST) { |
53
|
|
|
$error = ''; |
|
|
|
|
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
foreach (['header', 'content'] as $name) { |
57
|
|
|
if (empty($body[$name])) { |
58
|
|
|
throw new \InvalidArgumentException(ucfirst($name) . ' is required'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$post = new Post($body['header'], $body['content']); |
63
|
|
|
|
64
|
|
|
$userRepo = $orm->getRepository(User::class); |
65
|
|
|
$user = $userRepo->findByPK($userComponent->getId()); |
66
|
|
|
|
67
|
|
|
$post->setUser($user); |
|
|
|
|
68
|
|
|
$post->setPublic(true); |
69
|
|
|
|
70
|
|
|
$transaction = new Transaction($orm); |
71
|
|
|
$transaction->persist($post); |
72
|
|
|
|
73
|
|
|
$transaction->run(); |
74
|
|
|
|
75
|
|
|
return $this->responseFactory |
76
|
|
|
->createResponse(302) |
77
|
|
|
->withHeader( |
78
|
|
|
'Location', |
79
|
|
|
$urlGenerator->generate('blog/index') |
80
|
|
|
); |
81
|
|
|
} catch (\Throwable $e) { |
82
|
|
|
$error = $e->getMessage(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$parameters['error'] = $error; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$parameters['title'] = 'Add post'; |
89
|
|
|
return $this->viewRenderer->withCsrf()->render('__form', $parameters); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function edit( |
93
|
|
|
Request $request, |
94
|
|
|
ORMInterface $orm, |
95
|
|
|
UrlGeneratorInterface $urlGenerator, |
96
|
|
|
PostRepository $postRepository, |
97
|
|
|
AccessCheckerInterface $accessChecker, |
98
|
|
|
UserComponent $userComponent |
99
|
|
|
): Response { |
100
|
|
|
$userId = $userComponent->getId(); |
101
|
|
|
if (is_null($userId) || !$accessChecker->userHasPermission($userId, 'editPost')) { |
102
|
|
|
return $this->responseFactory->createResponse(403); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$slug = $request->getAttribute('slug', null); |
106
|
|
|
$post = $postRepository->fullPostPage($slug); |
107
|
|
|
if ($post === null) { |
108
|
|
|
return $this->responseFactory->createResponse(404); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$parameters = []; |
112
|
|
|
$parameters['action'] = ['blog/edit', ['slug' => $slug]]; |
113
|
|
|
|
114
|
|
|
if ($request->getMethod() === Method::POST) { |
115
|
|
|
try { |
116
|
|
|
$body = $request->getParsedBody(); |
117
|
|
|
$parameters['body'] = $body; |
118
|
|
|
|
119
|
|
|
foreach (['header', 'content'] as $name) { |
120
|
|
|
if (empty($body[$name])) { |
121
|
|
|
throw new \InvalidArgumentException(ucfirst($name) . ' is required'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$post->setTitle($body['header']); |
126
|
|
|
$post->setContent($body['content']); |
127
|
|
|
|
128
|
|
|
$transaction = new Transaction($orm); |
129
|
|
|
$transaction->persist($post); |
130
|
|
|
|
131
|
|
|
$transaction->run(); |
132
|
|
|
|
133
|
|
|
return $this->responseFactory |
134
|
|
|
->createResponse(302) |
135
|
|
|
->withHeader( |
136
|
|
|
'Location', |
137
|
|
|
$urlGenerator->generate('blog/index') |
138
|
|
|
); |
139
|
|
|
} catch (\Throwable $e) { |
140
|
|
|
$error = $e->getMessage(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$parameters['error'] = $error; |
144
|
|
|
} else { |
145
|
|
|
$parameters['body'] = [ |
146
|
|
|
'header' => $post->getTitle(), |
147
|
|
|
'content' => $post->getContent(), |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$parameters['title'] = 'Edit post'; |
152
|
|
|
return $this->viewRenderer->withCsrf()->render('__form', $parameters); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|