Passed
Push — main ( a57e19...f52e71 )
by Vedrana
28:10
created

ProductController::updateProduct()   A

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
ccs 0
cts 10
cp 0
crap 6
rs 9.9666
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Product;
6
use Doctrine\Persistence\ManagerRegistry;
7
use App\Repository\ProductRepository;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Attribute\Route;
12
13
/**
14
 * Controller Product entities.
15
 */
16
final class ProductController extends AbstractController
17
{
18
    /**
19
    * Renders the product index page.
20
    */
21 1
    #[Route('/product', name: 'app_product')]
22
    public function index(): Response
23
    {
24 1
        return $this->render('product/index.html.twig', [
25 1
            'controller_name' => 'ProductController',
26 1
        ]);
27
    }
28
29
    /**
30
     * Creates a new product.
31
     */
32 1
    #[Route('/product/create', name: 'product_create')]
33
    public function createProduct(
34
        EntityManagerInterface $entityManager
35
    ): Response {
36 1
        $product = new Product();
37 1
        $product->setName('Keyboard_num_' . rand(1, 9));
38 1
        $product->setValue(rand(100, 999));
39
40 1
        $entityManager->persist($product);
41 1
        $entityManager->flush();
42
43 1
        return new Response('Saved new product with id '.$product->getId());
44
    }
45
46
    /**
47
     * Show all products as JSON response.
48
     */
49 1
    #[Route('/product/show', name: 'product_show_all')]
50
    public function showAllProduct(
51
        ProductRepository $productRepository
52
    ): Response {
53 1
        $products = $productRepository
54 1
            ->findAll();
55
56 1
        $response = $this->json($products);
57 1
        $response->setEncodingOptions(
58 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
59 1
        );
60 1
        return $response;
61
    }
62
63
    /**
64
     * Show one product by ID as JSON.
65
     * Throws 404 if not found.
66
     */
67 1
    #[Route('/product/show/{id}', name: 'product_by_id')]
68
    public function showProductById(
69
        ProductRepository $productRepository,
70
        int $id
71
    ): Response {
72 1
        $product = $productRepository
73 1
            ->find($id);
74
75 1
        return $this->json($product);
76
    }
77
78
    /**
79
     * Delete a product by its ID.
80
     * Throws 404 if product not found.
81
     */
82
    #[Route('/product/delete/{id}', name: 'product_delete_by_id')]
83
    public function deleteProductById(
84
        ManagerRegistry $doctrine,
85
        int $id
86
    ): Response {
87
        $entityManager = $doctrine->getManager();
88
        $product = $entityManager->getRepository(Product::class)->find($id);
89
90
        if (!$product) {
91
            throw $this->createNotFoundException(
92
                'No product found for id '.$id
93
            );
94
        }
95
96
        $entityManager->remove($product);
97
        $entityManager->flush();
98
99
        return $this->redirectToRoute('product_show_all');
100
    }
101
102
    /**
103
    * Updates the value of a product by ID.
104
    *
105
    * @param int $id The ID of the product to update.
106
    * @param int $value The new value to assign.
107
    */
108
    #[Route('/product/update/{id}/{value}', name: 'product_update')]
109
    public function updateProduct(
110
        ManagerRegistry $doctrine,
111
        int $id,
112
        int $value
113
    ): Response {
114
        $entityManager = $doctrine->getManager();
115
        $product = $entityManager->getRepository(Product::class)->find($id);
116
117
        if (!$product) {
118
            throw $this->createNotFoundException(
119
                'No product found for id '.$id
120
            );
121
        }
122
123
        $product->setValue($value);
124
        $entityManager->flush();
125
126
        return $this->redirectToRoute('product_show_all');
127
    }
128
129
    /**
130
     * Renders a view listing all products.
131
     */
132 1
    #[Route('/product/view', name: 'product_view_all')]
133
    public function viewAllProduct(
134
        ProductRepository $productRepository
135
    ): Response {
136 1
        $products = $productRepository->findAll();
137
138 1
        $data = ['products' => $products];
139
140 1
        return $this->render('product/view.html.twig', $data);
141
    }
142
143
144
    /**
145
     * Renders products with value greater than or equal to a given minimum.
146
     *
147
     * @param int $value Minimum product value to filter by.
148
     */
149
    #[Route('/product/view/{value}', name: 'product_view_minimum_value')]
150
    public function viewProductWithMinimumValue(
151
        ProductRepository $productRepository,
152
        int $value
153
    ): Response {
154
        $products = $productRepository->findByMinimumValue($value);
155
156
        $data = ['products' => $products];
157
158
        return $this->render('product/view.html.twig', $data);
159
    }
160
161
    /**
162
     * Show products with value >= given minimum as JSON response.
163
     */
164
    #[Route('/product/show/min/{value}', name: 'product_by_min_value')]
165
    public function showProductByMinimumValue(
166
        ProductRepository $productRepository,
167
        int $value
168
    ): Response {
169
        $products = $productRepository->findByMinimumValue2($value);
170
171
        return $this->json($products);
172
    }
173
}
174