Passed
Push — master ( 75dc22...ad651f )
by Luiz Kim
09:59 queued 07:29
created

PrintService::addLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 5
c 5
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Device;
6
use ControleOnline\Entity\Spool;
7
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...
8
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...
9
as Security;
10
11
class PrintService
12
{
13
    private $initialSpace = 8;
14
    private $totalChars = 48;
15
    private $text = '';
16
17
    public function __construct(
18
        private EntityManagerInterface $entityManager,
19
        private FileService $fileService,
20
        private StatusService $statusService,
21
        private Security $security
22
    ) {}
23
24
    public function addLine($prefix = '', $suffix = '', $delimiter = ' ')
25
    {
26
        $initialSpace = str_repeat(" ", $this->initialSpace);
27
        $count =   $this->totalChars - $this->initialSpace - strlen($prefix) - strlen($suffix);
28
        if ($count > 0)
29
            $delimiter = str_repeat($delimiter, $count);
30
        $this->text .= $initialSpace . $prefix . $delimiter . $suffix . "\n";
31
    }
32
33
    public function generatePrintData(Device $device): Spool
34
    {
35
        $content =  [
36
            "operation" => "PRINT_TEXT",
37
            "styles" => [[]],
38
            "value" => [$this->text]
39
        ];
40
41
        return  $this->addToSpool($device, $content);
42
    }
43
44
    public function addToSpool(Device $device, $content): Spool
45
    {
46
        $user = $this->security->getToken()->getUser();
47
        $status = $this->statusService->discoveryStatus('open', 'open', 'print');
48
        $file = $this->fileService->addFile($user->getPeople(), $content, 'print', 'print', 'text', 'txt');
49
50
        $spool = new Spool();
51
        $spool->setDevice($device);
52
        $spool->setStatus($status);
53
        $spool->setFile($file);
54
55
        $this->entityManager->persist($spool);
56
        $this->entityManager->flush();
57
58
        return $spool;
59
    }
60
}
61