Completed
Pull Request — master (#20)
by Pavel
10:22
created

GenerateDocsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 32 4
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * GenerateDocsCommand
13
 */
14
class GenerateDocsCommand extends Command
15
{
16
    /**
17
     * Configuration of command
18
     */
19
    protected function configure()
20
    {
21
        $this
22
            ->setName('generate:docs')
23
            ->setDescription('Generate documentation for api')
24
            ->setHelp('<info>php partisan generate docs</info>')
25
        ;
26
    }
27
28
    /**
29
     * Execute method of command
30
     * @param InputInterface $input
31
     * @param OutputInterface $output
32
     *
33
     * @return int|null|void
34
     * @throws \Interop\Container\Exception\ContainerException
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        /* @var \App\Console\Partisan $app*/
39
        $app      = $this->getApplication();
40
        $settings = $app->container->get('settings');
0 ignored issues
show
Bug introduced by
The property container does not seem to exist in Symfony\Component\Console\Application.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
41
42
43
        $apidocPath = CONFIG_PATH . '/apidoc.php';
44
        if (false === file_exists($apidocPath)) {
45
            throw new \RunTimeException(sprintf('The apidoc file `%s` not found', $apidocPath));
46
        };
47
48
        $path = APP_PATH;
49
        if (!is_writeable($path)) {
50
            throw new \RunTimeException(sprintf('The directory `%s` is not writeable', $path));
51
        }
52
53
        $baseName = $path . '/apidoc.json';
54
        $content  = require($apidocPath);
55
56
        $content['url']       = $settings['params']['api'];
57
        $content['sampleUrl'] = $settings['params']['api'];
58
59
        $content = json_encode($content);
60
        if (false === file_put_contents($baseName, $content)) {
61
            throw new \RunTimeException(sprintf('The file `%s` could not be written to', $baseName));
62
        };
63
64
        $output->writeln([exec('apidoc -i ./app -o ./docs -t ./docstemplate')]);
65
66
        return;
67
    }
68
}
69