Completed
Pull Request — develop (#589)
by Narcotic
28:40 queued 23:01
created

GenerateResourceCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 56.1%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 4
dl 0
loc 93
ccs 23
cts 41
cp 0.561
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 20 1
A createGenerator() 0 16 3
A execute() 0 10 1
1
<?php
2
/**
3
 * generate:resource command
4
 */
5
6
namespace Graviton\GeneratorBundle\Command;
7
8
use Graviton\GeneratorBundle\Generator\ResourceGenerator;
9
use Graviton\GeneratorBundle\Definition\Loader\LoaderInterface;
10
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineEntityCommand;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * generator command
17
 *
18
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
19
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link     http://swisscom.ch
21
 */
22
class GenerateResourceCommand extends GenerateDoctrineEntityCommand
23
{
24
    /**
25
     * @var ResourceGenerator
26
     */
27
    private $resourceGenerator;
28
    /**
29
     * @var LoaderInterface
30
     */
31
    private $definitionLoader;
32
    /**
33
     * @var InputInterface
34
     */
35
    private $input;
36
37
    /**
38
     * @param ResourceGenerator $resourceGenerator generator to use for resource generation
39
     * @param LoaderInterface   $definitionLoader  JSON definition loaded
40
     */
41 4
    public function __construct(ResourceGenerator $resourceGenerator, LoaderInterface $definitionLoader)
42
    {
43 4
        $this->resourceGenerator = $resourceGenerator;
44 4
        $this->definitionLoader = $definitionLoader;
45 4
        parent::__construct();
46 4
    }
47
48
    /**
49
     * {@inheritDoc}
50
     *
51
     * @return void
52
     */
53 4
    protected function configure()
54
    {
55 4
        parent::configure();
56
57 2
        $this
58 4
            ->addOption(
59 4
                'json',
60 4
                '',
61 4
                InputOption::VALUE_OPTIONAL,
62 2
                'Path to the json definition.'
63 2
            )
64 4
            ->addOption(
65 4
                'no-controller',
66 4
                '',
67 4
                InputOption::VALUE_OPTIONAL,
68 2
                'Pass if no controller should be generated'
69 2
            )
70 4
            ->setName('graviton:generate:resource')
71 4
            ->setDescription('Generates a graviton rest resource');
72 4
    }
73
74
    /**
75
     * {@inheritDoc}
76
     *
77
     * @param InputInterface  $input  input
78
     * @param OutputInterface $output output
79
     *
80
     * @return void
81
     */
82
    protected function execute(InputInterface $input, OutputInterface $output)
83
    {
84
        // put input here for later use..
85
        $this->input = $input;
86
87
        parent::execute(
88
            $input,
89
            $output
90
        );
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     *
96
     * @return ResourceGenerator
97
     */
98
    protected function createGenerator()
99
    {
100
        // do we have a json path passed?
101
        if ($this->input->getOption('json') !== null) {
102
            $definitions = $this->definitionLoader->load($this->input->getOption('json'));
103
            if (count($definitions) > 0) {
104
                $this->resourceGenerator->setJson($definitions[0]);
105
            }
106
        }
107
108
        $this->resourceGenerator->setGenerateController(
109
            $this->input->getOption('no-controller') != 'true'
110
        );
111
112
        return $this->resourceGenerator;
113
    }
114
}
115