Completed
Push — master ( f81cf6...7fbbb0 )
by Oleg
03:40
created

AbstractApiCommand::getWriter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Command;
5
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use SlayerBirden\DFCodeGeneration\Writer\OutputWriter;
8
use SlayerBirden\DFCodeGeneration\Writer\WriteInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Exception\InvalidArgumentException;
11
use Symfony\Component\Console\Exception\LogicException;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
abstract class AbstractApiCommand extends Command
18
{
19
    /**
20
     * @var OutputInterface
21
     */
22
    protected $output;
23
    /**
24
     * @var string
25
     */
26
    protected $entityClassName;
27
    /**
28
     * @var bool
29
     */
30
    protected $force;
31
    /**
32
     * @var null|WriteInterface
33
     */
34
    protected $writer;
35
36 2
    public function __construct(?string $name = null, ?WriteInterface $writer = null)
37
    {
38 2
        parent::__construct($name);
39 2
        $this->writer = $writer;
40 2
    }
41
42 2
    protected function configure()
43
    {
44 2
        $this->addArgument('entity', InputArgument::REQUIRED, 'Entity class')
45 2
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Without force flag no writes happen (only output).');
46 2
    }
47
48 2
    protected function initialize(InputInterface $input, OutputInterface $output)
49
    {
50 2
        $this->output = $output;
51 2
        $this->entityClassName = $input->getArgument('entity');
52 2
        if (!class_exists($this->entityClassName)) {
53
            throw new InvalidArgumentException('Entity Class does not exist.');
54
        }
55 2
        $this->force = $input->getOption('force');
56
        // If it's not force mode we're using output writer
57 2
        if (!$this->force) {
58 1
            $this->writer = new OutputWriter($this->output);
59
        }
60
61
        // we need to register the default Annotation loader
62 2
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists') ( Ignorable by Annotation )

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

62
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
63 2
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 2
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70 2
        $output->writeln([
71 2
            '<error>IMPORTANT: please check all generated files before committing.</error>',
72
            '<error># You might want to run something like "php-cs-fixer" to make sure formatting is correct.</error>',
73
        ]);
74 2
    }
75
76
    /**
77
     * @return WriteInterface
78
     */
79
    protected function getWriter(): WriteInterface
80
    {
81
        if ($this->writer === null) {
82
            throw new LogicException('Writer is not defined!');
83
        }
84
        return $this->writer;
85
    }
86
}
87