Passed
Push — master ( 655177...3296ef )
by Anthony
11:29
created

VersionController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 43
c 4
b 0
f 0
dl 0
loc 111
rs 10
wmc 10

5 Methods

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