Completed
Push — master ( 1f5fd8...85994b )
by Andrii
03:30
created

BaseRepository   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 4
dl 0
loc 131
ccs 0
cts 92
cp 0
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 6 1
A setRecordClass() 0 4 1
A getRecordClass() 0 8 2
A findRecordClass() 0 6 1
A findAll() 0 8 1
A findOne() 0 8 1
A buildSelectQuery() 0 4 1
A buildQuery() 0 4 1
A getQueryClass() 0 4 1
A createMultiple() 0 9 2
A create() 0 4 1
A createDto() 0 13 3
A getEntityCreationDtoClass() 0 8 1
A createEntity() 0 4 1
A splitDbRawData() 0 12 3
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
    /**
19
     * @var ConnectionInterface
20
     */
21
    protected $db;
22
23
    protected $factory;
24
25
    /**
26
     * @var string
27
     */
28
    public $queryClass;
29
30
    public function find(ActiveQuery $query)
31
    {
32
        $query->setRepository($this);
33
34
        return $query;
35
    }
36
37
    public function setRecordClass($value)
38
    {
39
        $this->recordClass = $value;
0 ignored issues
show
Documentation introduced by
The property recordClass does not exist on object<hiapi\repositories\BaseRepository>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
40
    }
41
42
    public function getRecordClass()
43
    {
44
        if ($this->recordClass === null) {
0 ignored issues
show
Documentation introduced by
The property recordClass does not exist on object<hiapi\repositories\BaseRepository>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
            $this->recordClass = $this->findRecordClass();
0 ignored issues
show
Documentation introduced by
The property recordClass does not exist on object<hiapi\repositories\BaseRepository>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
46
        }
47
48
        return $this->recordClass;
0 ignored issues
show
Documentation introduced by
The property recordClass does not exist on object<hiapi\repositories\BaseRepository>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
    }
50
51
    public function findRecordClass()
52
    {
53
        $parts = explode('\\', get_called_class());
54
55
        return implode('\\', $parts);
56
    }
57
58
    public function findAll(Specification $specification)
59
    {
60
        $query = $this->buildSelectQuery($specification);
61
        $rows = $query->createCommand($this->db)->queryAll();
62
        /// 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...
63
64
        return $this->createMultiple($rows);
65
    }
66
67
    public function findOne(Specification $specification)
68
    {
69
        $specification->limit(1);
70
        $query = $this->buildSelectQuery($specification);
71
        $row = $query->createCommand($this->db)->queryOne();
72
73
        return $this->create($row);
74
    }
75
76
    protected function buildSelectQuery(Specification $specification)
77
    {
78
        return $this->buildQuery()->initSelect()->apply($specification);
79
    }
80
81
    protected function buildQuery()
82
    {
83
        return Yii::createObject($this->getQueryClass());
84
    }
85
86
    protected function getQueryClass()
87
    {
88
        return $this->queryClass;
89
    }
90
91
    protected function createMultiple($rows)
92
    {
93
        $entities = [];
94
        foreach ($rows as $row) {
95
            $entities[] = $this->create($row);
96
        }
97
98
        return $entities;
99
    }
100
101
    protected function create(array $row)
102
    {
103
        return $this->factory->create($this->createDto($row));
104
    }
105
106
    protected function createDto(array $row)
107
    {
108
        $class = $this->getEntityCreationDtoClass();
109
        $dto = new $class();
110
        $props = array_keys(get_object_vars($dto));
111
        foreach ($props as $name) {
112
            if (isset($row[$name])) {
113
                $dto->{$name} = $row[$name];
114
            }
115
        }
116
117
        return $dto;
118
    }
119
120
    protected function getEntityCreationDtoClass()
121
    {
122
        $class = new \ReflectionClass($this->factory);
123
        $method = $class->getMethod('create');
124
        $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...
125
126
        return $arg->getClass()->getName();
127
    }
128
129
    public function createEntity($entityClass, $row)
130
    {
131
        return Yii::$app->entityManager->getRepository($entityClass)->create($row);
132
    }
133
134
    protected function splitDbRawData(array $row)
135
    {
136
        foreach ($row as $key => $value) {
137
            $parts = explode('-', $key, 2);
138
            if (count($parts)>1) {
139
                $row[$parts[0]][$parts[1]] = $value;
140
                unset($row[$key]);
141
            }
142
        }
143
144
        return $row;
145
    }
146
}
147