1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of `Composer as a service`. |
5
|
|
|
* |
6
|
|
|
* (c) Pascal Borreli <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ayaline\Bundle\ComposerBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Sonata\NotificationBundle\Backend\AMQPBackendDispatcher; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
16
|
|
|
use Symfony\Component\Form\FormError; |
17
|
|
|
use Symfony\Component\Form\FormInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
|
22
|
|
|
class ComposerController |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var EngineInterface |
26
|
|
|
*/ |
27
|
|
|
private $templating; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var FormInterface |
31
|
|
|
*/ |
32
|
|
|
private $composerForm; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AMQPBackendDispatcher |
36
|
|
|
*/ |
37
|
|
|
private $sonataNotificationsBackend; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param EngineInterface $templating |
41
|
|
|
* @param FormInterface $composerForm |
42
|
|
|
* @param AMQPBackendDispatcher $sonataNotificationsBackend |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
EngineInterface $templating, |
46
|
|
|
FormInterface $composerForm, |
47
|
|
|
AMQPBackendDispatcher $sonataNotificationsBackend |
48
|
|
|
) { |
49
|
|
|
$this->templating = $templating; |
50
|
|
|
$this->composerForm = $composerForm; |
51
|
|
|
$this->sonataNotificationsBackend = $sonataNotificationsBackend; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return Response |
56
|
|
|
*/ |
57
|
|
|
public function indexAction() |
58
|
|
|
{ |
59
|
|
|
return $this->templating->renderResponse( |
60
|
|
|
'AyalineComposerBundle:Composer:index.html.twig', |
61
|
|
|
['form' => $this->composerForm->createView()] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Request $request |
67
|
|
|
* |
68
|
|
|
* @return JsonResponse |
69
|
|
|
*/ |
70
|
|
|
public function uploadComposerAction(Request $request) |
71
|
|
|
{ |
72
|
|
|
$this->composerForm->handleRequest($request); |
73
|
|
|
|
74
|
|
|
if ($this->composerForm->isValid()) { |
75
|
|
|
$data = $this->composerForm->getData(); |
76
|
|
|
$this->sonataNotificationsBackend->createAndPublish('upload.composer', [ |
77
|
|
|
'body' => $data['body'], |
78
|
|
|
'channelName' => $request->getSession()->get('channelName'), |
79
|
|
|
'hasDevDependencies' => $data['hasDevDependencies'], |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
return new JsonResponse(['status' => 'ok']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$errors = array_map(function (FormError $error) { |
86
|
|
|
return $error->getMessage(); |
87
|
|
|
}, $this->composerForm->get('body')->getErrors()); |
88
|
|
|
|
89
|
|
|
return new JsonResponse(['status' => 'ko', 'message' => $errors]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|