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

Post::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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()]);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->entityManager->ge... $this->post->getId())) of type array is incompatible with the declared type object<App\Entity\Comment> of property $comment.

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..

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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