Builder::compileQuery()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 12
ccs 6
cts 8
cp 0.75
rs 10
c 1
b 0
f 0
cc 4
nc 6
nop 0
crap 4.25
1
<?php
2
3
namespace PhpWinTools\WmiScripting\Query;
4
5
use PhpWinTools\WmiScripting\Models\Win32Model;
6
use PhpWinTools\WmiScripting\Connections\ComConnection;
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 $from;
15
16
    protected $services;
17
18
    protected $selects = ['*'];
19
20
    protected $wheres = [];
21
22
    protected $relationships = [];
23
24 2
    public function __construct(Win32Model $model, ComConnection $connection)
25
    {
26 2
        $this->model = $model;
27 2
        $this->from = $model->getWmiClassNameAttribute();
28 2
        $this->services = $connection->connect();
29 2
    }
30
31
    public function all()
32
    {
33
        return $this->execQuery("select * from {$this->from}")->getSet();
34
    }
35
36
    /**
37
     * @return ModelCollection|Win32Model[]
38
     */
39 2
    public function get()
40
    {
41 2
        return $this->getModelCollection();
42
    }
43
44
    /**
45
     * @return ModelCollection|Win32Model[]
46
     */
47 2
    public function getModelCollection()
48
    {
49 2
        return $this->getObjectSet()->getSet();
50
    }
51
52
    /**
53
     * @return ObjectSet
54
     */
55 2
    public function getObjectSet(): ObjectSet
56
    {
57 2
        return $this->execQuery($this->queryString());
58
    }
59
60
    /**
61
     * @param string $query
62
     *
63
     * @return ObjectSet
64
     */
65 2
    public function execQuery($query): ObjectSet
66
    {
67 2
        return $this->services
68 2
            ->resolvePropertySets($this->relationships)
69 2
            ->execQuery($query)
70 2
            ->instantiateModels($this->model);
71
    }
72
73
    /**
74
     * @return string
75
     */
76 2
    public function queryString(): string
77
    {
78 2
        return $this->compileQuery();
79
    }
80
81
    public function select(array $columns = ['*'])
82
    {
83
        $this->selects = $columns;
84
85
        return $this;
86
    }
87
88
    public function where($column, $operator, $value)
89
    {
90
        $this->wheres[] = [
91
            'column'    => $column,
92
            'operator'  => $operator,
93
            'value'     => $value,
94
        ];
95
96
        return $this;
97
    }
98
99
    public function with($relationship)
100
    {
101
        if (is_array($relationship)) {
102
            $this->relationships = array_merge($this->relationships, $relationship);
103
104
            return $this;
105
        }
106
107
        $this->relationships[] = $relationship;
108
109
        return $this;
110
    }
111
112 2
    protected function compileQuery()
113
    {
114 2
        $select = implode(', ', $this->selects);
115 2
        $query = "select {$select} from {$this->from}";
116 2
        $wheres = [];
117
118 2
        foreach ($this->wheres as $where) {
119
            $wheres[] = "{$where['column']} {$where['operator']} "
120
                . (is_string($where['value']) ? "'{$where['value']}'" : $where['value']);
121
        }
122
123 2
        return empty($wheres) ? $query : $query . ' where ' . implode(' and ', $wheres);
124
    }
125
}
126