Completed
Push — master ( b3c129...c717ef )
by Joe
03:25 queued 01:31
created

Win32ModelFactory   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 19
eloc 61
dl 0
loc 109
ccs 50
cts 54
cp 0.9259
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B fillAttributes() 0 25 8
A __construct() 0 3 1
A make() 0 45 2
B cast() 0 17 8
1
<?php
2
3
namespace PhpWinTools\WmiScripting\Testing\Wmi;
4
5
use Faker\Factory;
6
use Faker\Generator;
7
use PhpWinTools\WmiScripting\Connection;
8
use PhpWinTools\WmiScripting\Win32Model;
9
use PhpWinTools\WmiScripting\Collections\ArrayCollection;
10
use PhpWinTools\WmiScripting\Support\ApiObjects\SWbemServices;
11
12
class Win32ModelFactory
13
{
14
    protected $faker;
15
16 2
    public function __construct(Generator $faker = null)
17
    {
18 2
        $this->faker = $faker ?? Factory::create();
19 2
    }
20
21
    /**
22
     * @param string|Win32Model $class_name
23
     * @param int $count
24
     * @param array $attributes
25
     * @param Generator|null $faker
26
     * @return ModelFakeCollection
27
     */
28 2
    public static function make($class_name, int $count = 1, array $attributes = [], Generator $faker = null)
29
    {
30 2
        $factory = new static($faker ?? Factory::create());
31
32 2
        $modelFakes = new ModelFakeCollection();
33
34 2
        while ($count) {
35 2
            $modelFakes->add(new ModelFake([
36 2
                'properties' => $factory->fillAttributes(new ModelProperties($class_name)),
37
                'derivations' => array_filter(class_parents($class_name), function ($parent) {
38 2
                    return $parent !== Win32Model::class;
39 2
                }),
40 2
                'qualifiers' => new ArrayCollection([
41
                    [
42 2
                        'Name' => 'provider',
43
                        'Value' => 'CIMWin32',
44
                    ],
45
                    [
46 2
                        'Name' => 'UUID',
47 2
                        'Value' => $class_name::newInstance()->getAttribute('uuid'),
48
                    ],
49
                ]),
50 2
                'path' => new ArrayCollection([
51 2
                    'authority' => $authority = null,
0 ignored issues
show
Unused Code introduced by
The assignment to $authority is dead and can be removed.
Loading history...
52 2
                    'class' => $class_name::newInstance()->getWmiClassNameAttribute(),
53
                    'display_name' => SWbemServices::WMI_MONIKER
54
                        . '{authenticationLevel=pktPrivacy,impersonationLevel=impersonate}!'
55 2
                        . "\\\server\\namespace:{$class_name::newInstance()->getWmiClassNameAttribute()}"
56 2
                        . ".DeviceID=\"something:\"",
57
                    'is_class' => false,
58
                    'is_singleton' => false,
59
                    'keys' => [],
60
                    'namespace' => Connection::DEFAULT_NAMESPACE,
61 2
                    'parent_namespace' => "Root",
62 2
                    'path' => "\\\server\\namespace:{$class_name::newInstance()->getWmiClassNameAttribute()}"
63 2
                        . ".DeviceID=\"something:\"",
64 2
                    'relative_path' => "some other stuff",
65 2
                    'server' => 'server',
66
                ]),
67
            ]));
68
69 2
            --$count;
70
        }
71
72 2
        return $modelFakes;
73
    }
74
75 2
    protected function fillAttributes(ModelProperties $details)
76
    {
77
        return $details->getProperties()->map(function ($property) {
78 2
            $value = null;
79
80 2
            if (is_array($property['value']) && empty($property['value'])) {
81 2
                $value = (array) $this->faker->words;
82
            }
83
84 2
            if (is_array($property['value']) && !empty($property['value'])) {
85
                $value = $property['value'];
86
            }
87
88 2
            if (is_string($property['value']) && trim($property['value']) !== '') {
89 1
                $value = $property['value'];
90
            }
91
92 2
            if (is_null($value)) {
93 2
                $value = $this->cast($property['cast']);
94
            }
95
96
            return [
97 2
                'Name' => $property['name'],
98 2
                'Value' => $value,
99 2
                'Origin' => $property['origin'],
100
            ];
101 2
        });
102
    }
103
104 2
    protected function cast($cast)
105
    {
106
        switch ($cast) {
107 2
            case 'array':
108
                return (array) $this->faker->words;
109 2
            case 'bool':
110 2
            case 'boolean':
111
                return (bool) $this->faker->boolean;
112 2
            case 'float':
113
                return (float) $this->faker->randomFloat(2, PHP_INT_MIN, PHP_INT_MAX);
114 2
            case 'int':
115 2
            case 'integer':
116
                // Prevent integer overflow
117 2
                return (int) $this->faker->numberBetween(0, PHP_INT_MAX);
118 2
            case 'string':
119
            default:
120 2
                return $this->faker->word;
121
        }
122
    }
123
}
124