ControlledErrorCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 2
crap 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\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Output\StreamOutput;
18
use Ynlo\GraphQLBundle\Error\ControlledErrorManager;
19
use Ynlo\GraphQLBundle\Error\Exporter\ErrorListExporterInterface;
20
21
class ControlledErrorCommand extends Command
22
{
23
    /**
24
     * @var ControlledErrorManager
25
     */
26
    protected $errorManager;
27
28
    /**
29
     * @var array|iterable|ErrorListExporterInterface[]
30
     */
31
    protected $exporters = [];
32
33
    /**
34
     * ControlledErrorCommand constructor.
35
     *
36
     * @param ControlledErrorManager                      $errorManager
37
     * @param array|iterable|ErrorListExporterInterface[] $exporters
38
     */
39
    public function __construct(ControlledErrorManager $errorManager, iterable $exporters)
40
    {
41
        $this->errorManager = $errorManager;
42
        $this->exporters = $exporters;
43
        parent::__construct();
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    protected function configure()
50
    {
51
        $this->setName('graphql:error:list')
52
             ->setDescription('View, export and control your API errors.')
53
             ->addOption('exporter', 'x', InputOption::VALUE_REQUIRED, 'Exporter to user', 'console')
54
             ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Name of the file to save the error list, e.i. error_codes.md');
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    protected function execute(InputInterface $input, OutputInterface $output)
61
    {
62
        $this->errorManager->clear();//force reload
63
        $errors = $this->errorManager->all();
64
65
        $result = $output;
66
        if ($output = $input->getOption('output')) {
67
            $info = new \SplFileInfo($output);
68
            if (!file_exists($info->getPath())) {
69
                if (!mkdir($info->getPath(), 0777, true) && !is_dir($info->getPath())) {
70
                    throw new \RuntimeException(sprintf('Directory "%s" was not created', $info->getPath()));
71
                }
72
            }
73
74
            $result = new StreamOutput(fopen($output, 'w+b', false));
0 ignored issues
show
Bug introduced by
It seems like fopen($output, 'w+b', false) can also be of type false; however, parameter $stream of Symfony\Component\Consol...amOutput::__construct() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            $result = new StreamOutput(/** @scrutinizer ignore-type */ fopen($output, 'w+b', false));
Loading history...
75
        }
76
77
        foreach ($this->exporters as $exporter) {
78
            if ($exporter->getName() === $input->getOption('exporter')) {
79
                $exporter->export($errors, $result);
80
81
                return;
82
            }
83
        }
84
85
        throw new \Exception('There are not any registered exporter to process the list of errors');
86
    }
87
}
88