Mock   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
c 0
b 0
f 0
dl 0
loc 95
rs 10
wmc 27

13 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteById() 0 2 1
A findAggregate() 0 2 1
A __construct() 0 3 2
A findById() 0 2 2
A getPrimaryKey() 0 2 1
A findByField() 0 10 4
A getErrors() 0 2 1
A saveSinglePk() 0 10 2
A getOrderFunction() 0 14 5
A deleteByField() 0 6 3
A getMultiPkSaveId() 0 6 2
A save() 0 3 2
A saveMultiPk() 0 4 1
1
<?php
2
namespace Maphper\DataSource;
3
use Maphper\Maphper;
4
class Mock implements \Maphper\DataSource {
5
    private $data;
6
    private $id;
7
8
    public function __construct(\ArrayObject $data, $id) {
9
        $this->data = $data;
10
        $this->id = is_array($id) ? $id : [$id];
11
    }
12
13
    public function getPrimaryKey() {
14
        return $this->id;
15
    }
16
17
    public function findById($id) {
18
        return isset($this->data[$id]) ? (array)$this->data[$id] : [];
19
    }
20
21
    public function findByField(array $fields, $options = []) {
22
        $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator()));
23
        $filteredArray = $arrayFilter->filter($fields);
24
        if (isset($options['order'])) {
25
            list($columns, $order) = explode(' ', $options['order']);
26
            usort($filteredArray, $this->getOrderFunction($order, $columns));
27
        }
28
        if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']);
29
        if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']);
30
        return $filteredArray;
31
    }
32
33
  	public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
34
        return $function($this->findByField($criteria));
35
    }
36
37
  	public function deleteById($id) {
38
        unset($this->data[$id]);
39
    }
40
41
  	public function deleteByField(array $fields, array $options) {
42
        foreach ($this->findByField($fields, $options) as $val) {
43
            if (count($this->id) > 1) $id = $this->getMultiPkSaveId($val);
44
            else $id = $val->{$this->id[0]};
45
46
            unset($this->data[$id]);
47
        }
48
    }
49
50
    public function save($data) {
51
        if (count($this->id) > 1) return $this->saveMultiPk($data);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->saveMultiPk($data) targeting Maphper\DataSource\Mock::saveMultiPk() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
        else return $this->saveSinglePk($data);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->saveSinglePk($data) targeting Maphper\DataSource\Mock::saveSinglePk() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
    }
54
55
    private function saveSinglePk($data) {
56
        if (isset($data->{$this->id[0]})) {
57
            $id = $data->{$this->id[0]};
58
        }
59
        else {
60
            $id = count($this->data);
61
            $data->{$this->id[0]} = $id;
62
        }
63
64
        $this->data[$id] = (object)array_merge($this->findById($id), (array)$data);
65
    }
66
67
    private function saveMultiPk($data) {
68
        $saveId = $this->getMultiPkSaveId($data);
69
70
        $this->data[$saveId] = (object)array_merge($this->findById($saveId), (array)$data);
71
    }
72
73
    private function getMultiPkSaveId($data) {
74
        $keyVals = [];
75
        foreach ($this->id as $keyName) {
76
            $keyVals[] = $data->$keyName;
77
        }
78
        return implode(',', $keyVals);
79
    }
80
81
    public function getErrors() {
82
        return [];
83
    }
84
85
    private function getOrderFunction($order, $columns) {
86
        return function($a, $b) use ($order, $columns) {
87
          foreach (explode(',', $columns) as $column) {
88
            $aColumn = $a->$column;
89
            $bColumn = $b->$column;
90
            if ($aColumn === $bColumn) {
91
              $sortVal = 0;
92
              continue;
93
            }
94
            else $sortVal = ($aColumn < $bColumn) ? -1 : 1;
95
            break;
96
          }
97
          if ($order === 'desc') return -$sortVal;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sortVal seems to be defined by a foreach iteration on line 87. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
98
          else return $sortVal;
99
        };
100
    }
101
}
102