Completed
Push — master ( 1f1a8e...ab56b8 )
by Dmitry
03:54
created

Mapper::findOne()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 8.8571
cc 5
eloc 6
nc 4
nop 2
crap 5
1
<?php
2
3
namespace Basis\Test;
4
5
use Basis\Test;
6
use Exception;
7
8
class Mapper
9
{
10
    protected $test;
11
    public $serviceName;
12
13 2
    public function __construct(Test $test, $service)
14
    {
15 2
        $this->test = $test;
16 2
        $this->serviceName = $service;
17 2
    }
18
19 1
    public function find(string $space, $params = [])
20
    {
21 1
        $key = $this->serviceName.'.'.$space;
22 1
        if (array_key_exists($key, $this->test->data)) {
23 1
            $data = $this->test->data[$key];
24 1
            foreach ($data as $i => $v) {
25 1
                if (count($params) && array_intersect_assoc($params, $v) != $params) {
26 1
                    unset($data[$i]);
27 1
                    continue;
28
                }
29 1
                $data[$i] = (object) $v;
30
            }
31 1
            $data = array_values($data);
32 1
            return $data;
33
        }
34
        return [];
35
    }
36
37 1
    public function findOne(string $space, $params = [])
38
    {
39 1
        $key = $this->serviceName.'.'.$space;
40 1
        if (array_key_exists($key, $this->test->data)) {
41 1
            foreach ($this->test->data[$key] as $candidate) {
42 1
                if (!count($params) || array_intersect_assoc($params, $candidate) == $params) {
43 1
                    return (object) $candidate;
44
                }
45
            }
46
        }
47 1
    }
48
49 1
    public function findOrFail(string $space, $params = [])
50
    {
51 1
        $result = $this->findOne($space, $params);
52 1
        if (!$result) {
53 1
            throw new Exception("No ".$space.' found using '.json_encode($params));
54
        }
55 1
        return $result;
56
    }
57
}
58