Completed
Push — master ( 749edd...721fe7 )
by Park Jong-Hun
10:14
created

Board::showPostingForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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\User as UserModel;
9
use App\Entity\Board as BoardModel;
10
use App\Auth\LoginManagerInterface;
11
use Doctrine\ORM\EntityManagerInterface;
12
13
class Board
14
{
15
    /**
16
     * @var EntityManagerInterface
17
     */
18
    private $entityManager;
19
20
    /**
21
     * @var BoardModel
22
     */
23
    private $board;
24
25
    public function __construct($name, EntityManagerInterface $entityManager)
26
    {
27
        $this->entityManager = $entityManager;
28
        $this->board = $entityManager->getRepository(BoardModel::class)->findOneBy(['name' => $name]);
29
    }
30
31
    public function index($name, ViewModel $viewModel)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        $posts = $this->getPosts();
34
35
        $viewModel->set('board', $this->board);
36
        $viewModel->set('posts', $posts);
37
38
        return 'default/postList';
39
    }
40
41
    public function showPostingForm($name, ViewModel $viewModel)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        $viewModel->set('board', $this->board);
44
45
        return 'default/postingForm';
46
    }
47
48
    public function write($name, $parsedBody, LoginManagerInterface $loginManager)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $post = new PostModel();
51
        $post->setBoard($this->board);
52
        $post->setSubject($parsedBody['subject']);
53
        $post->setContent($parsedBody['content']);
54
55
        if ($loginManager->getLoggedAccountId()) {
56
            /** @var UserModel */
57
            $user = $this->entityManager->getRepository(UserModel::class)
58
                        ->findOneBy(['accountId' => $loginManager->getLoggedAccountId()]);
59
60
            $post->setUser($user);
61
            $post->setAuthor($user->getNickname());
62
            $post->setPassword($user->getPassword());
63
        } else {
64
            $post->setAuthor($parsedBody['author']);
65
            $post->setPassword($parsedBody['password']);
66
        }
67
68
        $this->entityManager->persist($post);
69
        $this->entityManager->flush();
70
71
        return 'redirect: ' . Application::getUrl('/post/' . $post->getId());
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    private function getPosts()
78
    {
79
        return $this->entityManager->getRepository(PostModel::class)
80
                ->findBy(['board' => $this->board->getId()], ['id' => 'DESC']);
81
    }
82
}
83