GenerateDocsCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * GenerateDocsCommand
11
 */
12
class GenerateDocsCommand extends Command
13
{
14
    /**
15
     * Configuration of command
16
     */
17
    protected function configure()
18
    {
19
        $this
20
            ->setName('generate:docs')
21
            ->setDescription('Generate documentation for api')
22
            ->setHelp('<info>php partisan generate docs</info>')
23
        ;
24
    }
25
26
    /**
27
     * Execute method of command
28
     * @param InputInterface $input
29
     * @param OutputInterface $output
30
     *
31
     * @return int|null|void
32
     * @throws \Interop\Container\Exception\ContainerException
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        /* @var \App\Console\Partisan $app */
37
        $app      = $this->getApplication();
38
        $settings = $app->container->get('settings');
39
40
        $apidocPath = CONFIG_PATH.'/apidoc.php';
41
        if (false === file_exists($apidocPath)) {
42
            throw new \RunTimeException(sprintf('The apidoc file `%s` not found', $apidocPath));
43
        };
44
45
        $path = APP_PATH;
46
        if (!is_writeable($path)) {
47
            throw new \RunTimeException(sprintf('The directory `%s` is not writeable', $path));
48
        }
49
50
        $baseName = $path.'/apidoc.json';
51
        $content  = require($apidocPath);
52
53
        $content['url']       = $settings['params']['api'];
54
        $content['sampleUrl'] = $settings['params']['api'];
55
56
        $content = json_encode($content);
57
        if (false === file_put_contents($baseName, $content)) {
58
            throw new \RunTimeException(sprintf('The file `%s` could not be written to', $baseName));
59
        };
60
61
        $output->writeln([exec('apidoc -i ./app -o ./docs -t ./docstemplate')]);
62
63
        return;
64
    }
65
}
66