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

FileWriterOperation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 33
ccs 6
cts 6
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processData() 0 5 1
A processStop() 0 10 1
A __construct() 0 4 1
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