PostsApi::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 22
ccs 10
cts 10
cp 1
crap 1
rs 9.9666

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace App\Modules\Posts\Domain;
4
5
use App\Infrastructure\Events\Api\ApplicationEventPublisherInterface;
6
use App\Infrastructure\Events\Api\ApplicationEventSubscriber;
7
use App\Infrastructure\Security\LoggedInUserProviderInterface;
8
use App\Modules\Posts\Api\Event\Inbound\CommentCreatedPostsIEvent;
9
use App\Modules\Posts\Api\Event\Inbound\CommentsBaselinedPostsIEvent;
10
use App\Modules\Posts\Api\Event\Inbound\UserRenamedPostsIEvent;
11
use App\Modules\Posts\Api\PostsApiInterface;
12
use App\Modules\Posts\Domain\Logic\CommentsEventsHandler;
13
use App\Modules\Posts\Domain\Logic\PostsBaseliner;
14
use App\Modules\Posts\Domain\Logic\PostsCreator;
15
use App\Modules\Posts\Domain\Logic\PostsFinder;
16
use App\Modules\Posts\Domain\Logic\PostsRemover;
17
use App\Modules\Posts\Domain\Logic\PostsValidator;
18
use App\Modules\Posts\Domain\Logic\PostUpdater;
19
use App\Modules\Posts\Domain\Logic\SecurityEventsHandler;
20
use App\Modules\Posts\Domain\Repository\PostsCommentsEventHandlingRepositoryInterface;
21
use App\Modules\Posts\Domain\Repository\PostsCreationRepositoryInterface;
22
use App\Modules\Posts\Domain\Repository\PostsDeletionRepositoryInterface;
23
use App\Modules\Posts\Domain\Repository\PostsFindingRepositoryInterface;
24
use App\Modules\Posts\Domain\Repository\PostsSecurityEventsHandlingRepositoryInterface;
25
use App\Modules\Posts\Domain\Repository\PostsUpdatingRepositoryInterface;
26
use App\Modules\Posts\Domain\Transactions\PostTransactionFactoryInterface;
27
use Psr\Log\LoggerInterface;
28
29
class PostsApi extends ApplicationEventSubscriber implements PostsApiInterface
30
{
31
    use PostsCreator {
32
        PostsCreator::__construct as private __creatorConstruct;
33
    }
34
35
    use PostUpdater {
36
        PostUpdater::__construct as private __updaterConstruct;
37
    }
38
39
    use PostsRemover {
40
        PostsRemover::__construct as private __removerConstruct;
41
    }
42
43
    use PostsFinder {
44
        PostsFinder::__construct as private __finderConstruct;
45
    }
46
47
    use CommentsEventsHandler {
48
        CommentsEventsHandler::__construct as private __commentsEventHandlerConstruct;
49
    }
50
51
    use SecurityEventsHandler {
52
        SecurityEventsHandler::__construct as __securityEventsHandlerConstruct;
53
    }
54
55
    use PostsBaseliner {
56
        PostsBaseliner::__construct as private __postsBaselinerConstruct;
57
    }
58
59
    /**
60
     * @param ApplicationEventPublisherInterface $eventPublisher
61
     * @param PostsCreationRepositoryInterface $creationRepository
62
     * @param PostsUpdatingRepositoryInterface $updatingRepository
63
     * @param PostsDeletionRepositoryInterface $deletionRepository
64
     * @param PostsFindingRepositoryInterface $findingRepository
65
     * @param LoggedInUserProviderInterface $loggedInUserProvider
66
     * @param PostTransactionFactoryInterface $transactionFactory
67
     * @param PostsCommentsEventHandlingRepositoryInterface $commentsEventHandlingRepository
68
     * @param PostsSecurityEventsHandlingRepositoryInterface $securityEventsHandlingRepository
69
     * @param LoggerInterface $logger
70
     */
71 24
    public function __construct(
72
        ApplicationEventPublisherInterface             $eventPublisher,
73
        PostsCreationRepositoryInterface               $creationRepository,
74
        PostsUpdatingRepositoryInterface               $updatingRepository,
75
        PostsDeletionRepositoryInterface               $deletionRepository,
76
        PostsFindingRepositoryInterface                $findingRepository,
77
        LoggedInUserProviderInterface                  $loggedInUserProvider,
78
        PostTransactionFactoryInterface                $transactionFactory,
79
        PostsCommentsEventHandlingRepositoryInterface  $commentsEventHandlingRepository,
80
        PostsSecurityEventsHandlingRepositoryInterface $securityEventsHandlingRepository,
81
        LoggerInterface                                $logger
82
    )
83
    {
84 24
        parent::__construct($logger);
85 24
        $validator = new PostsValidator($findingRepository);
86 24
        $this->__creatorConstruct($eventPublisher, $creationRepository, $loggedInUserProvider, $transactionFactory, $validator);
87 24
        $this->__updaterConstruct($eventPublisher, $updatingRepository, $transactionFactory, $validator);
88 24
        $this->__removerConstruct($eventPublisher, $deletionRepository, $transactionFactory, $validator);
89 24
        $this->__finderConstruct($findingRepository);
90 24
        $this->__commentsEventHandlerConstruct($transactionFactory, $commentsEventHandlingRepository, $validator);
91 24
        $this->__postsBaselinerConstruct($findingRepository, $eventPublisher);
92 24
        $this->__securityEventsHandlerConstruct($transactionFactory, $securityEventsHandlingRepository);
93 24
    }
94
95 24
    /**
96
     * @return array<string, string>
97
     */
98 24
    protected function subscribe(): array
99 24
    {
100 24
        return [
101
            CommentCreatedPostsIEvent::class => 'onCommentCreated',
102
            CommentsBaselinedPostsIEvent::class => 'onCommentsBaselined',
103 1
            UserRenamedPostsIEvent::class => 'onUserRenamed',
104
        ];
105
    }
106
}