Application::dispatch()   C
last analyzed

Complexity

Conditions 13
Paths 38

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 13.0096

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 25
cts 26
cp 0.9615
rs 6.6166
c 0
b 0
f 0
cc 13
nc 38
nop 3
crap 13.0096

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