PostsRemover   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 41
ccs 16
cts 16
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fromDeleteCommand() 0 3 1
A deletePost() 0 12 1
1
<?php
2
3
namespace App\Modules\Posts\Domain\Logic;
4
5
use App\Infrastructure\Events\Api\ApplicationEventPublisherInterface;
6
use App\Modules\Posts\Api\Command\DeletePostCommand;
7
use App\Modules\Posts\Domain\Dto\DeleteExistingPostDto;
8
use App\Modules\Posts\Domain\Event\Outbound\PostDeletedOEvent;
9
use App\Modules\Posts\Domain\Repository\PostsDeletionRepositoryInterface;
10
use App\Modules\Posts\Domain\Transactions\PostTransactionFactoryInterface;
11
12
trait PostsRemover
13
{
14
    /**
15
     * @param ApplicationEventPublisherInterface $eventPublisher
16
     * @param PostsDeletionRepositoryInterface $deletionRepository
17
     * @param PostTransactionFactoryInterface $transactionFactory
18
     * @param PostsValidator $validator
19
     */
20 24
    public function __construct(
21
        private ApplicationEventPublisherInterface $eventPublisher,
22
        private PostsDeletionRepositoryInterface   $deletionRepository,
23
        private PostTransactionFactoryInterface    $transactionFactory,
24
        private PostsValidator                     $validator
25
    )
26
    {
27 24
    }
28
29
    /**
30
     * @param DeletePostCommand $command
31
     */
32 3
   public function deletePost(DeletePostCommand $command): void
33
    {
34 3
        $this->validator->preDelete($command);
35 2
        $deletedPost = $this->fromDeleteCommand($command);
36 2
        $this->transactionFactory
37 2
            ->createTransaction(function () use ($deletedPost) {
38 2
                $this->deletionRepository->deletePost($deletedPost);
39 2
            })
40 2
            ->afterCommit(function () use ($deletedPost) {
41 2
                $this->publisher->publish(new PostDeletedOEvent($deletedPost));
42 2
            })
43 2
            ->execute();
44 2
    }
45
46
    /**
47
     * @param DeletePostCommand $command
48
     * @return DeleteExistingPostDto
49
     */
50 2
    private function fromDeleteCommand(DeletePostCommand $command): DeleteExistingPostDto
51
    {
52 2
        return new DeleteExistingPostDto($command->getId());
53
    }
54
}