Passed
Push — master ( 07a5d2...98935d )
by Anthony
02:57
created

PackageController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 132
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A updatePackage() 0 14 2
A edit() 0 27 4
A sendComposerJson() 0 9 2
A index() 0 5 1
A delete() 0 13 2
A sendPackageInformations() 0 11 2
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Controller;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Exception;
7
use PiouPiou\RibsAdminBundle\Entity\Package;
8
use PiouPiou\RibsAdminBundle\Service\Version;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Symfony\Component\Validator\Constraints\Date;
16
17
class PackageController extends AbstractController
18
{
19
    /**
20
     * @Route("/packages/", name="ribsadmin_packages")
21
     * @param EntityManagerInterface $em
22
     * @return Response
23
     */
24
    public function index(EntityManagerInterface $em): Response
25
    {
26
        $packages = $em->getRepository(Package::class)->findAll();
27
        
28
        return $this->render("@RibsAdmin/packages/list.html.twig", ["packages" => $packages]);
29
    }
30
31
    /**
32
     * @Route("/packages/create/", name="ribsadmin_packages_create")
33
     * @Route("/packages/show/{guid}", name="ribsadmin_packages_show")
34
     * @Route("/packages/edit/{guid}", name="ribsadmin_packages_edit")
35
     * @param Request $request
36
     * @param EntityManagerInterface $em
37
     * @param string|null $guid
38
     * @return Response
39
     */
40
    public function edit(Request $request, EntityManagerInterface $em, string $guid = null): Response
41
    {
42
        if (!$guid) {
43
            $package = new Package();
44
            $text = "created";
45
        } else {
46
            $package = $em->getRepository(Package::class)->findOneBy(["guid" => $guid]);
47
            $text = "edited";
48
        }
49
50
        $form = $this->createForm(\PiouPiou\RibsAdminBundle\Form\Package::class, $package);
51
        $form->handleRequest($request);
52
53
        if ($form->isSubmitted() && $form->isValid()) {
54
            /** @var Package $data */
55
            $data = $form->getData();
56
            $em->persist($data);
57
            $em->flush();
58
            $this->addFlash("success-flash", "Package " . $data->getProjectName() . " was " . $text);
59
60
            return $this->redirectToRoute("ribsadmin_packages");
61
        }
62
63
        return $this->render("@RibsAdmin/packages/edit.html.twig", [
64
            "form" => $form->createView(),
65
            "form_errors" => $form->getErrors(),
66
            "package" => $package
67
        ]);
68
    }
69
70
    /**
71
     * @Route("/packages/delete/{guid}", name="ribsadmin_packages_delete")
72
     * @param EntityManagerInterface $em
73
     * @param string $guid
74
     * @return RedirectResponse
75
     */
76
    public function delete(EntityManagerInterface $em, string $guid): RedirectResponse
77
    {
78
        $package = $em->getRepository(Package::class)->findOneBy(["guid" => $guid]);
79
80
        if ($package) {
81
            $em->remove($package);
82
            $em->flush();
83
            $this->addFlash("success-flash", "The project package was deleted");
84
        } else {
85
            $this->addFlash("error-flash", "The project package was not found");
86
        }
87
88
        return $this->redirectToRoute("ribsadmin_packages");
89
    }
90
91
    /**
92
     * @Route("/packages/update/{guid}", name="ribsadmin_packages_update")
93
     * @param EntityManagerInterface $em
94
     * @param Version $version
95
     * @param string $guid
96
     * @return RedirectResponse
97
     * @throws Exception
98
     */
99
    public function updatePackage(EntityManagerInterface $em, Version $version, string $guid): RedirectResponse
100
    {
101
        $package = $em->getRepository(Package::class)->findOneBy(["guid" => $guid]);
102
103
        if ($package) {
104
            $version->setPackageEntity($package);
105
            $version->save($package->getPackageName());
106
107
            $this->addFlash("success-flash", "The project package was updated");
108
        } else {
109
            $this->addFlash("error-flash", "The project package was not found");
110
        }
111
112
        return $this->redirectToRoute("ribsadmin_packages");
113
    }
114
115
    /**
116
     * @Route("/packages/send-package/{package_name}", name="ribsadmin_packages_send", requirements={"package_name"=".+"})
117
     * @param EntityManagerInterface $em
118
     * @param Version $version
119
     * @param string $package_name
120
     * @return mixed|null
121
     */
122
    public function sendPackageInformations(EntityManagerInterface $em, Version $version, string $package_name): JsonResponse
123
    {
124
        $package = $em->getRepository(Package::class)->findOneBy(["package_name" => $package_name]);
125
126
        if ($package) {
127
            $version->setPackageEntity($package);
128
        }
129
130
        return new JsonResponse([
131
            "package" => $version->getPackage($package_name),
132
            "package_date" => $version->getPackageDate($package_name)
0 ignored issues
show
Bug introduced by
The method getPackageDate() does not exist on PiouPiou\RibsAdminBundle\Service\Version. Did you maybe mean getPackage()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
            "package_date" => $version->/** @scrutinizer ignore-call */ getPackageDate($package_name)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
        ]);
134
    }
135
136
    /**
137
     * @Route("/packages/send-composer-lock/", name="ribsadmin_packages_send_composer_lock")
138
     * @return JsonResponse
139
     */
140
    public function sendComposerJson(): JsonResponse
141
    {
142
        $composer_lock = file_get_contents('../composer.lock');
143
144
        if ($composer_lock) {
145
            $composer_lock = json_decode($composer_lock);
146
        }
147
148
        return new JsonResponse($composer_lock);
149
    }
150
}
151