ConsoleApplicationFormHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 75
c 1
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C process() 0 42 7
1
<?php
2
3
/*
4
 * This file is part of the AMFConsoleBundle.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace AMF\ConsoleBundle\Form\Handler;
11
12
use AMF\ConsoleBundle\Form\Model\ConsoleApplication;
13
use Symfony\Bundle\FrameworkBundle\Console\Application;
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\KernelInterface;
17
use Symfony\Component\Console\Input\ArrayInput;
18
use Symfony\Component\Console\Output\BufferedOutput;
19
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
20
use Symfony\Component\HttpFoundation\ParameterBag;
21
22
/**
23
 * Class ConsoleApplicationFormHandler.
24
 *
25
 * @author Amine Fattouch <[email protected]>
26
 */
27
class ConsoleApplicationFormHandler
28
{
29
    /**
30
     * @var FormInterface
31
     */
32
    private $form;
33
34
    /**
35
     * @var KernelInterface
36
     */
37
    private $kernel;
38
39
    /**
40
     * Constructor class.
41
     *
42
     * @param FormInterface   $form
43
     * @param KernelInterface $kernel
44
     */
45
    public function __construct(FormInterface $form, KernelInterface $kernel)
46
    {
47
        $this->form   = $form;
48
        $this->kernel = $kernel;
49
    }
50
51
    /**
52
     * process.
53
     *
54
     * @param Request            $request
55
     * @param ConsoleApplication $consoleApplication
56
     *
57
     * @return boolean
58
     */
59
    public function process(Request $request, ConsoleApplication $consoleApplication)
60
    {
61
        $success = false;
62
        $content = [];
63
        $errors  = [];
64
65
        $parameters = json_decode($request->getContent(), true);
66
        if ($parameters === null) {
67
            throw new BadRequestHttpException('Json Malformed !');
68
        }
69
70
        $request->request = new ParameterBag($parameters);
71
72
        $this->form->setData($consoleApplication);
73
        $this->form->handleRequest($request);
74
75
        if ($this->form->isSubmitted() && $this->form->isValid()) {
76
            $application = new Application($this->kernel);
77
            $application->setAutoExit(false);
78
79
            foreach ($consoleApplication->getCommands() as $command) {
80
                $commandDefinition = ['command' => $command->getDefinition()];
81
                foreach ($command->getArguments() as $argument) {
82
                    $commandDefinition[$argument->getName()] = $argument->getValue();
83
                }
84
85
                $input  = new ArrayInput($commandDefinition);
86
                $output = new BufferedOutput();
87
88
                $application->run($input, $output);
89
90
                $content[] = $output->fetch();
91
                $success   = true;
92
            }
93
        }
94
95
        foreach ($this->form->getErrors(true) as $error) {
96
            $errors[] = $error->getMessage();
0 ignored issues
show
Bug introduced by
The method getMessage() does not seem to exist on object<Symfony\Component\Form\FormErrorIterator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
        }
98
99
        return ['success' => $success, 'output' => $content, 'errors' => $errors];
100
    }
101
}
102