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

ExternalFileProcessorOperation   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 26
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A processFile() 0 24 2
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
}