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

RequeueAbandonedJobMessageService::execute()   A

Complexity

Conditions 4
Paths 20

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 13
cts 15
cp 0.8667
rs 9.6
c 0
b 0
f 0
cc 4
nc 20
nop 1
crap 4.0378
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