Passed
Push — master ( c39fb6...9e9a0d )
by Rafael
04:42
created

DefinitionOptionsCommand::execute()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 16
nop 2
dl 0
loc 25
ccs 0
cts 22
cp 0
crap 72
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Command;
12
13
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15
use Symfony\Component\Config\Definition\ConfigurationInterface;
16
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Ynlo\GraphQLBundle\Definition\Extension\DefinitionExtensionInterface;
21
use Ynlo\GraphQLBundle\Definition\Extension\DefinitionExtensionManager;
22
23
/**
24
 * DefinitionOptionsCommand
25
 */
26
class DefinitionOptionsCommand extends ContainerAwareCommand
27
{
28
    protected static $defaultName = 'graphql:definition:options';
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    protected function configure()
34
    {
35
        $this->setDescription('Expose all available options for one or all graphql definition extensions');
36
        $this->addArgument('extension', InputArgument::OPTIONAL, 'Show only options for given extension');
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $filterBy = null;
45
        if ($input->hasArgument('extension')) {
46
            $filterBy = $input->getArgument('extension');
47
        }
48
49
        $extensions = $this->getContainer()->get(DefinitionExtensionManager::class)->getExtensions();
50
51
        $dumped = false;
52
        foreach ($extensions as $extension) {
53
            if ($filterBy && $extension->getName() != $filterBy) {
54
                continue;
55
            }
56
            $config = $this->createConfig($extension);
57
            $dumper = new YamlReferenceDumper();
58
            $dump = $dumper->dump($config);
59
            if (substr_count($dump, "\n") > 1) {
60
                $output->writeln($dump);
61
                $dumped = true;
62
            }
63
        }
64
65
        if ($filterBy && !$dumped) {
66
            throw new \InvalidArgumentException('The extension does not exist or not have configuration');
67
        }
68
    }
69
70
    /**
71
     * @param DefinitionExtensionInterface $extension
72
     *
73
     * @return ConfigurationInterface
74
     */
75
    protected function createConfig(DefinitionExtensionInterface $extension): ConfigurationInterface
76
    {
77
        return new class($extension) implements ConfigurationInterface
78
        {
79
            /**
80
             * @var DefinitionExtensionInterface
81
             */
82
            protected $extension;
83
84
            /**
85
             *  constructor.
86
             *
87
             * @param DefinitionExtensionInterface $extension
88
             */
89
            public function __construct(DefinitionExtensionInterface $extension)
90
            {
91
                $this->extension = $extension;
92
            }
93
94
            /**
95
             * @return TreeBuilder
96
             */
97
            public function getConfigTreeBuilder()
98
            {
99
                $treeBuilder = new TreeBuilder();
100
                $root = $treeBuilder->root($this->extension->getName());
101
                $this->extension->buildConfig($root);
102
103
                return $treeBuilder;
104
            }
105
        };
106
    }
107
}
108