Passed
Push — main ( dc275a...8e4fcf )
by Slawomir
04:22
created

PostsBaseliner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 37
rs 10
ccs 12
cts 13
cp 0.9231
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A baseline() 0 4 1
A __construct() 0 5 1
A baselineExistentPosts() 0 5 2
A baselineDeletedPosts() 0 5 2
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\BaselinePostsCommand;
7
use App\Modules\Posts\Domain\Dto\DeleteExistingPostDto;
8
use App\Modules\Posts\Domain\Event\Outbound\PostBaselinedOEvent;
9
use App\Modules\Posts\Domain\Event\Outbound\PostDeletedOEvent;
10
use App\Modules\Posts\Domain\Repository\PostsFindingRepositoryInterface;
11
12
trait PostsBaseliner
13
{
14
15
16
    public function __construct(
17 24
        private PostsFindingRepositoryInterface    $findingRepository,
18
        private ApplicationEventPublisherInterface $eventPublisher
19
    )
20
    {
21
    }
22 24
23
    public function baseline(BaselinePostsCommand $command): void
24 1
    {
25
        $this->baselineExistentPosts($command);
26 1
        $this->baselineDeletedPosts($command);
27 1
28
    }
29 1
30
    /**
31
     * @param BaselinePostsCommand $command
32
     */
33
    protected function baselineDeletedPosts(BaselinePostsCommand $command): void
34 1
    {
35
        $deletedPostIds = $this->findingRepository->findDeletedPostIdsForBaseline($command->getFrom());
36 1
        foreach ($deletedPostIds as $postId) {
37
            $this->eventPublisher->publish(new PostDeletedOEvent(new DeleteExistingPostDto($postId)));
38 1
        }
39
    }
40
41 1
    /**
42
     * @param BaselinePostsCommand $command
43
     */
44
    protected function baselineExistentPosts(BaselinePostsCommand $command): void
45
    {
46 1
        $posts = $this->findingRepository->findExistingPostsForBaseline($command->getFrom());
47
        foreach ($posts as $post) {
48 1
            $this->eventPublisher->publish(new PostBaselinedOEvent($post));
49 1
        }
50
    }
51
}