ProductController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 105
rs 10
ccs 0
cts 44
cp 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A showAll() 0 7 1
A delete() 0 4 1
A createProductPost() 0 28 1
A deletePost() 0 7 1
A show() 0 4 1
A createProduct() 0 4 1
A edit() 0 4 1
A index() 0 4 1
A editPost() 0 24 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Main\Product;
6
use App\Repository\ProductRepository;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\Routing\Annotation\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
    }
20
21
    #[Route('/product/create', name: 'create_product', methods: ['GET'])]
22
    public function createProduct(): Response
23
    {
24
        return $this->render('product/create.html.twig');
25
    }
26
27
    #[Route('/product/create', name: 'create_product_post', methods: ['POST'])]
28
    public function createProductPost(Request $request, EntityManagerInterface $entityManager): Response
29
    {
30
        /** @var string $name */
31
        $name = $request->request->get('name', '');
32
33
        /** @var int $price */
34
        $price = intval($request->request->get('price', 0));
35
36
        /** @var string $description */
37
        $description = $request->request->get('description', '');
38
39
        /** @var string $company */
40
        $company = $request->request->get('company', '');
41
42
        $product = new Product();
43
        $product->setName($name);
44
        $product->setPrice($price);
45
        $product->setDescription($description);
46
        $product->setCompany($company);
47
48
        // tell Doctrine you want to (eventually) save the Product (no queries yet)
49
        $entityManager->persist($product);
50
51
        // actually executes the queries (i.e. the INSERT query)
52
        $entityManager->flush();
53
54
        return $this->redirectToRoute('product_show_all');
55
    }
56
57
    #[Route('/product/show/', name: 'product_show_all')]
58
    public function showAll(ProductRepository $productRepository): Response
59
    {
60
        $products = $productRepository
61
            ->findAll();
62
63
        return $this->render('product/show_all.html.twig', ['products' => $products]);
64
    }
65
66
    #[Route('/product/show/{id}', name: 'product_show')]
67
    public function show(Product $product): Response
68
    {
69
        return $this->render('product/show.html.twig', ['product' => $product]);
70
    }
71
72
    #[Route('/product/edit/{id}', name: 'product_edit', methods: ['GET'])]
73
    public function edit(Product $product): Response
74
    {
75
        return $this->render('product/edit.html.twig', ['product' => $product]);
76
    }
77
78
    #[Route('/product/edit/{id}', name: 'product_edit_post', methods: ['POST'])]
79
    public function editPost(Request $request, EntityManagerInterface $entityManager, Product $product): Response
80
    {
81
82
        /** @var string $name */
83
        $name = $request->request->get('name', $product->getName());
84
85
        /** @var int $price */
86
        $price = intval($request->request->get('price', $product->getPrice()));
87
88
        /** @var string $description */
89
        $description = $request->request->get('description', $product->getDescription());
90
91
        /** @var string $company */
92
        $company = $request->request->get('company', $product->getCompany());
93
94
        $product->setName($name);
95
        $product->setPrice($price);
96
        $product->setDescription($description);
97
        $product->setCompany($company);
98
        $entityManager->flush();
99
100
        return $this->redirectToRoute('product_show', [
101
            'id' => $product->getId()
102
        ]);
103
    }
104
105
    #[Route('/product/delete/{id}', name: 'product_delete', methods: ['GET'])]
106
    public function delete(Product $product): Response
107
    {
108
        return $this->render('product/delete.html.twig', ['product' => $product]);
109
    }
110
111
    #[Route('/product/delete/{id}', name: 'product_delete_post', methods: ['POST'])]
112
    public function deletePost(EntityManagerInterface $entityManager, Product $product): Response
113
    {
114
        $entityManager->remove($product);
115
        $entityManager->flush();
116
117
        return $this->redirectToRoute('product_show_all');
118
    }
119
}
120