yiisoft /
yii-demo
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace App\Blog\Post; |
||||
| 6 | |||||
| 7 | use App\Blog\Entity\Post; |
||||
| 8 | use App\Service\WebControllerService; |
||||
| 9 | use App\User\UserService; |
||||
| 10 | use Psr\Http\Message\ResponseInterface as Response; |
||||
| 11 | use Psr\Http\Message\ServerRequestInterface as Request; |
||||
| 12 | use Yiisoft\Http\Method; |
||||
| 13 | use Yiisoft\Validator\ValidatorInterface; |
||||
| 14 | use Yiisoft\Yii\View\ViewRenderer; |
||||
| 15 | |||||
| 16 | final class PostController |
||||
| 17 | { |
||||
| 18 | private ViewRenderer $viewRenderer; |
||||
| 19 | private WebControllerService $webService; |
||||
| 20 | private PostService $postService; |
||||
| 21 | private UserService $userService; |
||||
| 22 | |||||
| 23 | public function __construct( |
||||
| 24 | ViewRenderer $viewRenderer, |
||||
| 25 | WebControllerService $webService, |
||||
| 26 | PostService $postService, |
||||
| 27 | UserService $userService |
||||
| 28 | ) { |
||||
| 29 | $this->viewRenderer = $viewRenderer->withControllerName('blog/post'); |
||||
| 30 | $this->webService = $webService; |
||||
| 31 | $this->postService = $postService; |
||||
| 32 | $this->userService = $userService; |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | public function index(Request $request, PostRepository $postRepository): Response |
||||
| 36 | { |
||||
| 37 | $canEdit = $this->userService->hasPermission('editPost'); |
||||
| 38 | $slug = $request->getAttribute('slug', null); |
||||
| 39 | $item = $postRepository->fullPostPage($slug); |
||||
| 40 | if ($item === null) { |
||||
| 41 | return $this->webService->getNotFoundResponse(); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | return $this->viewRenderer->render('index', ['item' => $item, 'canEdit' => $canEdit, 'slug' => $slug]); |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | public function add(Request $request, ValidatorInterface $validator): Response |
||||
| 48 | { |
||||
| 49 | $parameters = [ |
||||
| 50 | 'title' => 'Add post', |
||||
| 51 | 'action' => ['blog/add'], |
||||
| 52 | 'errors' => [], |
||||
| 53 | 'body' => $request->getParsedBody(), |
||||
| 54 | ]; |
||||
| 55 | |||||
| 56 | if ($request->getMethod() === Method::POST) { |
||||
| 57 | $form = new PostForm(); |
||||
| 58 | if ($form->load($parameters['body']) && $validator->validate($form)->isValid()) { |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 59 | $this->postService->savePost($this->userService->getUser(), new Post(), $form); |
||||
| 60 | return $this->webService->getRedirectResponse('blog/index'); |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | $parameters['errors'] = $form->getFirstErrors(); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | return $this->viewRenderer->render('__form', $parameters); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | public function edit( |
||||
| 70 | Request $request, |
||||
| 71 | PostRepository $postRepository, |
||||
| 72 | ValidatorInterface $validator |
||||
| 73 | ): Response { |
||||
| 74 | $slug = $request->getAttribute('slug', null); |
||||
| 75 | $post = $postRepository->fullPostPage($slug); |
||||
| 76 | if ($post === null) { |
||||
| 77 | return $this->webService->getNotFoundResponse(); |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | $parameters = [ |
||||
| 81 | 'title' => 'Edit post', |
||||
| 82 | 'action' => ['blog/edit', ['slug' => $slug]], |
||||
| 83 | 'errors' => [], |
||||
| 84 | 'body' => [ |
||||
| 85 | 'title' => $post->getTitle(), |
||||
| 86 | 'content' => $post->getContent(), |
||||
| 87 | 'tags' => $this->postService->getPostTags($post), |
||||
| 88 | ], |
||||
| 89 | ]; |
||||
| 90 | |||||
| 91 | if ($request->getMethod() === Method::POST) { |
||||
| 92 | $form = new PostForm(); |
||||
| 93 | $body = $request->getParsedBody(); |
||||
| 94 | if ($form->load($body) && $validator->validate($form)->isValid()) { |
||||
|
0 ignored issues
–
show
It seems like
$body can also be of type null and object; however, parameter $data of Yiisoft\Form\FormModel::load() does only seem to accept array, 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
Loading history...
|
|||||
| 95 | $this->postService->savePost($this->userService->getUser(), $post, $form); |
||||
| 96 | return $this->webService->getRedirectResponse('blog/index'); |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | $parameters['body'] = $body; |
||||
| 100 | $parameters['errors'] = $form->getFirstErrors(); |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | return $this->viewRenderer->render('__form', $parameters); |
||||
| 104 | } |
||||
| 105 | } |
||||
| 106 |