Passed
Pull Request — master (#132)
by
unknown
12:41
created

PostController::edit()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
c 1
b 0
f 0
nc 20
nop 5
dl 0
loc 55
rs 8.8017

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $error is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of App\Blog\Entity\Post::setUser() does only seem to accept App\Entity\User, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
                $post->setUser(/** @scrutinizer ignore-type */ $user);
Loading history...
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