Passed
Pull Request — master (#30)
by De Cramer
08:52
created

ExternalFileProcessorOperation::processFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 17
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 24
rs 9.7
1
<?php
2
declare(strict_types=1);
3
4
namespace Oliverde8\Component\PhpEtl\ChainOperation\Transformer;
5
6
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation;
7
use Oliverde8\Component\PhpEtl\Item\DataItem;
8
use Oliverde8\Component\PhpEtl\Item\ExternalFileItem;
9
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
10
use Oliverde8\Component\PhpEtl\Item\MixItem;
11
use Oliverde8\Component\PhpEtl\Model\ExecutionContext;
12
13
class ExternalFileProcessorOperation extends AbstractChainOperation
14
{
15
    public function processFile(ExternalFileItem $item, ExecutionContext $context): ItemInterface
16
    {
17
        $externalFilePath = $item->getFilePath();
18
        $externalDir = dirname($externalFilePath);
19
        $fileName = basename($externalFilePath);
20
        $externalFileSystem = $item->getFileSystem();
21
        $localFileSystem = $context->getFileSystem();
22
23
        if ($item->getState() == ExternalFileItem::STATE_NEW) {
24
            var_dump("EXECUTE");
0 ignored issues
show
Security Debugging Code introduced by
var_dump('EXECUTE') looks like debug code. Are you sure you do not want to remove it?
Loading history...
25
            // Move file to prevent it to be processed by another process.
26
            $externalFileSystem->createDirectory($externalDir . "/processing");
27
            $externalFileSystem->move($externalFilePath, $externalDir . "/processing/" . $fileName);
28
29
            $localFileSystem->writeStream($fileName, $externalFileSystem->readStream($externalDir . "/processing/" . $fileName));
30
31
            $item->setState(ExternalFileItem::STATE_PROCESSING);
32
            return new MixItem([new DataItem($fileName), $item]);
33
        } else {
34
            $externalFileSystem->createDirectory($externalDir . "/processed");
35
            $externalFileSystem->move($externalDir . "/processing/" . $fileName, $externalDir . "/processed/" . $fileName);
36
37
            $item->setState(ExternalFileItem::STATE_PROCESSED);
38
            return $item;
39
        }
40
    }
41
}