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\FileLoadedItem; |
11
|
|
|
use Oliverde8\Component\PhpEtl\Item\ItemInterface; |
12
|
|
|
use Oliverde8\Component\PhpEtl\Item\MixItem; |
13
|
|
|
use Oliverde8\Component\PhpEtl\Item\StopItem; |
14
|
|
|
use Oliverde8\Component\PhpEtl\Load\File\FileWriterInterface; |
15
|
|
|
use Oliverde8\Component\PhpEtl\Model\ExecutionContext; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FileWriter |
19
|
|
|
* |
20
|
|
|
* @author de Cramer Oliver<[email protected]> |
21
|
|
|
* @copyright 2022 Oliverde8 |
22
|
|
|
* @package Oliverde8\Component\PhpEtl\ChainOperation\Loader |
23
|
|
|
*/ |
24
|
|
|
class FileWriterOperation extends AbstractChainOperation implements DataChainOperationInterface |
25
|
|
|
{ |
26
|
|
|
/** @var FileWriterInterface */ |
27
|
1 |
|
protected $writer; |
28
|
|
|
|
29
|
1 |
|
protected string $fileName; |
30
|
1 |
|
|
31
|
|
|
public function __construct(FileWriterInterface $writer, string $fileName) |
32
|
|
|
{ |
33
|
|
|
$this->writer = $writer; |
34
|
|
|
$this->fileName = $fileName; |
35
|
|
|
} |
36
|
1 |
|
|
37
|
|
|
/** |
38
|
1 |
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
1 |
|
public function processData(DataItemInterface $item, ExecutionContext $context): DataItemInterface |
41
|
|
|
{ |
42
|
|
|
$this->writer->write($item->getData()); |
43
|
|
|
|
44
|
|
|
return $item; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function processStop(StopItem $stopItem, ExecutionContext $context): ItemInterface |
48
|
|
|
{ |
49
|
|
|
$resource = $this->writer->getResource(); |
50
|
|
|
|
51
|
|
|
$meta_data = stream_get_meta_data($resource); |
52
|
|
|
$filename = $meta_data["uri"]; |
53
|
|
|
|
54
|
|
|
$context->getFileSystem()->writeStream($this->fileName, fopen($filename, 'r')); |
55
|
|
|
|
56
|
|
|
return new MixItem([new FileLoadedItem($this->fileName), $stopItem]); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|