1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\{EntityManager, EntityManagerInterface}; |
6
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\{InputInterface, InputArgument, InputOption}; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Import users from CSV file exported from database by query: |
14
|
|
|
* COPY ( |
15
|
|
|
* SELECT u.id, u.login, ui.name, to_char(ui.created, 'YYYY-MM-DD_HH24:MI:SS') AS created_at |
16
|
|
|
* FROM users.logins u |
17
|
|
|
* LEFT JOIN users.info ui ON (ui.id = u.id) |
18
|
|
|
* WHERE u.id <> (-1) |
19
|
|
|
* ) TO '/tmp/point_users.csv' WITH HEADER DELIMITER '|' CSV; |
20
|
|
|
*/ |
21
|
|
|
class ImportUsersCommand extends Command |
22
|
|
|
{ |
23
|
|
|
/** @var EntityManager */ |
24
|
|
|
private $em; |
25
|
|
|
|
26
|
|
|
public function __construct(EntityManagerInterface $em) |
27
|
|
|
{ |
28
|
|
|
$this->em = $em; |
|
|
|
|
29
|
|
|
|
30
|
|
|
parent::__construct(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function configure() |
34
|
|
|
{ |
35
|
|
|
$this |
36
|
|
|
->setName('point:import:users') |
37
|
|
|
->setDescription('Import users from CSV file') |
38
|
|
|
->addArgument( |
39
|
|
|
'file', |
40
|
|
|
InputArgument::REQUIRED, |
41
|
|
|
'CSV file path' |
42
|
|
|
) |
43
|
|
|
->addOption( |
44
|
|
|
'check-only', |
45
|
|
|
null, |
46
|
|
|
InputOption::VALUE_NONE, |
47
|
|
|
'If set, command will not perform write operations in the database' |
48
|
|
|
) |
49
|
|
|
->addOption( |
50
|
|
|
'no-skip-first', |
51
|
|
|
null, |
52
|
|
|
InputOption::VALUE_NONE, |
53
|
|
|
'Do not skip first line (if no headers in CSV file)' |
54
|
|
|
) |
55
|
|
|
; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
$fs = new Filesystem(); |
61
|
|
|
|
62
|
|
|
$fileName = $input->getArgument('file'); |
63
|
|
|
|
64
|
|
|
if (!($fs->exists($fileName) && is_readable($fileName))) { |
65
|
|
|
$output->writeln('File does not exists or not readable.'); |
66
|
|
|
return 1; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (false === ($file = fopen($fileName, 'r'))) { |
70
|
|
|
$output->writeln('fopen() error'); |
71
|
|
|
return 1; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!$input->getOption('no-skip-first')) { |
75
|
|
|
// Reading headers line |
76
|
|
|
fgets($file); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$count = 0; |
80
|
|
|
|
81
|
|
|
while (false !== ($row = fgetcsv($file, 1000, '|'))) { |
82
|
|
|
if (count($row) !== 4) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$createdAt = \DateTime::createFromFormat('Y-m-d_H:i:s', $row[3]) ?: new \DateTime(); |
87
|
|
|
|
88
|
|
|
$user = new User($row[0], $createdAt, $row[1], $row[2]); |
89
|
|
|
|
90
|
|
|
if (!$input->getOption('check-only')) { |
91
|
|
|
$this->em->persist($user); |
92
|
|
|
$this->em->flush($user); |
93
|
|
|
$this->em->detach($user); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) { |
97
|
|
|
$output->writeln('@' . $row[1] . ' added'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$count++; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$output->writeln($count . ' users imported.'); |
104
|
|
|
|
105
|
|
|
return 0; |
106
|
|
|
} |
107
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..