TicketService   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
dl 0
loc 151
c 0
b 0
f 0
wmc 13
lcom 1
cbo 9
ccs 58
cts 60
cp 0.9667
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A onBitbucketTicketMapped() 0 11 2
A onJiraTicketMapped() 0 9 1
A onTicketDirIndexed() 0 9 1
A buildTicket() 0 6 2
A printCompletionInfo() 0 12 1
A ticketIsComplete() 0 4 3
A getTicket() 0 16 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace AppBuilder\Application\Module\TicketAggregate;
6
7
use AppBuilder\Application\Model\Exception\NullArgumentException;
8
use AppBuilder\Application\Model\ValueObject\Ticket;
9
use AppBuilder\Event\Application\BitbucketTicketMappedEvent;
10
use AppBuilder\Event\Application\BitbucketTicketMappedEventAware;
11
use AppBuilder\Event\Application\FullTicketBuiltEvent;
12
use AppBuilder\Event\Application\JiraTicketMappedEvent;
13
use AppBuilder\Event\Application\JiraTicketMappedEventAware;
14
use AppBuilder\Event\Application\TicketDirIndexedEvent;
15
use AppBuilder\Event\Application\TicketDirIndexedEventAware;
16
use Psr\Log\LoggerInterface;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
19
class TicketService implements
20
    JiraTicketMappedEventAware,
21
    BitbucketTicketMappedEventAware,
22
    TicketDirIndexedEventAware
23
{
24
    /** @var array */
25
    private $jiraData;
26
27
    /** @var array */
28
    private $bitBucketData;
29
30
    /** @var array */
31
    private $dirData;
32
33
    /** @var TicketBuilder */
34
    private $builder;
35
36
    /** @var Ticket */
37
    private $ticket;
38
39
    /** @var EventDispatcherInterface */
40
    private $dispatcher;
41
42
    /** @var LoggerInterface */
43
    private $logger;
44
45
    /** @var bool */
46
    private $jiraCompleted;
47
48
    /** @var bool */
49
    private $bbCompleted;
50
51
    /** @var bool */
52
    private $dirCompleted;
53
54
    /** @var int */
55
    private $parts;
56
57
    /** @var int */
58
    private $currentId;
59
60 1
    public function __construct(
61
        TicketBuilder $builder,
62
        EventDispatcherInterface $dispatcher,
63
        LoggerInterface $logger
64
    ) {
65 1
        $this->builder       = $builder;
66 1
        $this->dispatcher    = $dispatcher;
67 1
        $this->logger        = $logger;
68 1
        $this->jiraCompleted = false;
69 1
        $this->bbCompleted   = false;
70 1
        $this->dirCompleted  = false;
71 1
        $this->parts         = 0;
72 1
    }
73
74
    /**
75
     * Uses data array passed in event to build BitBucket part of ticket.
76
     */
77 1
    public function onBitbucketTicketMapped(BitbucketTicketMappedEvent $event) : void
78
    {
79 1
        foreach ($event->bitbucketTicket() as $key => $value) {
80 1
            $this->bitBucketData = $value;
81 1
            $this->currentId     = $key;
0 ignored issues
show
Documentation Bug introduced by
It seems like $key can also be of type string. However, the property $currentId is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
82
        }
83 1
        ++$this->parts;
84 1
        $this->bbCompleted = true;
85 1
        $this->printCompletionInfo();
86 1
        $this->buildTicket();
87 1
    }
88
89
    /**
90
     * Uses data array passed in event to build JIRA part of ticket.
91
     */
92 1
    public function onJiraTicketMapped(JiraTicketMappedEvent $event) : void
93
    {
94 1
        $this->jiraData  = $event->ticket();
95 1
        $this->currentId = $event->ticket()['id'];
96 1
        ++$this->parts;
97 1
        $this->jiraCompleted = true;
98 1
        $this->printCompletionInfo();
99 1
        $this->buildTicket();
100 1
    }
101
102
    /**
103
     * Uses data array passed in event to build part of ticket with local directory info.
104
     */
105 1
    public function onTicketDirIndexed(TicketDirIndexedEvent $event) : void
106
    {
107 1
        $this->dirData   = $event->indexedDir();
108 1
        $this->currentId = $event->indexedDir()['ticketId'];
109 1
        ++$this->parts;
110 1
        $this->dirCompleted = true;
111 1
        $this->printCompletionInfo();
112 1
        $this->buildTicket();
113 1
    }
114
115
    /**
116
     * Calls ticketIsComplete method.
117
     * Then calls getTicket method if true returned.
118
     */
119 1
    private function buildTicket() : void
120
    {
121 1
        if ($this->ticketIsComplete()) {
122 1
            $this->getTicket();
123
        }
124 1
    }
125
126
    /**
127
     * Logs how many parts of ticket are already ready.
128
     */
129 1
    private function printCompletionInfo() : void
130
    {
131
        $this
132 1
            ->logger
133 1
            ->info(
134
                'Ticket '
135 1
                . $this->currentId
136 1
                . ': '
137 1
                . $this->parts
138 1
                . '/3 parts built'
139
            );
140 1
    }
141
142
    /**
143
     * Checks whether all 3 parts of ticket were built.
144
     */
145 1
    private function ticketIsComplete() : bool
146
    {
147 1
        return $this->jiraCompleted && $this->bbCompleted && $this->dirCompleted;
148
    }
149
150
    /**
151
     * Gets ticket from ticket builder when all parts are complete.
152
     */
153 1
    private function getTicket() : void
154
    {
155 1
        $this->builder->addDirectoryData($this->dirData);
156 1
        $this->builder->addPullRequestData($this->bitBucketData);
157 1
        $this->builder->addTicketData($this->jiraData);
158
        try {
159 1
            $this->ticket = $this->builder->ticket();
160 1
            $this->logger->info('Ticket id: ' . $this->ticket->id() . ' created');
161 1
            $this->dispatcher->dispatch(
162 1
                FullTicketBuiltEvent::NAME,
163 1
                new FullTicketBuiltEvent($this->ticket)
164
            );
165
        } catch (NullArgumentException $e) {
166
            $this->logger->warning($e->getMessage());
167
        }
168 1
    }
169
}
170