ExternalFileFinderOperation   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 36
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A processData() 0 18 4
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
        $directory = $this->directory;
40
        if (strpos($this->directory, "@") === 0) {
41
            $directory = ltrim($this->directory, '@');
42
            $directory = $this->expressionLanguage->evaluate($directory, ['context' => $context->getParameters()]);
43
        }
44
45
        foreach ($this->fileSystem->listContents($directory) as $file) {
0 ignored issues
show
Bug introduced by
It seems like $directory can also be of type array and array; however, parameter $path of Oliverde8\Component\PhpE...terface::listContents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        foreach ($this->fileSystem->listContents(/** @scrutinizer ignore-type */ $directory) as $file) {
Loading history...
46
            if (preg_match($pattern, $file) !== 0) {
47
                $files[] = new ExternalFileItem($directory . "/" . $file, $this->fileSystem);
0 ignored issues
show
Bug introduced by
Are you sure $directory of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
                $files[] = new ExternalFileItem(/** @scrutinizer ignore-type */ $directory . "/" . $file, $this->fileSystem);
Loading history...
48
            }
49
        }
50
51
        return new MixItem($files);
52
    }
53
}
54