Passed
Push — main ( 56e558...7a7f73 )
by Slawomir
03:59
created

PostsCommandController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 53
ccs 14
cts 16
cp 0.875
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A deletePost() 0 9 2
A updatePost() 0 9 2
A __construct() 0 4 1
A createPost() 0 5 1
1
<?php
2
3
namespace App\Modules\Posts\Api\Controller;
4
5
use App\Infrastructure\Security\Permission;
6
use App\Modules\Posts\Api\Command\CreatePostCommand;
7
use App\Modules\Posts\Api\Command\DeletePostCommand;
8
use App\Modules\Posts\Api\Command\UpdatePostCommand;
9
use App\Modules\Posts\Api\PostsApiInterface;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
13
use Symfony\Component\Routing\Annotation\Route;
14
use Symfony\Component\Uid\Ulid;
15
16
#[Route('/api/admin/posts')]
17
class PostsCommandController extends AbstractController
18
{
19
    /**
20
     * @param PostsApiInterface $postsApi
21
     */
22
    public function __construct(
23 7
        private PostsApiInterface $postsApi
24
    )
25
    {
26
    }
27 7
28
    /**
29
     * @param CreatePostCommand $command
30
     * @return Response
31
     */
32
    #[Route('/', methods: ['POST'])]
33 7
    public function createPost(CreatePostCommand $command): Response
34
    {
35
        return $this->json(
36 7
            $this->postsApi->createPost($command)
37 7
        );
38
    }
39
40
    /**
41
     * @param UpdatePostCommand $command
42
     * @param string $id
43
     * @return Response
44
     */
45
    #[Route('/{id}', methods: ['PUT'])]
46 1
    public function updatePost(UpdatePostCommand $command, string $id): Response
47
    {
48
        $command->setId(new Ulid($id));
49 1
        if(!$this->isGranted(Permission::EDIT, $command)) {
50 1
            throw new UnauthorizedHttpException(Permission::EDIT);
51
        }
52
        $this->postsApi->updatePost($command);
1 ignored issue
show
Bug introduced by
It seems like $command can also be of type null; however, parameter $command of App\Modules\Posts\Api\Po...Interface::updatePost() does only seem to accept App\Modules\Posts\Api\Command\UpdatePostCommand, 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

52
        $this->postsApi->updatePost(/** @scrutinizer ignore-type */ $command);
Loading history...
53 1
        return new Response();
54 1
    }
55
56
    /**
57
     * @param string $id
58
     * @return Response
59
     */
60
    #[Route('/{id}', methods: ['DELETE'])]
61 1
    public function deletePost(string $id): Response
62
    {
63
        $command = new DeletePostCommand(new Ulid($id));
64 1
        if(!$this->isGranted(Permission::DELETE, $command)) {
65 1
            throw new UnauthorizedHttpException(Permission::DELETE);
66
        }
67
        $this->postsApi->deletePost($command);
1 ignored issue
show
Bug introduced by
It seems like $command can also be of type null; however, parameter $command of App\Modules\Posts\Api\Po...Interface::deletePost() does only seem to accept App\Modules\Posts\Api\Command\DeletePostCommand, 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

67
        $this->postsApi->deletePost(/** @scrutinizer ignore-type */ $command);
Loading history...
68 1
        return new Response();
69 1
    }
70
71
}