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