Passed
Branch main (02626f)
by Vedrana
05:25
created

ProductController::showProductByMinimumValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 3
cp 0
crap 2
rs 10
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
    #[Route('/product', name: 'app_product')]
22
    public function index(): Response
23
    {
24
        return $this->render('product/index.html.twig', [
25
            'controller_name' => 'ProductController',
26
        ]);
27
    }
28
29
    /**
30
     * Creates a new product.
31
     */
32
    #[Route('/product/create', name: 'product_create')]
33
    public function createProduct(
34
        EntityManagerInterface $entityManager
35
    ): Response {
36
        $product = new Product();
37
        $product->setName('Keyboard_num_' . rand(1, 9));
38
        $product->setValue(rand(100, 999));
39
40
        $entityManager->persist($product);
41
        $entityManager->flush();
42
43
        return new Response('Saved new product with id '.$product->getId());
44
    }
45
46
    /**
47
     * Show all products as JSON response.
48
     */
49
    #[Route('/product/show', name: 'product_show_all')]
50
    public function showAllProduct(
51
        ProductRepository $productRepository
52
    ): Response {
53
        $products = $productRepository
54
            ->findAll();
55
56
        $response = $this->json($products);
57
        $response->setEncodingOptions(
58
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
59
        );
60
        return $response;
61
    }
62
63
    /**
64
     * Show one product by ID as JSON.
65
     * Throws 404 if not found.
66
     */
67
    #[Route('/product/show/{id}', name: 'product_by_id')]
68
    public function showProductById(
69
        ProductRepository $productRepository,
70
        int $id
71
    ): Response {
72
        $product = $productRepository
73
            ->find($id);
74
75
        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
    #[Route('/product/view', name: 'product_view_all')]
133
    public function viewAllProduct(
134
        ProductRepository $productRepository
135
    ): Response {
136
        $products = $productRepository->findAll();
137
138
        $data = ['products' => $products];
139
140
        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