Passed
Push — master ( b7aac0...b718f8 )
by Luiz Kim
02:33
created

PrintService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 31
Bugs 0 Features 0
Metric Value
eloc 37
c 31
b 0
f 0
dl 0
loc 68
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addLine() 0 7 2
A addToSpool() 0 15 1
A makePrintDone() 0 7 1
A generatePrintData() 0 19 3
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Device;
6
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...
7
use ControleOnline\Entity\Spool;
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
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...e\TokenStorageInterface 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...
10
as Security;
11
12
class PrintService
13
{
14
    private $initialSpace = 8;
15
    private $totalChars = 48;
16
    private $text = '';
17
18
    public function __construct(
19
        private EntityManagerInterface $entityManager,
20
        private FileService $fileService,
21
        private StatusService $statusService,
22
        private DeviceService $deviceService,
23
        private Security $security
24
    ) {}
25
26
    public function addLine($prefix = '', $suffix = '', $delimiter = ' ')
27
    {
28
        $initialSpace = str_repeat(" ", $this->initialSpace);
29
        $count =   $this->totalChars - $this->initialSpace - strlen($prefix) - strlen($suffix);
30
        if ($count > 0)
31
            $delimiter = str_repeat($delimiter, $count);
32
        $this->text .= $initialSpace . $prefix . $delimiter . $suffix . "\n";
33
    }
34
35
    public function makePrintDone(Spool $spool): Spool
36
    {
37
        $status = $this->statusService->discoveryStatus('closed', 'done', 'print');
38
        $spool->setStatus($status);
39
        $this->entityManager->persist($spool);
40
        $this->entityManager->flush();
41
        return  $spool;
42
    }
43
44
    public function generatePrintData(Device $device, People $provider): Spool
45
    {
46
        $printer = null;
47
        $device_config =  $this->deviceService->discoveryDeviceConfig($device, $provider);
48
        if (isset($device_config['printer']))
49
            $printer = $this->deviceService->discoveryDevice($device_config['printer']);
50
51
        $content =  [
52
            "operation" => "PRINT_TEXT",
53
            "styles" => [[]],
54
            "value" => [$this->text]
55
        ];
56
57
        $printData = $this->addToSpool($printer ?? $device, json_encode($content));
58
59
        if ($printer != $device)
60
            $x = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $x is dead and can be removed.
Loading history...
61
62
        return $printData;
63
    }
64
65
    public function addToSpool(Device $printer, string  $content): Spool
66
    {
67
        $user = $this->security->getToken()->getUser();
68
        $status = $this->statusService->discoveryStatus('open', 'open', 'print');
69
        $file = $this->fileService->addFile($user->getPeople(), $content, 'print', 'print', 'text', 'txt');
70
71
        $spool = new Spool();
72
        $spool->setDevice($printer);
73
        $spool->setStatus($status);
74
        $spool->setFile($file);
75
        $spool->setUser($user);
76
        $this->entityManager->persist($spool);
77
        $this->entityManager->flush();
78
79
        return $spool;
80
    }
81
}
82