Passed
Push — master ( 2395d5...b7dec9 )
by Luiz Kim
02:21
created

AddProductsOrderAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
c 1
b 0
f 0
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 21 4
1
<?php
2
3
namespace ControleOnline\Controller;
4
5
use App\Library\Rates\Model\Product;
0 ignored issues
show
Bug introduced by
The type App\Library\Rates\Model\Product 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\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...
7
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...
8
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...
9
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...
10
use ControleOnline\Entity\Order;
11
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...
12
use ControleOnline\Service\OrderProductService;
13
use Exception;
14
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...
15
16
class AddProductsOrderAction
17
{
18
19
20
    public function __construct(
21
        private EntityManagerInterface $entityManager,
22
        private HydratorService $hydratorService,
23
        private OrderProductService $orderProductService
24
25
    ) {}
26
27
    public function __invoke(Request $request, int $id): JsonResponse
28
    {
29
        try {
30
            $order = $this->entityManager->getRepository(Order::class)->find($id);
31
            if (!$order)
32
                return new JsonResponse(['error' => 'Order not found'], 404);
33
34
            $data = json_decode($request->getContent(), true);
35
36
            foreach ($data as $p) {
37
                $product = $this->entityManager->getRepository(Product::class)->find($p['product']);
38
                $quantity = $p['quantity'];
39
                $price = $product->getPrice();
40
                $this->orderProductService->addOrderProduct($order, $product, $quantity, $price);
41
            }
42
43
            $this->entityManager->refresh($order);
44
45
            return new JsonResponse($this->hydratorService->item(Order::class, $order->getId(), "order_details:read"), Response::HTTP_OK);
46
        } catch (Exception $e) {
47
            return new JsonResponse($this->hydratorService->error($e));
48
        }
49
    }
50
}
51