Test Setup Failed
Push — master ( 584547...730ac2 )
by Alexey
14:04
created

ImportUsersCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 5
dl 0
loc 87
ccs 0
cts 65
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B configure() 0 24 1
C execute() 0 49 10
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $em of type object<Doctrine\ORM\EntityManagerInterface> is incompatible with the declared type object<EntityManager> of property $em.

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..

Loading history...
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
}