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

GenerateDocsCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 31 4
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');
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...
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