Completed
Push — master ( c9ec76...c066a0 )
by Dominik
01:42
created

RunSqlCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Doctrine\DBAL\Command;
6
7
use Doctrine\DBAL\Connection;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * (c) <http://www.doctrine-project.org>.
13
 */
14
final class RunSqlCommand
15
{
16
    /**
17
     * @var Connection
18
     */
19
    private $connection;
20
21
    /**
22
     * @param Connection $connection
23
     */
24
    public function __construct(Connection $connection)
25
    {
26
        $this->connection = $connection;
27
    }
28
29
    /**
30
     * @param InputInterface  $input
31
     * @param OutputInterface $output
32
     *
33
     * @return int|null
34
     */
35
    public function __invoke(InputInterface $input, OutputInterface $output)
36
    {
37
        if (($sql = $input->getArgument('sql')) === null) {
38
            throw new \RuntimeException("Argument 'SQL' is required in order to execute this command correctly.");
39
        }
40
41
        $depth = $input->getOption('depth');
42
43
        if (!is_numeric($depth)) {
44
            throw new \LogicException("Option 'depth' must contains an integer value");
45
        }
46
47
        if (stripos($sql, 'select') === 0) {
48
            $resultSet = $this->connection->fetchAll($sql);
49
        } else {
50
            $resultSet = $this->connection->executeUpdate($sql);
51
        }
52
53
        $message = \Doctrine\Common\Util\Debug::dump($resultSet, (int) $depth, true, false);
54
55
        $output->write($message);
56
    }
57
}
58