|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Core\ViewModel; |
|
6
|
|
|
use Core\Application; |
|
7
|
|
|
use App\Entity\Post as PostModel; |
|
8
|
|
|
use App\Entity\Board as BoardModel; |
|
9
|
|
|
use App\Entity\Comment as CommentModel; |
|
10
|
|
|
use App\Entity\User as UserModel; |
|
11
|
|
|
use App\Auth\LoginManagerInterface; |
|
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
13
|
|
|
use \DateTime; |
|
14
|
|
|
|
|
15
|
|
|
class Post |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EntityManagerInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $entityManager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var PostModel |
|
24
|
|
|
*/ |
|
25
|
|
|
private $post; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var CommentModel |
|
29
|
|
|
*/ |
|
30
|
|
|
private $comment; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct($id, EntityManagerInterface $entityManager) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->entityManager = $entityManager; |
|
35
|
|
|
|
|
36
|
|
|
$this->post = $this->entityManager->getRepository(PostModel::class)->find($id); |
|
37
|
|
|
$this->comment = $this->entityManager->getRepository(CommentModel::class)->findBy(['post' => $this->post->getId()]); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function index(ViewModel $viewModel) |
|
41
|
|
|
{ |
|
42
|
|
|
$viewModel->set('post', $this->post); |
|
43
|
|
|
$viewModel->set('comments', $this->comment); |
|
44
|
|
|
|
|
45
|
|
|
return 'default/post'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function showEditForm(ViewModel $viewModel) |
|
49
|
|
|
{ |
|
50
|
|
|
$viewModel->set('post', $this->post); |
|
51
|
|
|
|
|
52
|
|
|
return 'default/postingForm'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function edit($id, $parsedBody, LoginManagerInterface $loginManager) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$this->post->setSubject($parsedBody['subject']); |
|
58
|
|
|
$this->post->setContent($parsedBody['content']); |
|
59
|
|
|
$this->post->setUpdatedAt(new DateTime()); |
|
60
|
|
|
$this->fillUserInfomanionByLoginAccount($this->post, $parsedBody, $loginManager); |
|
61
|
|
|
|
|
62
|
|
|
$this->entityManager->flush(); |
|
63
|
|
|
|
|
64
|
|
|
return 'redirect: ' . Application::getUrl('/post/' . $this->post->getId()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function writeComment($id, $parsedBody, LoginManagerInterface $loginManager) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$comment = new CommentModel(); |
|
70
|
|
|
$comment->setPost($this->post); |
|
71
|
|
|
$comment->setContent($parsedBody['content']); |
|
72
|
|
|
$this->fillUserInfomanionByLoginAccount($comment, $parsedBody, $loginManager); |
|
73
|
|
|
|
|
74
|
|
|
$this->entityManager->persist($comment); |
|
75
|
|
|
$this->entityManager->flush(); |
|
76
|
|
|
|
|
77
|
|
|
return 'redirect: ' . Application::getUrl('/post/' . $this->post->getId()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
View Code Duplication |
private function fillUserInfomanionByLoginAccount($userContent, $parsedBody, LoginManagerInterface $loginManager) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
if ($loginManager->getLoggedAccountId()) { |
|
83
|
|
|
/** @var UserModel */ |
|
84
|
|
|
$user = $this->entityManager->getRepository(UserModel::class) |
|
85
|
|
|
->findOneBy(['accountId' => $loginManager->getLoggedAccountId()]); |
|
86
|
|
|
|
|
87
|
|
|
$userContent->setUser($user); |
|
88
|
|
|
$userContent->setAuthor($user->getNickname()); |
|
89
|
|
|
$userContent->setPassword($user->getPassword()); |
|
90
|
|
|
} else { |
|
91
|
|
|
$userContent->setAuthor($parsedBody['author']); |
|
92
|
|
|
$userContent->setPassword($parsedBody['password']); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..