ProductController::updateProduct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 19
rs 9.9666
ccs 0
cts 10
cp 0
crap 6
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Product;
6
use App\Repository\ProductRepository;
7
use Doctrine\Persistence\ManagerRegistry;
8
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Attribute\Route;
12
13
class ProductController extends AbstractController
14
{
15
    #[Route('/product', name: 'app_product')]
16
    public function index(): Response
17
    {
18
        return $this->render('product/index.html.twig', [
19
            'controller_name' => 'ProductController',
20
        ]);
21
    }
22
    #[Route('/product/create', name: 'product_create')]
23
    public function createProduct(
24
        ManagerRegistry $doctrine
25
    ): Response {
26
        $entityManager = $doctrine->getManager();
27
28
        $product = new Product();
29
        $product->setName('Keyboard_num_' . rand(1, 9));
30
        $product->setValue(rand(100, 999));
31
32
        // tell Doctrine you want to (eventually) save the Product
33
        // (no queries yet)
34
        $entityManager->persist($product);
35
36
        // actually executes the queries (i.e. the INSERT query)
37
        $entityManager->flush();
38
39
        return new Response('Saved new product with id '.$product->getId());
40
    }
41
    #[Route('/product/show', name: 'product_show_all')]
42
    public function showAllProduct(
43
        ProductRepository $productRepository
44
    ): Response {
45
        $products = $productRepository
46
            ->findAll();
47
48
        // return $this->json($products);
49
        $response = $this->json($products);
50
        $response->setEncodingOptions(
51
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
52
        );
53
        return $response;
54
    }
55
    #[Route('/product/show/{id}', name: 'product_by_id')]
56
    public function showProductById(
57
        ProductRepository $productRepository,
58
        int $id
59
    ): Response {
60
        $product = $productRepository
61
            ->find($id);
62
63
        return $this->json($product);
64
    }
65
    #[Route('/product/delete/{id}', name: 'product_delete_by_id')]
66
    public function deleteProductById(
67
        ManagerRegistry $doctrine,
68
        int $id
69
    ): Response {
70
        $entityManager = $doctrine->getManager();
71
        $product = $entityManager->getRepository(Product::class)->find($id);
72
73
        if (!$product) {
74
            throw $this->createNotFoundException(
75
                'No product found for id '.$id
76
            );
77
        }
78
79
        $entityManager->remove($product);
80
        $entityManager->flush();
81
82
        return $this->redirectToRoute('product_show_all');
83
    }
84
    #[Route('/product/update/{id}/{value}', name: 'product_update')]
85
    public function updateProduct(
86
        ManagerRegistry $doctrine,
87
        int $id,
88
        int $value
89
    ): Response {
90
        $entityManager = $doctrine->getManager();
91
        $product = $entityManager->getRepository(Product::class)->find($id);
92
93
        if (!$product) {
94
            throw $this->createNotFoundException(
95
                'No product found for id '.$id
96
            );
97
        }
98
99
        $product->setValue($value);
100
        $entityManager->flush();
101
102
        return $this->redirectToRoute('product_show_all');
103
    }
104
    #[Route('/product/view', name: 'product_view_all')]
105
    public function viewAllProduct(
106
        ProductRepository $productRepository
107
    ): Response {
108
        $products = $productRepository->findAll();
109
110
        $data = [
111
            'products' => $products
112
        ];
113
114
        return $this->render('product/view.html.twig', $data);
115
    }
116
    #[Route('/product/view/{value}', name: 'product_view_minimum_value')]
117
    public function viewProductWithMinimumValue(
118
        ProductRepository $productRepository,
119
        int $value
120
    ): Response {
121
        $products = $productRepository->findByMinimumValue($value);
122
123
        $data = [
124
            'products' => $products
125
        ];
126
127
        return $this->render('product/view.html.twig', $data);
128
    }
129
    #[Route('/product/show/min/{value}', name: 'product_by_min_value')]
130
    public function showProductByMinimumValue(
131
        ProductRepository $productRepository,
132
        int $value
133
    ): Response {
134
        $products = $productRepository->findByMinimumValue2($value);
135
136
        return $this->json($products);
137
    }
138
}
139