Passed
Push — drivers ( 453ead...6c2ab9 )
by Joe
01:44
created

Builder::hydrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PhpWinTools\WmiScripting\Query;
4
5
use PhpWinTools\WmiScripting\Models\Win32Model;
6
use PhpWinTools\WmiScripting\Connections\Connection;
7
use PhpWinTools\WmiScripting\Collections\ModelCollection;
8
use PhpWinTools\WmiScripting\Support\ApiObjects\Contracts\ObjectSet;
9
10
class Builder
11
{
12
    protected $model;
13
14
    protected $services;
15
16
    protected $connection;
17
18
    protected $from;
19
20
    protected $selects = ['*'];
21
22
    protected $wheres = [];
23
24
    protected $relationships = [];
25
26
    public function __construct(Win32Model $model, Connection $connection)
27
    {
28
        $this->model = $model;
29
        $this->connection = $connection;
30
        $this->from = $model->getWmiClassNameAttribute();
31
        $this->services = $connection->connect();
0 ignored issues
show
Bug introduced by
The method connect() does not exist on PhpWinTools\WmiScripting\Connections\Connection. Since it exists in all sub-types, consider adding an abstract or default implementation to PhpWinTools\WmiScripting\Connections\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        /** @scrutinizer ignore-call */ 
32
        $this->services = $connection->connect();
Loading history...
32
    }
33
34
    public function all()
35
    {
36
        return $this->hydrate($this->query("select * from {$this->from}"));
0 ignored issues
show
Bug introduced by
It seems like $this->query('select * from '.$this->from) can also be of type PhpWinTools\WmiScripting...cts\Contracts\ObjectSet; however, parameter $items of PhpWinTools\WmiScripting\Query\Builder::hydrate() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        return $this->hydrate(/** @scrutinizer ignore-type */ $this->query("select * from {$this->from}"));
Loading history...
37
    }
38
39
    public function hydrate(array $items = [])
40
    {
41
        return new ModelCollection(array_map(function ($item) {
42
            return $this->model::newInstance($item);
43
        }, $items));
44
    }
45
46
    /**
47
     * @return ModelCollection|Win32Model[]
48
     */
49
    public function get()
50
    {
51
        return $this->hydrate($this->query($this->queryString()));
0 ignored issues
show
Bug introduced by
It seems like $this->query($this->queryString()) can also be of type PhpWinTools\WmiScripting...cts\Contracts\ObjectSet; however, parameter $items of PhpWinTools\WmiScripting\Query\Builder::hydrate() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        return $this->hydrate(/** @scrutinizer ignore-type */ $this->query($this->queryString()));
Loading history...
52
    }
53
54
    /**
55
     * @param $query
56
     *
57
     * @return mixed|ObjectSet
58
     */
59
    public function query($query)
60
    {
61
        return $this->connection->query($query, $this->model, $this->relationships);
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function queryString(): string
68
    {
69
        return $this->compileQuery();
70
    }
71
72
    public function select(array $columns = ['*'])
73
    {
74
        $this->selects = $columns;
75
76
        return $this;
77
    }
78
79
    public function where($column, $operator, $value)
80
    {
81
        $this->wheres[] = [
82
            'column'    => $column,
83
            'operator'  => $operator,
84
            'value'     => $value,
85
        ];
86
87
        return $this;
88
    }
89
90
    public function with($relationship)
91
    {
92
        if (is_array($relationship)) {
93
            $this->relationships = array_merge($this->relationships, $relationship);
94
95
            return $this;
96
        }
97
98
        $this->relationships[] = $relationship;
99
100
        return $this;
101
    }
102
103
    protected function compileQuery()
104
    {
105
        $select = implode(', ', $this->selects);
106
        $query = "select {$select} from {$this->from}";
107
        $wheres = [];
108
109
        foreach ($this->wheres as $where) {
110
            $wheres[] = "{$where['column']} {$where['operator']} "
111
                . (is_string($where['value']) ? "'{$where['value']}'" : $where['value']);
112
        }
113
114
        return empty($wheres) ? $query : $query . ' where ' . implode(' and ', $wheres);
115
    }
116
}
117