Publisher   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 52
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A add() 5 5 1
A publish() 8 8 2
A getEvents() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Cmp\Queues\Domain\Event;
3
4
use Cmp\Queues\Domain\Event\Exception\DomainEventException;
5
use Cmp\Queues\Domain\Queue\QueueWriter;
6
7 View Code Duplication
class Publisher
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
{
9
    /**
10
     * @var QueueWriter
11
     */
12
    protected $queueWriter;
13
14
    /**
15
     * @var DomainEvent[]
16
     */
17
    protected $events = [];
18
19
    /**
20
     * Publisher constructor.
21
     * @param QueueWriter $queueWriter
22
     */
23
    public function __construct(QueueWriter $queueWriter)
24
    {
25
        $this->queueWriter = $queueWriter;
26
    }
27
28
    /**
29
     * Add Domain Events to buffer
30
     * @param DomainEvent $event
31
     * @return $this
32
     */
33
    public function add(DomainEvent $event)
34
    {
35
        $this->events[] = $event;
36
        return $this;
37
    }
38
    
39
    /**
40
     * @throws DomainEventException
41
     */
42
    public function publish()
43
    {
44
        if(!isset($this->events[0])) {
45
            throw new DomainEventException('You must add at least 1 DomainEvent in order to publish to queue.');
46
        }
47
        $this->queueWriter->write($this->events);
48
        $this->events = [];
49
    }
50
51
    /**
52
     * @return DomainEvent[]
53
     */
54
    public function getEvents()
55
    {
56
        return $this->events;
57
    }
58
}