Passed
Push — master ( ce99b8...8dba24 )
by Luiz Kim
03:01
created

DefaultCommand::addLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
1
<?php
2
3
namespace ControleOnline\Command;
4
5
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\Lock\LockFactory;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Lock\LockFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use ControleOnline\Service\DatabaseSwitchService;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Service\DatabaseSwitchService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\Console\Input\InputArgument;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputArgument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
abstract class DefaultCommand extends Command
14
{
15
    protected $input;
16
    protected $output;
17
    protected $lock;
18
    protected $lockFactory;
19
    protected $databaseSwitchService;
20
    protected $loggerService;
21
22
    abstract protected function runCommand(): int;
23
24
    public function __construct(string $name)
25
    {
26
        parent::__construct($name);
27
        $this->lock = $this->lockFactory->createLock($name);
28
        $this->addOption('domain', ['d'], InputOption::VALUE_OPTIONAL,  'Database domain identifier');
29
    }
30
31
    protected function execute(InputInterface $input, OutputInterface $output): int
32
    {
33
        $this->input = $input;
34
        $this->output = $output;
35
36
        $domain = $input->getOption('domain');
37
38
        if ($domain) {
39
            $this->addLog(sprintf('Executando migrações para o domínio: %s', $domain));
40
            $this->databaseSwitchService->switchDatabaseByDomain($domain);
41
            return $this->runCommand();
42
        }
43
44
        $domains = $this->databaseSwitchService->getAllDomains();
45
46
        foreach ($domains as $domain) {
47
            $this->addLog(sprintf('Executando migrações para o domínio: %s', $domain));
48
            $this->databaseSwitchService->switchDatabaseByDomain($domain);
49
            $this->runCommand();
50
        }
51
52
        return Command::SUCCESS;
53
    }
54
55
    public function addLog(string|iterable $messages, int $options = 0, ?string $logName = 'integration')
56
    {
57
        $this->output->writeln($messages, $options);
58
        $this->loggerService->getLogger($logName)->info($messages);
59
    }
60
61
    public function __destruct()
62
    {
63
        $this->lock->release();
64
    }
65
}
66