Issues (21)

src/Command/AbstractCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Command;
6
7
use MongoDB\Database as Connection;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Class AbstractCommand.
17
 */
18
abstract class AbstractCommand extends Command
19
{
20
    /** @var SymfonyStyle */
21
    protected $io;
22
23
    /** @var Connection */
24
    protected $connection;
25 8
26
    /** @var ContainerInterface */
27 8
    private $container;
28
29 8
    /**
30 8
     * AbstractCommand constructor.
31
     *
32
     * @param ContainerInterface $container
33
     */
34
    public function __construct(ContainerInterface $container, string $name = null)
35 8
    {
36
        parent::__construct($name);
37 8
        $this->container = $container;
38 8
    }
39
40 8
    protected function getContainer(): ContainerInterface
41
    {
42 8
        return $this->container;
43 2
    }
44
45
    /**
46 8
     * {@inheritdoc}
47 1
     */
48
    protected function configure()
49
    {
50 7
        parent::configure();
51 7
        $this
52
            ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function initialize(InputInterface $input, OutputInterface $output)
59
    {
60
        parent::initialize($input, $output);
61
        $this->io = new SymfonyStyle($input, $output);
62
63
        $connectionName = 'mongo.connection';
64
65
        if ($input->getOption('connection')) {
66
            $connectionName .= '.' . $input->getOption('connection');
67
        }
68
69
        if (! $this->container->has($connectionName)) {
70
            throw new \LogicException(sprintf('No connection named \'%s\' found', $input->getOption('connection')));
0 ignored issues
show
It seems like $input->getOption('connection') can also be of type string[]; however, parameter $values of sprintf() does only seem to accept double|integer|string, 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

70
            throw new \LogicException(sprintf('No connection named \'%s\' found', /** @scrutinizer ignore-type */ $input->getOption('connection')));
Loading history...
71
        }
72
73
        $this->connection = $this->container->get($connectionName);
74
    }
75
}
76