ImportCommand::setPaths()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace DoctrineDataFixtureModule\Command;
4
5
use Doctrine\Common\DataFixtures\Loader;
6
use Doctrine\ORM\EntityManager;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
12
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
13
use Zend\ServiceManager\ServiceLocatorInterface;
14
15
/**
16
 * Command for generate migration classes by comparing your current database schema
17
 * to your mapping information.
18
 *
19
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
20
 * @link    www.doctrine-project.org
21
 * @since   2.0
22
 * @author  Jonathan Wage <[email protected]>
23
 * @author  Rob van der Lee <[email protected]>
24
 */
25
class ImportCommand extends Command
26
{
27
    /**
28
     * @var array
29
     */
30
    protected $paths;
31
32
    /**
33
     * @var EntityManager
34
     */
35
    protected $entityManager;
36
    
37
    /**
38
     * The Zend ServiceManager
39
     * @var ServiceLocatorInterface
40
     */
41
    protected $serviceLocator;
42
43
    /**
44
     * @var int
45
     */
46
    const PURGE_MODE_TRUNCATE = 2;
47
48
    /**
49
     * ImportCommand constructor.
50
     * @param ServiceLocatorInterface $serviceLocator
51
     */
52
    public function __construct(ServiceLocatorInterface $serviceLocator)
53
    {
54
        $this->setServiceLocator($serviceLocator);
55
56
        parent::__construct();
57
    }
58
59
    /**
60
     * Configure the command
61
     */
62
    protected function configure()
63
    {
64
        parent::configure();
65
66
        $this->setName('data-fixture:import')
67
            ->setDescription('Import Data Fixtures')
68
            ->setHelp('The import command Imports data-fixtures')
69
            ->addOption('append', null, InputOption::VALUE_NONE, 'Append data to existing data.')
70
            ->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Truncate tables before inserting data');
71
    }
72
73
    /**
74
     * @param InputInterface $input
75
     * @param OutputInterface $output
76
     * @return int|null|void
77
     */
78
    public function execute(InputInterface $input, OutputInterface $output)
79
    {
80
        /** @var Loader $loader */
81
        $loader = new Loader();
82
        /** @var ORMPurger $purger */
83
        $purger = new ORMPurger();
84
85
        // Accept option purge-with-truncate
86
        if ($input->getOption('purge-with-truncate')) {
87
            $purger->setPurgeMode(self::PURGE_MODE_TRUNCATE);
88
        }
89
90
        /** @var ORMExecutor $executor */
91
        $executor = new ORMExecutor($this->getEntityManager(), $purger);
92
93
        foreach ($this->getPaths() as $key => $value) {
94
            $loader->loadFromDirectory($value);
95
        }
96
97
        $executor->execute($loader->getFixtures(), $input->getOption('append'));
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getPaths(): array
104
    {
105
        return $this->paths;
106
    }
107
108
    /**
109
     * @param array $paths
110
     */
111
    public function setPaths(array $paths): void
112
    {
113
        $this->paths = $paths;
114
    }
115
116
    /**
117
     * @return EntityManager
118
     */
119
    public function getEntityManager(): EntityManager
120
    {
121
        return $this->entityManager;
122
    }
123
124
    /**
125
     * @param EntityManager $entityManager
126
     */
127
    public function setEntityManager(EntityManager $entityManager): void
128
    {
129
        $this->entityManager = $entityManager;
130
    }
131
132
    /**
133
     * @return ServiceLocatorInterface
134
     */
135
    public function getServiceLocator(): ServiceLocatorInterface
136
    {
137
        return $this->serviceLocator;
138
    }
139
140
    /**
141
     * @param ServiceLocatorInterface $serviceLocator
142
     */
143
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator): void
144
    {
145
        $this->serviceLocator = $serviceLocator;
146
    }
147
}
148