Completed
Push — master ( 300eda...15c856 )
by Rafael
09:26
created

GraphQLPluginOptionsCommand::execute()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 16
nop 2
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\Plugin\DefinitionPluginInterface;
21
use Ynlo\GraphQLBundle\Definition\Plugin\GraphQLDefinitionPluginManager;
22
23
/**
24
 * GraphQLPluginOptionsCommand
25
 */
26
class GraphQLPluginOptionsCommand extends ContainerAwareCommand
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31
    protected function configure()
32
    {
33
        $this->setName('graphql:plugins')
34
             ->setDescription('Expose all available options for one or all graphql plugins')
35
             ->addArgument('plugin', InputArgument::OPTIONAL, 'Show only options for given plugin');
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $filterBy = null;
44
        if ($input->hasArgument('plugin')) {
45
            $filterBy = $input->getArgument('plugin');
46
        }
47
48
        $extensions = $this->getContainer()->get(GraphQLDefinitionPluginManager::class)->getExtensions();
49
50
        $dumped = false;
51
        foreach ($extensions as $extension) {
52
            if ($filterBy && $extension->getName() !== $filterBy) {
53
                continue;
54
            }
55
            $config = $this->createConfig($extension);
56
            $dumper = new YamlReferenceDumper();
57
            $dump = $dumper->dump($config);
58
            if (substr_count($dump, "\n") > 1) {
59
                $output->writeln($dump);
60
                $dumped = true;
61
            }
62
        }
63
64
        if ($filterBy && !$dumped) {
65
            throw new \InvalidArgumentException('The plugin does not exist or not have configuration');
66
        }
67
    }
68
69
    /**
70
     * @param DefinitionPluginInterface $extension
71
     *
72
     * @return ConfigurationInterface
73
     */
74
    protected function createConfig(DefinitionPluginInterface $extension): ConfigurationInterface
75
    {
76
        return new class($extension) implements ConfigurationInterface
77
        {
78
            /**
79
             * @var DefinitionPluginInterface
80
             */
81
            protected $extension;
82
83
            /**
84
             *  constructor.
85
             *
86
             * @param DefinitionPluginInterface $extension
87
             */
88
            public function __construct(DefinitionPluginInterface $extension)
89
            {
90
                $this->extension = $extension;
91
            }
92
93
            /**
94
             * @return TreeBuilder
95
             */
96
            public function getConfigTreeBuilder()
97
            {
98
                $treeBuilder = new TreeBuilder();
99
                $root = $treeBuilder->root($this->extension->getName());
100
                $this->extension->buildConfig($root);
101
102
                return $treeBuilder;
103
            }
104
        };
105
    }
106
}
107