ProductController::showProductById()   A
last analyzed

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
rs 10
ccs 0
cts 3
cp 0
crap 2
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
23
    #[Route('/product/create', name: 'product_create')]
24
    public function createProduct(
25
        ManagerRegistry $doctrine
26
    ): Response {
27
        $entityManager = $doctrine->getManager();
28
    
29
        $product = new Product();
30
        $product->setName('Keyboard_num_' . rand(1, 9));
31
        $product->setValue(rand(100, 999));
32
    
33
        // tell Doctrine you want to (eventually) save the Product
34
        // (no queries yet)
35
        $entityManager->persist($product);
36
    
37
        // actually executes the queries (i.e. the INSERT query)
38
        $entityManager->flush();
39
    
40
        return new Response('Saved new product with id '.$product->getId());
41
    }
42
43
    #[Route('/product/show', name: 'product_show_all')]
44
    public function showAllProduct(
45
        ProductRepository $productRepository
46
    ): Response {
47
        $products = $productRepository->findAll();
48
    
49
        //return $this->json($products);
50
        $response = $this->json($products);
51
        $response->setEncodingOptions(
52
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
53
        );
54
        return $response;
55
    }
56
57
    #[Route('/product/show/{id}', name: 'product_by_id')]
58
    public function showProductById(
59
        ProductRepository $productRepository,
60
        int $id
61
    ): Response {
62
        $product = $productRepository->find($id);
63
64
        return $this->json($product);
65
    }
66
67
    #[Route('/product/delete/{id}', name: 'product_delete_by_id')]
68
    public function deleteProductById(
69
        ManagerRegistry $doctrine,
70
        int $id
71
    ): Response {
72
        $entityManager = $doctrine->getManager();
73
        $product = $entityManager->getRepository(Product::class)->find($id);
74
75
        if (!$product) {
76
            throw $this->createNotFoundException(
77
                'No product found for id '.$id
78
            );
79
        }
80
81
        $entityManager->remove($product);
82
        $entityManager->flush();
83
84
        return $this->redirectToRoute('product_show_all');
85
    }
86
87
    #[Route('/product/update/{id}/{value}', name: 'product_update')]
88
    public function updateProduct(
89
        ManagerRegistry $doctrine,
90
        int $id,
91
        int $value
92
    ): Response {
93
        $entityManager = $doctrine->getManager();
94
        $product = $entityManager->getRepository(Product::class)->find($id);
95
    
96
        if (!$product) {
97
            throw $this->createNotFoundException(
98
                'No product found for id '.$id
99
            );
100
        }
101
    
102
        $product->setValue($value);
103
        $entityManager->flush();
104
    
105
        return $this->redirectToRoute('product_show_all');
106
    }
107
108
    #[Route('/product/view', name: 'product_view_all')]
109
    public function viewAllProduct(
110
        ProductRepository $productRepository
111
    ): Response {
112
        $products = $productRepository->findAll();
113
    
114
        $data = [
115
            'products' => $products
116
        ];
117
    
118
        return $this->render('product/view.html.twig', $data);
119
    }
120
121
    #[Route('/product/view/{value}', name: 'product_view_minimum_value')]
122
    public function viewProductWithMinimumValue(
123
        ProductRepository $productRepository,
124
        int $value
125
    ): Response {
126
        $products = $productRepository->findByMinimumValue($value);
127
    
128
        $data = [
129
            'products' => $products
130
        ];
131
    
132
        return $this->render('product/view.html.twig', $data);
133
    }
134
135
    #[Route('/product/show/min/{value}', name: 'product_by_min_value')]
136
    public function showProductByMinimumValue(
137
        ProductRepository $productRepository,
138
        int $value
139
    ): Response {
140
        $products = $productRepository->findByMinimumValue2($value);
141
142
        return $this->json($products);
143
    }
144
145
}
146