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

Mapper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 50
ccs 27
cts 28
cp 0.9643
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B find() 0 17 5
B findOne() 0 11 5
A findOrFail() 0 8 2
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