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

ExternalFileFinderOperation::processData()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 17
rs 9.9666
1
<?php
2
declare(strict_types=1);
3
4
namespace Oliverde8\Component\PhpEtl\ChainOperation\Extract;
5
6
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation;
7
use Oliverde8\Component\PhpEtl\ChainOperation\DataChainOperationInterface;
8
use Oliverde8\Component\PhpEtl\Item\DataItemInterface;
9
use Oliverde8\Component\PhpEtl\Item\ExternalFileItem;
10
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
11
use Oliverde8\Component\PhpEtl\Item\MixItem;
12
use Oliverde8\Component\PhpEtl\Model\ExecutionContext;
13
use Oliverde8\Component\PhpEtl\Model\File\FileSystemInterface;
14
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
15
16
class ExternalFileFinderOperation extends AbstractChainOperation implements DataChainOperationInterface
17
{
18
    protected FileSystemInterface $fileSystem;
19
20
    protected string $directory;
21
22
    private ExpressionLanguage $expressionLanguage;
23
24
25
    public function __construct(FileSystemInterface $fileSystem, string $directory)
26
    {
27
        $this->fileSystem = $fileSystem;
28
        $this->directory = $directory;
29
30
        $this->expressionLanguage = new ExpressionLanguage();
31
32
    }
33
34
    function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
    {
36
        $pattern = $item->getData();
37
        $files = [];
38
39
        if (strpos($this->directory, "@") === 0) {
40
            $directory = ltrim($this->directory, '@');
41
            $directory = $this->expressionLanguage->evaluate($directory, ['context' => $context->getParameters()]);
42
        }
43
44
        foreach ($this->fileSystem->listContents($directory) as $file) {
45
            if (preg_match($pattern, $file) !== 0) {
46
                $files[] = new ExternalFileItem($directory . "/" . $file, $this->fileSystem);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $directory does not seem to be defined for all execution paths leading up to this point.
Loading history...
47
            }
48
        }
49
50
        return new MixItem($files);
51
    }
52
}
53