Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

Result::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by IntelliJ IDEA.
4
 * User: inji
5
 * Date: 07.01.2018
6
 * Time: 16:39
7
 */
8
9
namespace Inji\Db\InjiStorage;
10
11
12
use Inji\Db\DriverResult;
13
use Inji\Model;
14
15
class Result implements DriverResult {
16
    public $result = [];
17
    public $curKey = 0;
18
    /**
19
     * @var Query
20
     */
21
    public $query;
22
23
    public function __construct($dbResult, $query) {
24
        $this->result = $dbResult;
25
        $this->query = $query;
26
        reset($dbResult);
27
        $this->curKey = key($dbResult);
28
    }
29
30
    public function fetch(string $className = '') {
31
        if (!$this->result) {
32
            return false;
33
        }
34
        $item = $this->result[$this->curKey];
35
        next($this->result);
36
        $this->curKey = key($this->result);
37 View Code Duplication
        if ($className) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
            /**
39
             * @var Model
40
             */
41
            $item = new $className($item);
42
            $item->connectionName = $this->query->connection->dbInstance;
43
            $item->dbOptions = $this->query->dbOptions;
44
            $item->app = $this->query->connection->dbInstance->app;
45
            return $item;
46
        }
47
        return $item;
48
    }
49
50
    public function fetchAll(string $className = '', $keyCol = '') {
51
        if (!$this->result) {
52
            return [];
53
        }
54
        $items = [];
55
        foreach ($this->result as $item) {
56 View Code Duplication
            if ($className) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
                /**
58
                 * @var Model
59
                 */
60
                $item = new $className($item);
61
                $item->connectionName = $this->query->connection->dbInstance;
62
                $item->dbOptions = $this->query->dbOptions;
63
                $item->app = $this->query->connection->dbInstance->app;
64
            }
65
            if ($keyCol) {
66
                $key = $className ? $item->$keyCol : $item[$keyCol];
67
                $items[$key] = $item;
68
            } else {
69
                $items[] = $item;
70
            }
71
        }
72
        return $items;
73
    }
74
}