Completed
Push — master ( bb6905...021686 )
by Grégoire
18s queued 12s
created

src/Command/AddMassMediaCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Command;
15
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * @final since sonata-project/media-bundle 3.21.0
22
 */
23
class AddMassMediaCommand extends BaseCommand
24
{
25
    /**
26
     * @var string[]
27
     */
28
    protected $setters;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function configure(): void
34
    {
35
        $this->setName('sonata:media:add-multiple')
36
            ->setDescription('Add medias in mass into the database')
37
            ->setDefinition([
38
                new InputOption('file', null, InputOption::VALUE_OPTIONAL, 'The file to parse'),
39
                new InputOption('delimiter', null, InputOption::VALUE_OPTIONAL, 'Set the field delimiter (one character only)', ','),
40
                new InputOption('enclosure', null, InputOption::VALUE_OPTIONAL, 'Set the field enclosure character (one character only).', '"'),
41
                new InputOption('escape', null, InputOption::VALUE_OPTIONAL, 'Set the escape character (one character only). Defaults as a backslash', '\\'),
42
            ]);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function execute(InputInterface $input, OutputInterface $output): void
49
    {
50
        $fp = $this->getFilePointer($input, $output);
51
        $imported = -1;
52
53
        while (!feof($fp)) {
54
            $data = fgetcsv($fp, null, $input->getOption('delimiter'), $input->getOption('enclosure'), $input->getOption('escape'));
55
56
            if (-1 === $imported) {
57
                $this->setters = $data;
58
59
                ++$imported;
60
61
                continue;
62
            }
63
64
            if (!\is_array($data)) {
65
                continue;
66
            }
67
68
            ++$imported;
69
70
            $this->insertMedia($data, $output);
71
            $this->optimize();
72
        }
73
74
        $output->writeln('Done!');
75
    }
76
77
    /**
78
     * @return resource
79
     */
80
    protected function getFilePointer(InputInterface $input, OutputInterface $output)
0 ignored issues
show
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        if (false !== ftell(STDIN)) {
83
            return STDIN;
84
        }
85
86
        if (!$input->getOption('file')) {
87
            throw new \RuntimeException('Please provide a CSV file argument or CSV input stream');
88
        }
89
90
        return fopen($input->getOption('file'), 'r');
91
    }
92
93
    protected function insertMedia(array $data, OutputInterface $output): void
94
    {
95
        $media = $this->getMediaManager()->create();
96
97
        foreach ($this->setters as $pos => $name) {
98
            $media->{'set'.$name}($data[$pos]);
99
        }
100
101
        try {
102
            $this->getMediaManager()->save($media);
103
            $output->writeln(sprintf(' > %s - %s', $media->getId(), $media->getName()));
104
        } catch (\Exception $e) {
105
            $output->writeln(sprintf('<error>%s</error> : %s', $e->getMessage(), json_encode($data)));
106
        }
107
    }
108
109
    protected function optimize(): void
110
    {
111
        if ($this->getContainer()->has('doctrine')) {
112
            $this->getContainer()->get('doctrine')->getManager()->getUnitOfWork()->clear();
113
        }
114
    }
115
}
116