SWbemObjectSet::buildSet()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 9.6111
c 0
b 0
f 0
cc 5
nc 9
nop 0
crap 5.0187
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\Models\Win32Model;
10
use function PhpWinTools\WmiScripting\Support\resolve;
11
use PhpWinTools\WmiScripting\Collections\ModelCollection;
12
use PhpWinTools\WmiScripting\Collections\ObjectSetCollection;
13
use PhpWinTools\WmiScripting\Support\ApiObjects\Contracts\ObjectSet;
14
use PhpWinTools\WmiScripting\Support\ApiObjects\Contracts\ObjectItem;
15
use PhpWinTools\WmiScripting\Support\ApiObjects\VariantInterfaces\ObjectVariant;
16
use PhpWinTools\WmiScripting\Support\ApiObjects\VariantInterfaces\ObjectExVariant;
17
use PhpWinTools\WmiScripting\Support\ApiObjects\VariantInterfaces\ObjectSetVariant;
18
19
/**
20
 * @link https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobjectset
21
 */
22
class SWbemObjectSet extends AbstractWbemObject implements ArrayAccess, Countable, ObjectSet
23
{
24
    protected $count = 0;
25
26
    /** @var ObjectSetCollection|array|SWbemObject[] */
27
    protected $set = [];
28
29
    protected $resolve_property_sets = [];
30
31
    /** @var VariantWrapper|ObjectSetVariant|ObjectVariant[]|ObjectExVariant[] */
32
    protected $object;
33
34 2
    public function __construct(VariantWrapper $variant, array $resolve_property_sets = [])
35
    {
36 2
        parent::__construct($variant);
37
38 2
        $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...
39 2
        $this->resolve_property_sets = $resolve_property_sets;
40 2
        $this->set = new ObjectSetCollection();
41 2
        $this->buildSet();
42 2
    }
43
44
    /**
45
     * @return int
46
     */
47
    public function count(): int
48
    {
49
        return $this->count;
50
    }
51
52
    /**
53
     * @return Win32Model[]|ModelCollection
54
     */
55 2
    public function getSet(): ModelCollection
56
    {
57 2
        return new ModelCollection($this->set->map->getModel());
58
    }
59
60 2
    public function instantiateModels(Win32Model $model): ObjectSet
61
    {
62
        $this->set = $this->set->map(function (ObjectItem $item) use ($model) {
63 2
            $item->instantiateWin32Model($model);
64
65 2
            return $item;
66 2
        });
67
68 2
        return $this;
69
    }
70
71
    public function offsetExists($offset)
72
    {
73
        return $this->set->offsetExists($offset);
74
    }
75
76
    /**
77
     * @param mixed $offset
78
     *
79
     * @return SWbemObject
80
     */
81
    public function offsetGet($offset)
82
    {
83
        return $this->set->offsetGet($offset);
84
    }
85
86
    public function offsetSet($offset, $value)
87
    {
88
        return $this->set->offsetSet($offset, $value);
89
    }
90
91
    public function offsetUnset($offset)
92
    {
93
        return $this->set->offsetUnset($offset);
94
    }
95
96 2
    protected function buildSet()
97
    {
98 2
        $is_object_ex = null;
99
100 2
        foreach ($this->object as $item) {
101 2
            $wbemObject = null;
102 2
            if (is_null($is_object_ex)) {
103 2
                $is_object_ex = $this->isWbemObjectEx($item);
104
            }
105
106 2
            if ($is_object_ex) {
107
                $wbemObject = resolve()->objectItemEx($item, $this->resolve_property_sets);
108
            }
109
110 2
            if (is_null($wbemObject)) {
111 2
                $wbemObject = resolve()->objectItem($item, $this->resolve_property_sets);
112
            }
113
114 2
            $this->set->add($wbemObject);
115
        }
116 2
    }
117
118 2
    protected function isWbemObjectEx(ComVariantWrapper $variant)
119
    {
120 2
        return $variant::getComClassName($variant) === 'ISWbemObjectEx';
121
    }
122
}
123