Passed
Push — master ( 38bb21...58e144 )
by De Cramer
08:33
created

FileWriterOperation::processStop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Oliverde8\Component\PhpEtl\ChainOperation\Loader;
6
7
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation;
8
use Oliverde8\Component\PhpEtl\ChainOperation\DataChainOperationInterface;
9
use Oliverde8\Component\PhpEtl\Item\DataItemInterface;
10
use Oliverde8\Component\PhpEtl\Item\StopItem;
11
use Oliverde8\Component\PhpEtl\Load\File\FileWriterInterface;
12
use Oliverde8\Component\PhpEtl\Model\ExecutionContext;
13
14
/**
15
 * Class FileWriter
16
 *
17
 * @author    de Cramer Oliver<[email protected]>
18
 * @copyright 2022 Oliverde8
19
 * @package Oliverde8\Component\PhpEtl\ChainOperation\Loader
20
 */
21
class FileWriterOperation extends AbstractChainOperation implements DataChainOperationInterface
22
{
23
    /** @var FileWriterInterface */
24
    protected $writer;
25
26
    protected string $fileName;
27 1
28
    public function __construct(FileWriterInterface $writer, string $fileName)
29 1
    {
30 1
        $this->writer = $writer;
31
        $this->fileName = $fileName;
32
    }
33
34
    /**
35
     * @inheritdoc
36 1
     */
37
    public function processData(DataItemInterface $item, ExecutionContext $context): DataItemInterface
38 1
    {
39
        $this->writer->write($item->getData());
40 1
41
        return $item;
42
    }
43
44
    public function processStop(StopItem $stopItem, ExecutionContext $context): StopItem
45
    {
46
        $resource = $this->writer->getResource();
47
48
        $meta_data = stream_get_meta_data($resource);
49
        $filename = $meta_data["uri"];
50
51
        $context->getFileSystem()->writeStream($this->fileName, fopen($filename, 'r'));
52
53
        return $stopItem;
54
    }
55
}
56