Completed
Pull Request — master (#169)
by Mikołaj
02:27
created

ImportProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Processor;
14
15
use BitBag\SyliusCmsPlugin\Exception\ImportFailedException;
16
use BitBag\SyliusCmsPlugin\Importer\ImporterChainInterface;
17
use BitBag\SyliusCmsPlugin\Reader\ReaderInterface;
18
use Doctrine\ORM\EntityManagerInterface;
19
20
final class ImportProcessor implements ImportProcessorInterface
21
{
22
    /** @var ImporterChainInterface */
23
    private $importerChain;
24
25
    /** @var ReaderInterface */
26
    private $reader;
27
28
    /** @var EntityManagerInterface */
29
    private $entityManager;
30
31
    public function __construct(
32
        ImporterChainInterface $importerChain,
33
        ReaderInterface $reader,
34
        EntityManagerInterface $entityManager
35
    ) {
36
        $this->importerChain = $importerChain;
37
        $this->reader = $reader;
38
        $this->entityManager = $entityManager;
39
    }
40
41
    public function process(string $resourceCode, string $filePath): void
42
    {
43
        $importer = $this->importerChain->getImporterForResource($resourceCode);
44
        $data = $this->reader->read($filePath);
45
46
        foreach ($data as $index => $row) {
47
            try {
48
                $importer->import($row);
49
            } catch (\Exception $exception) {
50
                throw new ImportFailedException($exception->getMessage(), ++$index);
51
            }
52
53
            $this->entityManager->clear();
54
        }
55
56
        $importer->cleanup();
57
    }
58
}
59