ImportProcessor::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Processor;
12
13
use BitBag\SyliusCmsPlugin\Exception\ImportFailedException;
14
use BitBag\SyliusCmsPlugin\Importer\ImporterChainInterface;
15
use BitBag\SyliusCmsPlugin\Reader\ReaderInterface;
16
use Doctrine\ORM\EntityManagerInterface;
17
18
final class ImportProcessor implements ImportProcessorInterface
19
{
20
    /** @var ImporterChainInterface */
21
    private $importerChain;
22
23
    /** @var ReaderInterface */
24
    private $reader;
25
26
    /** @var EntityManagerInterface */
27
    private $entityManager;
28
29
    public function __construct(
30
        ImporterChainInterface $importerChain,
31
        ReaderInterface $reader,
32
        EntityManagerInterface $entityManager
33
    ) {
34
        $this->importerChain = $importerChain;
35
        $this->reader = $reader;
36
        $this->entityManager = $entityManager;
37
    }
38
39
    public function process(string $resourceCode, string $filePath): void
40
    {
41
        $importer = $this->importerChain->getImporterForResource($resourceCode);
42
        $data = $this->reader->read($filePath);
43
44
        foreach ($data as $index => $row) {
45
            try {
46
                $importer->import($row);
47
            } catch (\Exception $exception) {
48
                $index += 1;
49
50
                throw new ImportFailedException($exception->getMessage(), $index);
51
            }
52
53
            $this->entityManager->clear();
54
        }
55
56
        $importer->cleanup();
57
    }
58
}
59