Completed
Push — master ( 88ba9b...8a3d51 )
by Andrii
03:03
created

BaseRepository::find()   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
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * HiAPI Yii2 base project for building API
4
 *
5
 * @link      https://github.com/hiqdev/hiapi
6
 * @package   hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiapi\repositories;
12
13
use hiapi\query\Specification;
14
use Yii;
15
16
abstract class BaseRepository extends \yii\base\Component
17
{
18
    protected $db;
19
20
    protected $factory;
21
22
    public $queryClass;
23
24
    public function find(ActiveQuery $query)
25
    {
26
        $query->setRepository($this);
27
28
        return $query;
29
    }
30
31
    protected $_recordClass;
32
33
    public function setRecordClass($value)
34
    {
35
        $this->recordClass = $value;
0 ignored issues
show
Bug introduced by
The property recordClass does not seem to exist. Did you mean _recordClass?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
36
    }
37
38
    public function getRecordClass()
39
    {
40
        if ($this->recordClass === null) {
0 ignored issues
show
Bug introduced by
The property recordClass does not seem to exist. Did you mean _recordClass?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
41
            $this->recordClass = $this->findRecordClass();
0 ignored issues
show
Bug introduced by
The property recordClass does not seem to exist. Did you mean _recordClass?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42
        }
43
44
        return $this->recordClass;
0 ignored issues
show
Bug introduced by
The property recordClass does not seem to exist. Did you mean _recordClass?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
45
    }
46
47
    public function findRecordClass()
48
    {
49
        $parts = explode('\\', get_called_class());
50
51
        return implode('\\', $parts);
52
    }
53
54
    public function findAll(Specification $specification)
55
    {
56
        $query = $this->buildSelectQuery($specification);
57
        $rows = $query->createCommand($this->db)->queryAll();
58
        /// todo $this->addWithes();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
60
        return $this->createMultiple($rows);
61
    }
62
63
    public function findOne(Specification $specification)
64
    {
65
        $specification->limit(1);
66
        $query = $this->buildSelectQuery($specification);
67
        $row = $query->createCommand($this->db)->queryOne();
68
69
        return $this->create($row);
70
    }
71
72
    protected function buildSelectQuery(Specification $specification)
73
    {
74
        return $this->buildQuery()->initSelect()->apply($specification);
75
    }
76
77
    protected function buildQuery()
78
    {
79
        return Yii::createObject($this->getQueryClass());
80
    }
81
82
    protected function getQueryClass()
83
    {
84
        return $this->queryClass;
85
    }
86
87
    protected function createMultiple($rows)
88
    {
89
        $entities = [];
90
        foreach ($rows as $row) {
91
            $entities[] = $this->create($row);
92
        }
93
94
        return $entities;
95
    }
96
97
    protected function create(array $row)
98
    {
99
        return $this->factory->create($this->createDto($row));
100
    }
101
102
    protected function createDto(array $row)
103
    {
104
        $class = $this->getEntityCreationDtoClass();
105
        $dto = new $class();
106
        $props = array_keys(get_object_vars($dto));
107
        foreach ($props as $name) {
108
            if (isset($row[$name])) {
109
                $dto->{$name} = $row[$name];
110
            }
111
        }
112
113
        return $dto;
114
    }
115
116
    protected function getEntityCreationDtoClass()
117
    {
118
        $class = new \ReflectionClass($this->factory);
119
        $method = $class->getMethod('create');
120
        $arg = reset($method->getParameters());
0 ignored issues
show
Bug introduced by
$method->getParameters() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
121
122
        return $arg->getClass()->getName();
123
    }
124
}
125