Completed
Push — master ( 56fb48...1f1a8e )
by Dmitry
04:18
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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 37
    public function dispatch(string $job, array $params = [], string $service = null)
23
    {
24 37
        if (array_key_exists($job, $this->test->mockInstances)) {
25 7
            $mocks = $this->test->mockInstances[$job];
26 7
            $valid = null;
27 7
            foreach ($mocks as $mock) {
28 7
                if ($mock->params == $params || (!$mock->params && !$valid)) {
29 7
                    $valid = $mock;
30
                }
31
            }
32 7
            if ($valid) {
33 7
                $result = $valid->result;
34 7
                if (is_callable($result)) {
35 4
                    $result = $result($params);
36
                }
37 7
                $valid->calls++;
38 7
                return $this->get(Converter::class)->toObject($result);
39
            }
40
        }
41 37
        if ($this->test->disableRemote) {
42 36
            if (!$this->get(Runner::class)->hasJob($job)) {
43 1
                throw new Exception("Remote calls ($job) are disabled for tests");
44
            }
45
        }
46
47 37
        $converter = $this->get(Converter::class);
48
49 37
        $global = $this->test->params ?: [];
50 37
        $global = $converter->toObject($global);
51 37
        if (is_object($global)) {
52 2
            $global = get_object_vars($global);
53
        }
54
55 37
        return parent::dispatch($job, array_merge($params, $global), $service);
56
    }
57
}
58