Passed
Push — master ( c4ea7e...31f09d )
by Luiz Kim
02:28
created

PrintOrderAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generatePrintData() 0 31 4
A __construct() 0 3 1
A __invoke() 0 18 2
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request 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 Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse 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 ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
0 ignored issues
show
Bug introduced by
The type ApiPlatform\Core\Bridge\...Util\QueryNameGenerator 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
use ControleOnline\Entity\Order;
10
11
class PrintOrderAction
12
{
13
    private $entityManager;
14
15
    public function __construct(EntityManagerInterface $entityManager)
16
    {
17
        $this->entityManager = $entityManager;
18
    }
19
20
    public function __invoke(Request $request, int $id): JsonResponse
21
    {
22
        // Busca o pedido pelo ID
23
        $order = $this->entityManager->getRepository(Order::class)->find($id);
24
        if (!$order) {
25
            return new JsonResponse(['error' => 'Order not found'], 404);
26
        }
27
28
        // Pega os parâmetros da requisição (print-type e device-type)
29
        $data = json_decode($request->getContent(), true);
30
        $printType = $data['print-type'] ?? 'pos';
31
        $deviceType = $data['device-type'] ?? 'cielo';
32
33
        // Lógica para decidir o que retornar (texto ou imagem)
34
        $printData = $this->generatePrintData($order, $printType, $deviceType);
35
36
        // Retorna os dados a serem impressos
37
        return new JsonResponse($printData);
38
    }
39
40
    private function generatePrintData(Order $order, string $printType, string $deviceType): array|string
41
    {
42
        if ($deviceType !== 'cielo') {
43
            return ['error' => 'Unsupported device type'];
44
        }
45
46
        if ($printType === 'pos') {
47
            // Exemplo: retorna um texto simples para impressão no POS
48
            $text = "Order ID: " . $order->getId() . "\n";
49
            $text .= "Client: " . $order->getClient()->getName() . "\n";
50
            $text .= "Price: " . number_format($order->getPrice(), 2, ',', '.') . "\n";
51
            $text .= "Date: " . $order->getOrderDate()->format('d/m/Y H:i:s') . "\n";
52
53
            foreach ($order->getOrderProducts() as $product) {
54
                $text .= "- " . $product->getProduct()->getName() . " x" . $product->getQuantity() . "\n";
55
            }
56
57
            return $text;
58
        }
59
60
        // Caso queira suportar imagem no futuro
61
        // Exemplo fictício de imagem em base64
62
        /*
63
        if ($printType === 'image') {
64
            $imagePath = $this->generateImage($order); // Implementar lógica para gerar imagem
65
            $imageContent = file_get_contents($imagePath);
66
            return base64_encode($imageContent);
67
        }
68
        */
69
70
        return ['error' => 'Unsupported print type'];
71
    }
72
}