Completed
Push — master ( e32f5d...09c5b8 )
by Dmitry
02:45
created

Application::dispatch()   C

Complexity

Conditions 12
Paths 19

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 25
cts 25
cp 1
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 24
nc 19
nop 3
crap 12

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Basis\Test;
4
5
use Basis\Application as BaseApplication;
6
use Basis\Cache;
7
use Basis\Converter;
8
use Basis\Runner;
9
use Basis\Test;
10
use Exception;
11
12
class Application extends BaseApplication
13
{
14
    private $test;
15
16
    public function __construct(Test $test)
17
    {
18
        parent::__construct(getcwd());
19
20
        $this->test = $test;
21
    }
22
23 39
    public function dispatch(string $job, array $params = [], string $service = null)
24
    {
25 39
        if (array_key_exists($job, $this->test->mockInstances)) {
26 8
            $mocks = $this->test->mockInstances[$job];
27 8
            $valid = null;
28 8
            foreach ($mocks as $mock) {
29 8
                if ($mock->params == $params || (!$mock->params && !$valid)) {
30 8
                    $valid = $mock;
31
                }
32
            }
33 8
            if ($valid) {
34 8
                return $this->get(Cache::class)
35 8
                    ->wrap([$job, $params, $service], function () use ($valid, $params) {
36 8
                        $result = $valid->result;
37 8
                        if (is_callable($result)) {
38 5
                            $result = $result($params);
39
                        }
40 8
                        $valid->calls++;
41 8
                        return $this->get(Converter::class)->toObject($result);
42 8
                    });
43
            }
44
        }
45 39
        if ($this->test->disableRemote) {
46 37
            if (!$this->get(Runner::class)->hasJob($job)) {
47 1
                throw new Exception("Remote calls ($job) are disabled for tests");
48
            }
49
        }
50
51 39
        $converter = $this->get(Converter::class);
52
53 39
        $global = $this->test->params ?: [];
54 39
        $global = $converter->toObject($global);
55 39
        if (is_object($global)) {
56 2
            $global = get_object_vars($global);
57
        }
58
59 39
        return parent::dispatch($job, array_merge($params, $global), $service);
60
    }
61
}
62