Win32ModelFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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