ProductController::getProductBySku()   A
last analyzed

Complexity

Conditions 5
Paths 13

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 13
nop 1
dl 0
loc 32
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use ControleOnline\Entity\Device;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Device 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\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...
7
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...
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
0 ignored issues
show
Bug introduced by
The type Symfony\Bundle\Framework...ller\AbstractController 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\HttpFoundation\Response;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Response 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
use Symfony\Component\Routing\Attribute\Route;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Routing\Attribute\Route 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...
11
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...
12
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...
13
use ControleOnline\Entity\Spool;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\Spool 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...
14
use ControleOnline\Service\HydratorService;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\HydratorService 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...
15
use ControleOnline\Service\ProductService;
16
use Exception;
17
use Symfony\Component\Security\Http\Attribute\Security;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Security\Http\Attribute\Security 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...
18
use ControleOnline\Entity\Product;
19
20
class ProductController extends AbstractController
21
{
22
    public function __construct(
23
        private EntityManagerInterface $manager,
24
        private ProductService $productService,
25
        private HydratorService $hydratorService
26
27
    ) {}
28
29
    #[Route('/products/purchasing-suggestion', name: 'purchasing_suggestion', methods: ['GET'])]
30
    #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')")]
31
    public function getPurchasingSuggestion(Request $request): JsonResponse
32
    {
33
        $company =  $this->manager->getRepository(People::class)->find($request->get('company'));
34
        $data = $this->productService->getPurchasingSuggestion($company);
35
        return new JsonResponse($data);
36
    }
37
38
    #[Route('/products/purchasing-suggestion/print', name: 'purchasing_suggestion_print', methods: ['POST'])]
39
    #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')")]
40
    public function printPurchasingSuggestion(Request $request): JsonResponse
41
    {
42
        try {
43
            $data = json_decode($request->getContent(), true);
44
            $device = $this->manager->getRepository(Device::class)->findOneBy([
45
                'device' => $data['device']
46
            ]);
47
            $company = $this->manager->getRepository(People::class)->find($data['people']);
48
            $printData = $this->productService->purchasingSuggestionPrintData($company, $device);
49
50
            return new JsonResponse($this->hydratorService->item(Spool::class, $printData->getId(), "spool_item:read"), Response::HTTP_OK);
51
        } catch (Exception $e) {
52
            return new JsonResponse($this->hydratorService->error($e));
53
        }
54
    }
55
56
    #[Route('/products/inventory', name: 'products_inventory', methods: ['GET'])]
57
    #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')")]
58
    public function getProductsInventory(Request $request): JsonResponse
59
    {
60
        $company =  $this->manager->getRepository(People::class)->find($request->get('company'));
61
        $data = $this->productService->getProductsInventory($company);
62
        return new JsonResponse($data);
63
    }
64
65
    #[Route('/products/inventory/print', name: 'products_inventory_print', methods: ['POST'])]
66
    #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')")]
67
    public function print(Request $request): JsonResponse
68
    {
69
        try {
70
            $data = json_decode($request->getContent(), true);
71
            $device = $this->manager->getRepository(Device::class)->findOneBy([
72
                'device' => $data['device']
73
            ]);
74
75
            $company = $this->manager->getRepository(People::class)->find($data['people']);
76
            $printData = $this->productService->productsInventoryPrintData($company, $device);
77
            return new JsonResponse($this->hydratorService->item(Spool::class, $printData->getId(), "spool_item:read"), Response::HTTP_OK);
78
        } catch (Exception $e) {
79
            return new JsonResponse($this->hydratorService->error($e));
80
        }
81
    }
82
83
    #[Route('/products/sku', name: 'product_by_sku', methods: ['POST'])]
84
    #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_CLIENT')")]
85
    public function getProductBySku(Request $request): JsonResponse
86
    {
87
        try {
88
            $data = json_decode($request->getContent(), true);
89
90
            if (!isset($data['sku'], $data['people'])) {
91
                return new JsonResponse(['error' => 'Parâmetros obrigatórios: sku e people'], Response::HTTP_BAD_REQUEST);
92
            }
93
94
            $sku = (int) ltrim($data['sku'], '0');
95
            $company = $this->manager->getRepository(People::class)->find($data['people']);
96
97
            if (!$company) {
98
                return new JsonResponse(['error' => 'Empresa não encontrada'], Response::HTTP_NOT_FOUND);
99
            }
100
101
            $product = $this->manager
102
                ->getRepository(Product::class)
103
                ->findProductBySkuAsInteger($sku, $company);
104
105
            if (!$product) {
106
                return new JsonResponse(['error' => 'Produto não encontrado'], Response::HTTP_NOT_FOUND);
107
            }
108
109
            return new JsonResponse(
110
                $this->hydratorService->item(Product::class, $product->getId(), 'product:read'),
111
                Response::HTTP_OK
112
            );
113
        } catch (\Exception $e) {
114
            return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
115
        }
116
    }
117
118
    #[Route('/teste', name: 'teste', methods: ['GET'])]
119
    public function teste(): JsonResponse
120
    {
121
        return new JsonResponse(['message' => 'rota funcionando']);
122
    }
123
124
}
125