Passed
Pull Request — master (#7448)
by Ilya
14:31
created

RunDqlCommand::execute()   B

Complexity

Conditions 9
Paths 14

Size

Total Lines 57
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 12.6176

Importance

Changes 0
Metric Value
cc 9
eloc 30
nc 14
nop 2
dl 0
loc 57
ccs 20
cts 31
cp 0.6452
crap 12.6176
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Tools\Console\Command;
6
7
use Doctrine\Common\Util\Debug;
8
use Doctrine\ORM\EntityManagerInterface;
9
use LogicException;
10
use RuntimeException;
11
use Symfony\Component\Console\Command\Command;
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
use Symfony\Component\Console\Style\SymfonyStyle;
17
use function constant;
18
use function defined;
19
use function is_numeric;
20
use function sprintf;
21
use function str_replace;
22
use function strtoupper;
23
24
/**
25
 * Command to execute DQL queries in a given EntityManager.
26
 */
27
class RunDqlCommand extends Command
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32 3
    protected function configure()
33
    {
34 3
        $this->setName('orm:run-dql')
35 3
             ->setDescription('Executes arbitrary DQL directly from the command line')
36 3
             ->addArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.')
37 3
             ->addOption('hydrate', null, InputOption::VALUE_REQUIRED, 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.', 'object')
38 3
             ->addOption('first-result', null, InputOption::VALUE_REQUIRED, 'The first result in the result set.')
39 3
             ->addOption('max-result', null, InputOption::VALUE_REQUIRED, 'The maximum number of results in the result set.')
40 3
             ->addOption('depth', null, InputOption::VALUE_REQUIRED, 'Dumping depth of Entity graph.', 7)
41 3
             ->addOption('show-sql', null, InputOption::VALUE_NONE, 'Dump generated SQL instead of executing query')
42 3
             ->setHelp('Executes arbitrary DQL directly from the command line.');
43 3
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50 2
        $ui = new SymfonyStyle($input, $output);
51
52
        /** @var EntityManagerInterface $em */
53 2
        $em  = $this->getHelper('em')->getEntityManager();
54 2
        $dql = $input->getArgument('dql');
55
56 2
        if ($dql === null) {
57
            throw new RuntimeException("Argument 'dql' is required in order to execute this command correctly.");
58
        }
59
60 2
        $depth = $input->getOption('depth');
61
62 2
        if (! is_numeric($depth)) {
63
            throw new LogicException("Option 'depth' must contain an integer value");
64
        }
65
66 2
        $hydrationModeName = $input->getOption('hydrate');
67 2
        $hydrationMode     = 'Doctrine\ORM\Query::HYDRATE_' . strtoupper(str_replace('-', '_', $hydrationModeName));
68
69 2
        if (! defined($hydrationMode)) {
70
            throw new RuntimeException(sprintf(
71
                "Hydration mode '%s' does not exist. It should be either: object. array, scalar or single-scalar.",
72
                $hydrationModeName
73
            ));
74
        }
75
76 2
        $query       = $em->createQuery($dql);
77 2
        $firstResult = $input->getOption('first-result');
78
79 2
        if ($firstResult !== null) {
80
            if (! is_numeric($firstResult)) {
81
                throw new LogicException("Option 'first-result' must contain an integer value");
82
            }
83
84
            $query->setFirstResult((int) $firstResult);
85
        }
86
87 2
        $maxResult = $input->getOption('max-result');
88
89 2
        if ($maxResult !== null) {
90
            if (! is_numeric($maxResult)) {
91
                throw new LogicException("Option 'max-result' must contain an integer value");
92
            }
93
94
            $query->setMaxResults((int) $maxResult);
95
        }
96
97 2
        if ($input->getOption('show-sql')) {
98 1
            $ui->text($query->getSQL());
99 1
            return;
100
        }
101
102 1
        $resultSet = $query->execute([], constant($hydrationMode));
103
104 1
        $ui->text(Debug::dump($resultSet, $input->getOption('depth'), true, false));
105 1
    }
106
}
107