Passed
Push — master ( 806c04...bf1804 )
by Hirofumi
05:36
created

RequeueAbandonedJobMessageService::execute()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 13
cp 0.8462
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 12
nop 1
crap 4.0582
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\AbandonedJobMessageStore;
9
10
class RequeueAbandonedJobMessageService
11
{
12
    /**
13
     * @var PsrContext
14
     */
15
    private $context;
16
17
    /**
18
     * @var AbandonedJobMessageStore
19
     */
20
    private $abandonedJobMessageStore;
21
22
    /**
23
     * @param PsrContext $context
24
     * @param AbandonedJobMessageStore $abandonedJobMessageStore
25
     */
26 3
    public function __construct(
27
        PsrContext $context,
28
        AbandonedJobMessageStore $abandonedJobMessageStore
29
    ) {
30 3
        $this->context = $context;
31 3
        $this->abandonedJobMessageStore = $abandonedJobMessageStore;
32 3
    }
33
34
    /**
35
     * @param int $abandonedJobMessageId
36
     * @throws AbandonedJobMessageFailedToRequeueException
37
     * @throws \Shippinno\Job\Domain\Model\AbandonedJobMessageNotFoundException
38
     */
39 3
    public function execute(int $abandonedJobMessageId): void
40
    {
41 3
        $abandonedJobMessage = $this->abandonedJobMessageStore->abandonedJobMessageOfId($abandonedJobMessageId);
42 2
        $queue = $this->context->createQueue($abandonedJobMessage->queue());
43 2
        $message = $this->context->createMessage($abandonedJobMessage->message());
44 2
        if (method_exists($message, 'setMessageDeduplicationId')) {
45
            $message->setMessageDeduplicationId(uniqid());
46
        }
47 2
        if (method_exists($message, 'setMessageGroupId')) {
48
            $message->setMessageGroupId(uniqid());
49
        }
50
        try {
51 2
            $this->context->createProducer()->send($queue, $message);
52 1
            $this->abandonedJobMessageStore->remove($abandonedJobMessage);
0 ignored issues
show
Bug introduced by
It seems like $abandonedJobMessage defined by $this->abandonedJobMessa...$abandonedJobMessageId) on line 41 can be null; however, Shippinno\Job\Domain\Mod...bMessageStore::remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
53 1
        } catch (QueueException $e) {
54 1
            throw new AbandonedJobMessageFailedToRequeueException($abandonedJobMessage->id(), $e);
55
        }
56 1
    }
57
}
58