Completed
Push — master ( eb9ee0...7c81ae )
by
unknown
02:37 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 Doctrine\Persistence\ManagerRegistry;
17
use Sonata\Doctrine\Model\ManagerInterface;
18
use Sonata\MediaBundle\Provider\Pool;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * @final since sonata-project/media-bundle 3.21.0
25
 */
26
class AddMassMediaCommand extends BaseCommand
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\MediaBundle\Command\BaseCommand has been deprecated with message: since sonata-project/media-bundle 3.x, to be removed in 4.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
27
{
28
    /**
29
     * @var ManagerRegistry|null
30
     */
31
    protected $managerRegistry;
32
33
    /**
34
     * @var string[]
35
     */
36
    protected $setters;
37
38
    public function __construct(ManagerInterface $mediaManager, Pool $pool, ?ManagerRegistry $managerRegistry = null)
39
    {
40
        parent::__construct($mediaManager, $pool);
41
42
        $this->managerRegistry = $managerRegistry;
43
    }
44
45
    public function configure(): void
46
    {
47
        $this->setName('sonata:media:add-multiple')
48
            ->setDescription('Add medias in mass into the database')
49
            ->setDefinition([
50
                new InputOption('file', null, InputOption::VALUE_OPTIONAL, 'The file to parse'),
51
                new InputOption('delimiter', null, InputOption::VALUE_OPTIONAL, 'Set the field delimiter (one character only)', ','),
52
                new InputOption('enclosure', null, InputOption::VALUE_OPTIONAL, 'Set the field enclosure character (one character only).', '"'),
53
                new InputOption('escape', null, InputOption::VALUE_OPTIONAL, 'Set the escape character (one character only). Defaults as a backslash', '\\'),
54
            ]);
55
    }
56
57
    public function execute(InputInterface $input, OutputInterface $output): int
58
    {
59
        $fp = $this->getFilePointer($input, $output);
60
        $imported = -1;
61
62
        while (!feof($fp)) {
63
            $data = fgetcsv($fp, null, $input->getOption('delimiter'), $input->getOption('enclosure'), $input->getOption('escape'));
64
65
            if (-1 === $imported) {
66
                $this->setters = $data;
67
68
                ++$imported;
69
70
                continue;
71
            }
72
73
            if (!\is_array($data)) {
74
                continue;
75
            }
76
77
            ++$imported;
78
79
            $this->insertMedia($data, $output);
80
            $this->optimize();
81
        }
82
83
        $output->writeln('Done!');
84
85
        return 0;
86
    }
87
88
    /**
89
     * @return resource
90
     */
91
    protected function getFilePointer(InputInterface $input, OutputInterface $output)
92
    {
93
        if (false !== ftell(STDIN)) {
94
            return STDIN;
95
        }
96
97
        if (!$input->getOption('file')) {
98
            throw new \RuntimeException('Please provide a CSV file argument or CSV input stream');
99
        }
100
101
        return fopen($input->getOption('file'), 'r');
102
    }
103
104
    protected function insertMedia(array $data, OutputInterface $output): void
105
    {
106
        $media = $this->getMediaManager()->create();
107
108
        foreach ($this->setters as $pos => $name) {
109
            $media->{'set'.$name}($data[$pos]);
110
        }
111
112
        try {
113
            $this->getMediaManager()->save($media);
114
            $output->writeln(sprintf(' > %s - %s', $media->getId(), $media->getName()));
115
        } catch (\Exception $e) {
116
            $output->writeln(sprintf('<error>%s</error> : %s', $e->getMessage(), json_encode($data)));
117
        }
118
    }
119
120
    protected function optimize(): void
121
    {
122
        if (null !== $this->managerRegistry) {
123
            $this->managerRegistry->getManager()->getUnitOfWork()->clear();
124
        }
125
    }
126
}
127