Passed
Push — master ( c4c0f1...e1851e )
by Anthony
03:02
created

VersionController::sendComposerJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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