Test Failed
Push — master ( 79aaa0...94c60c )
by Hirofumi
02:46
created

RequeueAbandonedJobMessageService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 9
dl 11
loc 66
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
A execute() 0 20 4

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
3
namespace Shippinno\Job\Application\Messaging;
4
5
use Interop\Queue\Exception as QueueException;
6
use Interop\Queue\PsrContext;
7
use Shippinno\Job\Domain\Model\AbandonedJobMessageFailedToRequeueException;
8
use Shippinno\Job\Domain\Model\AbandonedJobMessageNotFoundException;
9
use Shippinno\Job\Domain\Model\AbandonedJobMessageStore;
10
use Shippinno\Job\Domain\Model\StoredJobSerializer;
11
12
class RequeueAbandonedJobMessageService
13
{
14
    /**
15
     * @var PsrContext
16
     */
17
    private $context;
18
19
    /**
20
     * @var AbandonedJobMessageStore
21
     */
22
    private $abandonedJobMessageStore;
23
24
    /**
25
     * @var StoredJobSerializer
26
     */
27
    private $storedJobSerializer;
28
29
    /**
30
     * @var JobFlightManager
31
     */
32
    private $jobFlightManager;
33
34
    /**
35
     * @param PsrContext $context
36
     * @param AbandonedJobMessageStore $abandonedJobMessageStore
37
     * @param StoredJobSerializer $storedJobSerializer
38
     * @param JobFlightManager|null $jobFlightManager
39
     */
40 3 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
41
        PsrContext $context,
42
        AbandonedJobMessageStore $abandonedJobMessageStore,
43
        StoredJobSerializer $storedJobSerializer,
44
        JobFlightManager $jobFlightManager = null
45
    ) {
46 3
        $this->context = $context;
47 3
        $this->abandonedJobMessageStore = $abandonedJobMessageStore;
48 3
        $this->storedJobSerializer = $storedJobSerializer;
49 3
        $this->jobFlightManager = $jobFlightManager ?: new NullJobFlightManager;
50 3
    }
51
52
    /**
53
     * @param int $abandonedJobMessageId
54
     * @throws AbandonedJobMessageFailedToRequeueException
55
     * @throws AbandonedJobMessageNotFoundException
56
     */
57 3
    public function execute(int $abandonedJobMessageId): void
58
    {
59 3
        $abandonedJobMessage = $this->abandonedJobMessageStore->abandonedJobMessageOfId($abandonedJobMessageId);
60 2
        $queue = $this->context->createQueue($abandonedJobMessage->queue());
61 2
        $message = $this->context->createMessage($abandonedJobMessage->message());
62 2
        if (method_exists($message, 'setMessageDeduplicationId')) {
63
            $message->setMessageDeduplicationId(uniqid());
64
        }
65 2
        if (method_exists($message, 'setMessageGroupId')) {
66
            $message->setMessageGroupId(uniqid());
67
        }
68
        try {
69 2
            $storedJob = $this->storedJobSerializer->deserialize($message->getBody());
70 2
            $message->setMessageId($storedJob->id());
71 2
            $this->context->createProducer()->send($queue, $message);
72 1
            $this->abandonedJobMessageStore->remove($abandonedJobMessage);
73 1
        } catch (QueueException $e) {
74 1
            throw new AbandonedJobMessageFailedToRequeueException($abandonedJobMessage->id(), $e);
75
        }
76 1
    }
77
}
78