RunSqlCommand::getDepth()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Doctrine\DBAL\Command;
6
7
use Doctrine\Common\Util\Debug;
8
use Doctrine\DBAL\Connection;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * (c) <http://www.doctrine-project.org>.
14
 */
15
final class RunSqlCommand
16
{
17
    /**
18
     * @var Connection
19
     */
20
    private $connection;
21
22
    /**
23
     * @param Connection $connection
24
     */
25 4
    public function __construct(Connection $connection)
26
    {
27 4
        $this->connection = $connection;
28 4
    }
29
30
    /**
31
     * @param InputInterface  $input
32
     * @param OutputInterface $output
33
     *
34
     * @return int|null
35
     */
36 4
    public function __invoke(InputInterface $input, OutputInterface $output)
37
    {
38 4
        $sql = $this->getSql($input);
39 3
        $depth = $this->getDepth($input);
40
41 2
        if (stripos($sql, 'select') === 0) {
42 1
            $resultSet = $this->connection->fetchAll($sql);
43
        } else {
44 1
            $resultSet = $this->connection->executeUpdate($sql);
45
        }
46
47 2
        $message = Debug::dump($resultSet, (int) $depth, true, false);
48
49 2
        $output->write($message);
50 2
    }
51
52
    /**
53
     * @param InputInterface $input
54
     *
55
     * @return string
56
     */
57 4
    private function getSql(InputInterface $input): string
58
    {
59 4
        if (($sql = $input->getArgument('sql')) === null) {
60 1
            throw new \RuntimeException("Argument 'SQL' is required in order to execute this command correctly.");
61
        }
62
63 3
        return $sql;
64
    }
65
66
    /**
67
     * @param InputInterface $input
68
     *
69
     * @return int
70
     */
71 3
    private function getDepth(InputInterface $input): int
72
    {
73 3
        $depth = $input->getOption('depth');
74
75 3
        if (!is_numeric($depth)) {
76 1
            throw new \InvalidArgumentException("Option 'depth' must contains an integer value");
77
        }
78
79 2
        return (int) $depth;
80
    }
81
}
82