Test Failed
Push — master ( 9d048f...9d083f )
by Mathijs
07:23
created

JobService::execute()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 11
nc 3
nop 1
crap 6
1
<?php
2
3
namespace mcorten87\rabbitmq_api\services;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use mcorten87\rabbitmq_api\jobs\JobBase;
8
use mcorten87\rabbitmq_api\MqManagementFactory;
9
use mcorten87\rabbitmq_api\objects\JobResult;
10
11
class JobService
12
{
13
    /** @var MqManagementFactory */
14
    private $factory;
15
16
    /** @var Client */
17
    private $client;
18
19
    public function __construct(MqManagementFactory $factory, Client $client)
20
    {
21
        $this->factory = $factory;
22
        $this->client = $client;
23
    }
24
25
    /**
26
     * @param JobBase $job
27
     * @return JobResult
28
     * @throws \mcorten87\rabbitmq_api\exceptions\NoMapperForJob
29
     */
30
    public function execute(JobBase $job) : JobResult
31
    {
32
        $mapper = $this->factory->getJobMapper($job);
33
34
        $mapResult = $mapper->map($job);
35
36
        try {
37
            $res = $this->client->request(
38
                $mapResult->getMethod(),
39
                $this->factory->getConfig()->getUrl().$mapResult->getUrl(),
40
                $mapResult->getConfig()
41
            );
42
43
            return new JobResult($res);
44
        } catch (ClientException $e) {
45
            return JobResult::populateFromClientException($e);
46
        }
47
    }
48
}
49