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

SWbemObjectSet::get()   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 0
1
<?php
2
3
namespace PhpWinTools\WmiScripting\Support\ApiObjects;
4
5
use Countable;
6
use ArrayAccess;
7
use PhpWinTools\Support\COM\VariantWrapper;
8
use PhpWinTools\Support\COM\ComVariantWrapper;
9
use PhpWinTools\WmiScripting\Connections\Response;
10
use PhpWinTools\WmiScripting\Models\Win32Model;
11
use function PhpWinTools\WmiScripting\Support\resolve;
12
use PhpWinTools\WmiScripting\Collections\ModelCollection;
13
use PhpWinTools\WmiScripting\Collections\ObjectSetCollection;
14
use PhpWinTools\WmiScripting\Support\ApiObjects\Contracts\ObjectSet;
15
use PhpWinTools\WmiScripting\Support\ApiObjects\Contracts\ObjectItem;
16
use PhpWinTools\WmiScripting\Support\ApiObjects\VariantInterfaces\ObjectVariant;
17
use PhpWinTools\WmiScripting\Support\ApiObjects\VariantInterfaces\ObjectExVariant;
18
use PhpWinTools\WmiScripting\Support\ApiObjects\VariantInterfaces\ObjectSetVariant;
19
20
/**
21
 * @link https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobjectset
22
 */
23
class SWbemObjectSet extends AbstractWbemObject implements ArrayAccess, Countable, ObjectSet
24
{
25
    protected $count = 0;
26
27
    /** @var ObjectSetCollection|array|SWbemObject[] */
28
    protected $set = [];
29
30
    protected $resolve_property_sets = [];
31
32
    /** @var VariantWrapper|ObjectSetVariant|ObjectVariant[]|ObjectExVariant[] */
33
    protected $object;
34
35
    public function __construct(VariantWrapper $variant, array $resolve_property_sets = [])
36
    {
37
        parent::__construct($variant);
38
39
        $this->count = (int) $this->object->Count;
0 ignored issues
show
Bug Best Practice introduced by
The property Count does not exist on PhpWinTools\Support\COM\VariantWrapper. Since you implemented __get, consider adding a @property annotation.
Loading history...
40
        $this->resolve_property_sets = $resolve_property_sets;
41
        $this->set = new ObjectSetCollection();
42
        $this->buildSet();
43
    }
44
45
    /**
46
     * @return int
47
     */
48
    public function count(): int
49
    {
50
        return $this->count;
51
    }
52
53
    public function get()
54
    {
55
        return $this->set->map(function (ObjectItem $item) {
56
            return $item->get();
57
        })->toArray();
58
    }
59
60
    /**
61
     * @return Win32Model[]|ModelCollection
62
     */
63
    public function getSet(): ModelCollection
64
    {
65
        return new ModelCollection($this->set->map->getModel());
66
    }
67
68
    public function instantiateModels(Win32Model $model): ObjectSet
69
    {
70
        $this->set = $this->set->map(function (ObjectItem $item) use ($model) {
71
            $item->instantiateWin32Model($model);
72
            return $item;
73
        });
74
75
        return $this;
76
    }
77
78
    public function offsetExists($offset)
79
    {
80
        return $this->set->offsetExists($offset);
81
    }
82
83
    /**
84
     * @param mixed $offset
85
     *
86
     * @return SWbemObject
87
     */
88
    public function offsetGet($offset)
89
    {
90
        return $this->set->offsetGet($offset);
91
    }
92
93
    public function offsetSet($offset, $value)
94
    {
95
        return $this->set->offsetSet($offset, $value);
96
    }
97
98
    public function offsetUnset($offset)
99
    {
100
        return $this->set->offsetUnset($offset);
101
    }
102
103
    protected function buildSet()
104
    {
105
        $is_object_ex = null;
106
107
        foreach ($this->object as $item) {
108
            $wbemObject = null;
109
            if (is_null($is_object_ex)) {
110
                $is_object_ex = $this->isWbemObjectEx($item);
111
            }
112
113
            if ($is_object_ex) {
114
                $wbemObject = resolve()->objectItemEx($item, $this->resolve_property_sets);
115
            }
116
117
            if (is_null($wbemObject)) {
118
                $wbemObject = resolve()->objectItem($item, $this->resolve_property_sets);
119
            }
120
121
            $this->set->add($wbemObject);
122
        }
123
    }
124
125
    protected function isWbemObjectEx(ComVariantWrapper $variant)
126
    {
127
        return $variant::getComClassName($variant) === 'ISWbemObjectEx';
128
    }
129
}
130