pborreli/composer-service

View on GitHub
src/Ayaline/Bundle/ComposerBundle/Controller/ComposerController.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

/*
 * This file is part of `Composer as a service`.
 *
 * (c) Pascal Borreli <pascal@borreli.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Ayaline\Bundle\ComposerBundle\Controller;

use Sonata\NotificationBundle\Backend\AMQPBackendDispatcher;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ComposerController
{
    /**
     * @var EngineInterface
     */
    private $templating;

    /**
     * @var FormInterface
     */
    private $composerForm;

    /**
     * @var AMQPBackendDispatcher
     */
    private $sonataNotificationsBackend;

    /**
     * @param EngineInterface       $templating
     * @param FormInterface         $composerForm
     * @param AMQPBackendDispatcher $sonataNotificationsBackend
     */
    public function __construct(
        EngineInterface $templating,
        FormInterface $composerForm,
        AMQPBackendDispatcher $sonataNotificationsBackend
    ) {
        $this->templating = $templating;
        $this->composerForm = $composerForm;
        $this->sonataNotificationsBackend = $sonataNotificationsBackend;
    }

    /**
     * @return Response
     */
    public function indexAction()
    {
        return $this->templating->renderResponse(
            'AyalineComposerBundle:Composer:index.html.twig',
            ['form' => $this->composerForm->createView()]
        );
    }

    /**
     * @param Request $request
     *
     * @return JsonResponse
     */
    public function uploadComposerAction(Request $request)
    {
        $this->composerForm->handleRequest($request);

        if ($this->composerForm->isValid()) {
            $data = $this->composerForm->getData();
            $this->sonataNotificationsBackend->createAndPublish('upload.composer', [
                'body'               => $data['body'],
                'channelName'        => $request->getSession()->get('channelName'),
                'hasDevDependencies' => $data['hasDevDependencies'],
            ]);

            return new JsonResponse(['status' => 'ok']);
        }

        $errors = array_map(function (FormError $error) {
            return $error->getMessage();
        }, $this->composerForm->get('body')->getErrors());

        return new JsonResponse(['status' => 'ko', 'message' => $errors]);
    }
}