Passed
Push — master ( e8a7fb...69cdb7 )
by Luiz Kim
02:36
created

PrintService   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 57
c 1
b 0
f 0
dl 0
loc 98
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getQueues() 0 22 7
B generatePrintData() 0 55 9
A addLine() 0 5 1
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Order;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Order 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...
6
use ControleOnline\Entity\ProductGroupProduct;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\ProductGroupProduct 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 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 Exception;
9
10
class PrintService
11
{
12
    private $noQueue = 'Sem fila definida';
13
    private $initialSpace = 8;
14
    private $totalChars = 30;
15
    private $text = '';
16
17
    public function __construct(
18
        private EntityManagerInterface $entityManager
19
    ) {}
20
21
    private function addLine($pre = '', $pos = '', $delimiter = ' ')
22
    {
23
        $initialSpace = str_repeat(" ", $this->initialSpace);
24
        $delimiter = str_repeat($delimiter, $this->totalChars - $initialSpace - strlen($pre) - strlen($pos));
25
        $this->text .= $initialSpace . $pre . $delimiter . $pos . "\n";
26
    }
27
28
29
    private function getQueues(Order $order)
30
    {
31
        $queues = [];
32
        foreach ($order->getOrderProducts() as $orderProduct) {
33
            $queueEntries = $orderProduct->getOrderProductQueues();
34
            if ($queueEntries->isEmpty()) {
35
                if (!isset($queues[$this->noQueue]))
36
                    $queues[$this->noQueue] = [];
37
                $queues[$this->noQueue][] = $orderProduct;
38
            } else
39
                foreach ($queueEntries as $queueEntry) {
40
                    $queue = $queueEntry->getQueue();
41
                    $queueName = $queue ? $queue->getQueue() : $this->noQueue;
42
43
44
                    if (!isset($queues[$queueName]))
45
                        $queues[$queueName] = [];
46
47
                    $queues[$queueName][] = $orderProduct;
48
                }
49
        }
50
        return $queues;
51
    }
52
53
    public  function generatePrintData(Order $order, string $printType, string $deviceType)
54
    {
55
56
        if ($printType === 'pos') {
57
            $this->addLine("PEDIDO #" . $order->getId());
58
            $this->addLine($order->getOrderDate()->format('d/m/Y H:i'));
59
            $client = $order->getClient();
60
            $this->addLine(($client !== null ? $client->getName() : 'Não informado'));
61
            $this->addLine(number_format($order->getPrice(), 2, ',', '.'));
62
            $this->addLine("", "", "-");
63
64
            $queues = $this->getQueues($order);
65
66
67
            foreach ($queues as $queueName => $products) {
68
                $this->addLine(strtoupper($queueName) . ":");
69
                foreach ($products as $orderProduct) {
70
                    $product = $orderProduct->getProduct();
71
                    $unit = $product->getProductUnit()->getProductUnit();
72
                    $quantity = $orderProduct->getQuantity();
73
74
                    $this->addLine(
75
                        "- " . $product->getProduct() . " (" . $quantity . " " . $unit . ")",
76
                        " R$ " . number_format($product->getPrice() * $quantity, 2, ',', '.'),
77
                        '.'
78
                    );
79
80
81
                    if ($product->getType() === 'custom') {
82
                        $this->addLine("    Personalizações:");
83
                        $productGroupProducts = $this->entityManager->getRepository(ProductGroupProduct::class)
84
                            ->findBy(['product' => $product->getId()]);
85
86
                        foreach ($productGroupProducts as $pgp) {
87
                            $childProduct = $pgp->getProductChild();
88
                            if ($childProduct)
89
                                $this->addLine("    - " . $childProduct->getProduct() . " (" . $pgp->getQuantity() . " " . $childProduct->getProductUnit()->getProductUnit() . ")");
90
                        }
91
                    }
92
                }
93
                $this->addLine('', '', ' ');
94
            }
95
96
            $this->addLine('', '', '-');
97
98
99
            if ($deviceType === 'cielo')
100
                return   [
101
                    "operation" => "PRINT_TEXT",
102
                    "styles" => [[]],
103
                    "value" => [$this->text]
104
                ];
105
        }
106
107
        throw new Exception("Unsupported print type", 1);
108
    }
109
}
110