Passed
Push — master ( fb7b29...781089 )
by Anthony
10:52
created

PackagesDistController::sendComposerJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
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 SensioLabs\AnsiConverter\AnsiToHtmlConverter;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Bundle\FrameworkBundle\Console\Application;
12
use Symfony\Component\Console\Input\ArrayInput;
13
use Symfony\Component\Console\Output\BufferedOutput;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
16
use Symfony\Component\HttpFoundation\File\Exception\FileException;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\KernelInterface;
20
use Symfony\Component\Routing\Annotation\Route;
21
22
class PackagesDistController extends AbstractController
23
{
24
    /**
25
     * @Route("/packages/dist/send-package/{package_name}", name="ribsadmin_packages_dist_send", requirements={"package_name"=".+"})
26
     * @param EntityManagerInterface $em
27
     * @param Version $version
28
     * @param string $package_name
29
     * @return mixed|null
30
     * @throws Exception
31
     */
32
    public function sendPackageInformations(EntityManagerInterface $em, Version $version, string $package_name): JsonResponse
33
    {
34
        $package = $em->getRepository(Package::class)->findOneBy(["package_name" => $package_name]);
35
36
        if ($package) {
37
            $version->setPackageEntity($package);
38
        }
39
40
        return new JsonResponse([
41
            "package" => $version->getPackage(),
42
            "package_date" => $version->getVersionDate()
43
        ]);
44
    }
45
46
    /**
47
     * @Route("/packages/dist/send-composer-lock/", name="ribsadmin_packages_dist_send_composer_lock")
48
     * @return JsonResponse
49
     */
50
    public function sendComposerJson(): JsonResponse
51
    {
52
        $composer_lock = file_get_contents('../composer.lock');
53
54
        if ($composer_lock) {
55
            $composer_lock = json_decode($composer_lock);
56
        }
57
58
        return new JsonResponse($composer_lock);
59
    }
60
61
    /**
62
     * @Route("/packages/dist/send-token/", name="ribsadmin_packages_dist_send_token")
63
     * @param ParameterBagInterface $parameter
64
     * @return JsonResponse
65
     */
66
    public function sendToken(ParameterBagInterface $parameter): JsonResponse
67
    {
68
        return new JsonResponse([
69
            "token" => $parameter->get("ribs_admin.packages_token")
70
        ]);
71
    }
72
73
    /**
74
     * @param $file
75
     * @param $folder
76
     * @return bool
77
     */
78
    private function uploadConfigFile($file, $folder)
79
    {
80
        $success = true;
81
        if ($file) {
82
            try {
83
                $file->move('../config/'.$folder, $file->getClientOriginalName());
84
            } catch (FileException $exception) {
85
                $success = false;
86
            }
87
        }
88
        return $success;
89
    }
90
91
    /**
92
     * @Route("/packages/dist/change-version/", name="ribsadmin_packages_dist_change_version", requirements={"package_name"=".+"}, methods={"POST"})
93
     * @param KernelInterface $kernel
94
     * @param Request $request
95
     * @return JsonResponse
96
     * @throws Exception
97
     */
98
    public function changePackageVersion(KernelInterface $kernel, Request $request): JsonResponse
99
    {
100
        $application = new Application($kernel);
101
        $application->setAutoExit(false);
102
103
        $package_name = $request->get("package_name");
104
        $version = $request->get("version");
105
106
        if (!$this->uploadConfigFile($request->files->get('package_routes'), "routes")) {
107
            return new JsonResponse([
108
                "error_message" => "Routes file move is impossible"
109
            ]);
110
        }
111
112
        if (!$this->uploadConfigFile($request->files->get('package_config'), "packages")) {
113
            return new JsonResponse([
114
                "error_message" => "Package file move is impossible"
115
            ]);
116
        }
117
118
        $input = new ArrayInput([
119
            'command' => 'ribsadmin:change-package-version',
120
            'package-name' => $package_name.":".$version,
121
        ]);
122
123
        $output = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true);
124
        $application->run($input, $output);
125
126
        $converter = new AnsiToHtmlConverter();
127
        $content = $output->fetch();
128
129
        return new JsonResponse($converter->convert($content));
130
    }
131
}
132