ImportCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 79
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A execute() 0 22 3
A configure() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrine\Console\Command;
6
7
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
8
use Doctrine\Common\DataFixtures\FixtureInterface;
9
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
10
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...
11
use Symfony\Component\Console\Exception\InvalidArgumentException;
12
use Symfony\Component\Console\Exception\LogicException;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
final class ImportCommand extends Command
18
{
19
    /**
20
     * @var FixtureInterface[]
21
     */
22
    private array $fixtures;
23
24
    private ORMExecutor $executor;
25
26
    private ?ORMPurger $purger;
27
28
    /**
29
     * @param FixtureInterface[] $fixtures
30
     * @param ORMExecutor        $executor
31
     * @param ORMPurger|null     $purger
32
     *
33
     * @throws LogicException
34
     */
35
    public function __construct(array $fixtures, ORMExecutor $executor, ?ORMPurger $purger = null)
36
    {
37
        $this->fixtures = $fixtures;
38
        $this->executor = $executor;
39
        $this->purger = $purger;
40
41
        parent::__construct();
42
    }
43
44
    /**
45
     * @param InputInterface  $input
46
     * @param OutputInterface $output
47
     *
48
     * @return int
49
     *
50
     * @throws InvalidArgumentException
51
     */
52
    public function execute(InputInterface $input, OutputInterface $output): int
53
    {
54
        $output->writeln('Executing data fixtures...');
55
56
        $purger = $this->purger ?? $this->executor->getPurger();
57
58
        // 1. Remove existing data with delete SQL (default)
59
        // 2. Remove existing data with truncate SQL
60
        if ($purger instanceof ORMPurger && $input->getOption('purge-with-truncate')) {
61
            $output->writeln('Import has been configured to purge existing data');
62
            $purger->setPurgeMode(2);
63
            $this->executor->setPurger($purger);
64
        }
65
66
        $this->executor->execute(
67
            $this->fixtures,
68
            (bool)$input->getOption('append')
69
        );
70
71
        $output->writeln(sprintf('Completed execution of \'%d\' fixtures', count($this->fixtures)));
72
73
        return 0;
74
    }
75
76
    /**
77
     * Configure the command's options
78
     *
79
     * @throws InvalidArgumentException
80
     */
81
    protected function configure(): void
82
    {
83
        parent::configure();
84
85
        $this->setName('data-fixture:import')
86
            ->setDescription('Import Data Fixtures')
87
            ->setHelp('The import command Imports data-fixtures')
88
            ->addOption('append', null, InputOption::VALUE_NONE, 'Append data to existing data.');
89
90
        if (null !== $this->purger) {
91
            $this->addOption(
92
                'purge-with-truncate',
93
                null,
94
                InputOption::VALUE_NONE,
95
                'Truncate tables before inserting data'
96
            );
97
        }
98
    }
99
}
100