Completed
Push — master ( 4848aa...64d9da )
by Dmitry
03:44
created

Application   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 46
ccs 20
cts 25
cp 0.8
rs 10
c 0
b 0
f 0

2 Methods

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