Passed
Push — master ( bf46cc...bf9f11 )
by Luiz Kim
02:42
created

DeviceService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 22
c 1
b 0
f 0
dl 0
loc 44
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A discoveryDeviceConfig() 0 13 2
A __construct() 0 3 1
A addDeviceConfigs() 0 10 2
A discoveryDevice() 0 11 2
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
    ) {}
15
16
17
    public function discoveryDevice($deviceId)
18
    {
19
        $device = $this->manager->getRepository(Device::class)->findOneBy([
20
            'device' => $deviceId
21
        ]);
22
        if (! $device) {
23
            $device = new Device();
24
            $device->setDevice($deviceId);
25
        }
26
27
        return $device;
28
    }
29
    public function discoveryDeviceConfig(Device $device, People $people)
30
    {
31
        $device_config = $this->manager->getRepository(DeviceConfig::class)->findOneBy([
32
            'device' => $device,
33
            'people' => $people
34
        ]);
35
        if (! $device_config) {
36
            $device_config = new DeviceConfig();
37
            $device_config->setDevice($device);
38
            $device_config->setPeople($people);
39
        }
40
41
        return $device_config;
42
    }
43
44
    public function addDeviceConfigs(People $people, array $configs, $deviceId)
45
    {
46
        $device = $this->discoveryDevice($deviceId);
47
        $device_config = $this->discoveryDeviceConfig($device,  $people);
48
        foreach ($configs as $key => $config)
49
            $device_config->addConfigs($key,  $config);
50
51
        $this->manager->persist($device_config);
52
        $this->manager->flush();
53
        return $device;
54
    }
55
}
56