PostmanCollectionBuildCommand   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 93
ccs 0
cts 43
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A interact() 0 4 1
A execute() 0 16 1
B __construct() 0 30 1
1
<?php
2
3
/*
4
 * This file is part of the PostmanGeneratorBundle package.
5
 *
6
 * (c) Vincent Chalamon <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PostmanGeneratorBundle\Command;
13
14
use Doctrine\Common\Inflector\Inflector;
15
use PostmanGeneratorBundle\CommandParser\CommandParserChain;
16
use PostmanGeneratorBundle\Generator\CollectionGenerator;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
22
class PostmanCollectionBuildCommand extends Command
23
{
24
    /**
25
     * @var CollectionGenerator
26
     */
27
    private $collectionGenerator;
28
29
    /**
30
     * @var NormalizerInterface
31
     */
32
    private $normalizer;
33
34
    /**
35
     * @var CommandParserChain
36
     */
37
    private $commandParserChain;
38
39
    /**
40
     * @var string
41
     */
42
    private $collectionName;
43
44
    /**
45
     * @var string
46
     */
47
    private $rootDir;
48
49
    /**
50
     * @param CollectionGenerator $collectionGenerator
51
     * @param NormalizerInterface $normalizer
52
     * @param CommandParserChain  $commandParserChain
53
     * @param string              $collectionName
54
     * @param string              $rootDir
55
     */
56
    public function __construct(
57
        CollectionGenerator $collectionGenerator,
58
        NormalizerInterface $normalizer,
59
        CommandParserChain $commandParserChain,
60
        $collectionName,
61
        $rootDir
62
    ) {
63
        parent::__construct('postman:collection:build');
64
65
        $this->collectionGenerator = $collectionGenerator;
66
        $this->normalizer = $normalizer;
67
        $this->commandParserChain = $commandParserChain;
68
        $this->collectionName = $collectionName;
69
        $this->rootDir = $rootDir;
70
71
        $this
72
            ->setDescription('Build Postman collection')
73
            ->setHelp(<<<EOT
74
The <info>postman:collection:build</info> command helps you generate a Postman
75
collection according to your project configuration:
76
77
<info>php app/console postman:collection:build</info>
78
79
If any of the options is missing, the command will ask for their values interactively.
80
If you want to disable any user interaction, use <comment>--no-interaction</comment>,
81
but don't forget to pass all required arguments.
82
EOT
83
            )
84
        ;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function interact(InputInterface $input, OutputInterface $output)
91
    {
92
        $this->commandParserChain->parse($input, $output);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function execute(InputInterface $input, OutputInterface $output)
99
    {
100
        $this->commandParserChain->execute($input, $output);
101
102
        $filename = sprintf('%s.json', Inflector::camelize(strtolower($this->collectionName)));
103
        $filepath = $this->rootDir.'/../'.$filename;
104
105
        file_put_contents($filepath, json_encode($this->normalizer->normalize($this->collectionGenerator->generate(), 'json')));
106
107
        $text = sprintf('Postman collection has been successfully built in file %s.', $filename);
108
        $output->writeln([
109
            '',
110
            $this->getHelperSet()->get('formatter')->formatBlock($text, 'bg=blue;fg=white', true),
111
            '',
112
        ]);
113
    }
114
}
115