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

Application   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 50
ccs 25
cts 29
cp 0.8621
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C dispatch() 0 38 12
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