Completed
Push — master ( 2ea06c...bb888f )
by Jaroslaw
13s
created

Producer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 2
dl 59
loc 59
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A add() 5 5 1
A produce() 8 8 2
A getTasks() 4 4 1
A getQueueWriter() 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\Task;
3
4
use Cmp\Queues\Domain\Queue\QueueWriter;
5
use Cmp\Queues\Domain\Task\Exception\TaskException;
6
7 View Code Duplication
class Producer
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 Task[]
16
     */
17
    protected $tasks = [];
18
19
    /**
20
     * Producer constructor.
21
     * @param QueueWriter $queueWriter
22
     */
23
    public function __construct(QueueWriter $queueWriter)
24
    {
25
        $this->queueWriter = $queueWriter;
26
    }
27
28
    /**
29
     * @param TaskInterface $task
30
     * @return $this
31
     */
32
    public function add(TaskInterface $task)
33
    {
34
        $this->tasks[] = $task;
35
        return $this;
36
    }
37
38
    /**
39
     * @throws TaskException
40
     */
41
    public function produce()
42
    {
43
        if(!isset($this->tasks[0])) {
44
            throw new TaskException('You must add at least 1 Task before producing.');
45
        }
46
        $this->queueWriter->write($this->tasks);
47
        $this->tasks = [];
48
    }
49
50
    /**
51
     * @return Task[]
52
     */
53
    public function getTasks()
54
    {
55
        return $this->tasks;
56
    }
57
58
    /**
59
     * @return QueueWriter
60
     */
61
    public function getQueueWriter()
62
    {
63
        return $this->queueWriter;
64
    }
65
}
66