Completed
Push — master ( 2fe979...097c86 )
by diego
06:23
created

Resource::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 28
ccs 17
cts 17
cp 1
rs 8.5806
cc 4
eloc 17
nc 4
nop 5
crap 4
1
<?php
2
3
namespace HDNET\Importr\Processor;
4
5
use HDNET\Importr\Domain\Model\Import;
6
use HDNET\Importr\Service\ImportServiceInterface;
7
use HDNET\Importr\Service\ManagerInterface;
8
use HDNET\Importr\Service\Resources\ResourceInterface;
9
10
/**
11
 * Resource
12
 */
13
class Resource
14
{
15
    /**
16
     * @var Configuration
17
     */
18
    protected $configuration;
19
20
    /**
21
     * @var Target
22
     */
23
    protected $target;
24
25
    /**
26
     * @var ImportServiceInterface
27
     */
28
    protected $importService;
29
30
    /**
31
     * Resource constructor.
32
     * @param Configuration          $configuration
33
     * @param Target                 $target
34
     * @param ImportServiceInterface $importService
35
     */
36 3
    public function __construct(Configuration $configuration, Target $target, ImportServiceInterface $importService)
37
    {
38 3
        $this->configuration = $configuration;
39 3
        $this->target = $target;
40 3
        $this->importService = $importService;
41 3
    }
42
43
    /**
44
     * @param Import            $import
45
     * @param array             $targets
46
     * @param array             $configuration
47
     * @param ResourceInterface $resource
48
     * @param ManagerInterface  $manager
49
     * @return bool
50
     * @throws \HDNET\Importr\Exception\ReinitializeException
51
     */
52 3
    public function process(Import $import, array $targets, array $configuration, ResourceInterface $resource, ManagerInterface $manager)
53
    {
54
        // Resourcen Object anhand der Datei auswählen
55 3
        if (!preg_match($resource->getFilepathExpression(), $import->getFilepath())) {
56 1
            return false;
57
        }
58
59 2
        $this->configuration->process($configuration, $manager, 'before');
60
        // Resource "benutzen"
61 2
        $resource->parseResource();
62
        // Basis Import Aktualsieren (DB)
63 2
        $import->setAmount($resource->getAmount());
64 2
        $import->setStarttime(new \DateTime('now'));
65 2
        $this->importService->updateImport($import);
66
        // Durchlauf starten
67 2
        for ($pointer = $import->getPointer(); $pointer < $import->getAmount(); $pointer++) {
68 2
            $this->configuration->process($configuration, $manager, 'each');
69 2
            $this->processOneLine($resource, $pointer, $targets, $import);
70 2
            if (($pointer + 1) % $manager->getUpdateInterval() == 0) {
71 1
                $this->importService->updateImport($import, $pointer + 1);
72
            }
73
        }
74 2
        $import->setEndtime(new \DateTime('now'));
75 2
        $this->importService->updateImport($import, $pointer);
76 2
        $this->configuration->process($configuration, $manager, 'after');
77
78 2
        return true;
79
    }
80
81
    /**
82
     * @param ResourceInterface $resource
83
     * @param int               $pointer
84
     * @param array             $targets
85
     * @param Import            $import
86
     */
87 2
    protected function processOneLine(ResourceInterface $resource, $pointer, array $targets, Import $import)
88
    {
89 2
        $entry = $resource->getEntry($pointer);
90 2
        foreach ($targets as $target) {
91 2
            $this->target->process($target, $entry, $import, $pointer);
92
        }
93 2
    }
94
}
95