Passed
Push — master ( 1e9707...c186f6 )
by Luiz Kim
02:27
created

DeviceService::getPrinters()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Device;
6
use ControleOnline\Entity\DeviceConfig;
7
use ControleOnline\Entity\People;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\People was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class DeviceService
11
{
12
    public function __construct(
13
        private EntityManagerInterface $manager,
14
        private ConfigService $configService,
15
    ) {}
16
17
    public function getPrinters(People $people)
18
    {
19
        $device_configs = $this->manager->getRepository(DeviceConfig::class)->findBy([
20
            'people' => $people
21
        ]);
22
        $devices = [];
23
        foreach ($device_configs as $device_config) {
24
            $configs = $device_config->getConfigs(true);
25
            if (isset($configs['pos-gateway']) && $configs['pos-gateway'] == 'cielo')
26
                $devices[] = $device_config->getDevice();
27
        }
28
        return $devices;
29
    }
30
31
    public function discoveryDevice($deviceId)
32
    {
33
        $device = $this->manager->getRepository(Device::class)->findOneBy([
34
            'device' => $deviceId
35
        ]);
36
        if (!$device) {
37
            $device = new Device();
38
            $device->setDevice($deviceId);
39
            $this->manager->persist($device);
40
            $this->manager->flush();
41
        }
42
        return $device;
43
    }
44
45
46
    public function discoveryDeviceConfig(Device $device, People $people): DeviceConfig
47
    {
48
        $device_config = $this->manager->getRepository(DeviceConfig::class)->findOneBy([
49
            'device' => $device,
50
            'people' => $people
51
        ]);
52
        if (!$device_config) {
53
            $device_config = new DeviceConfig();
54
            $device_config->setDevice($device);
55
            $device_config->setPeople($people);
56
            $this->manager->persist($device_config);
57
        }
58
59
        return $device_config;
60
    }
61
62
    public function addDeviceConfigs(People $people, array $configs, $deviceId)
63
    {
64
        $device = $this->discoveryDevice($deviceId);
65
66
        $device_config = $this->discoveryDeviceConfig($device,  $people);
67
        foreach ($configs as $key => $config)
68
            $device_config->addConfig($key,  $config);
69
70
        $this->manager->persist($device_config);
71
        $this->manager->flush();
72
73
        return $device_config;
74
    }
75
}
76