Passed
Push — main ( bd03c4...c6ca62 )
by De Cramer
04:36
created

DeleteFilesForOldExecutionOperation::processData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 4
nc 3
nop 2
1
<?php
2
3
namespace Oliverde8\PhpEtlBundle\Etl\Operation\Cleanup;
4
5
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation;
6
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
7
use Oliverde8\PhpEtlBundle\Entity\EtlExecution;
8
use Oliverde8\PhpEtlBundle\Services\ChainWorkDirManager;
9
10
class DeleteFilesForOldExecutionOperation extends AbstractChainOperation
11
{
12
    /** @var ChainWorkDirManager */
13
    protected $chainWorkdDirManager;
14
15
    /**
16
     * DeleteFilesForOldExecutionOperation constructor.
17
     * @param ChainWorkDirManager $chainWorkdDirManager
18
     */
19
    public function __construct(ChainWorkDirManager $chainWorkdDirManager)
20
    {
21
        $this->chainWorkdDirManager = $chainWorkdDirManager;
22
    }
23
24
    protected function processData(ItemInterface $item, array &$context)
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

24
    protected function processData(ItemInterface $item, /** @scrutinizer ignore-unused */ array &$context)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        /** @var EtlExecution $entity */
27
        $entity = $item->getData();
0 ignored issues
show
Bug introduced by
The method getData() does not exist on Oliverde8\Component\PhpEtl\Item\ItemInterface. It seems like you code against a sub-type of Oliverde8\Component\PhpEtl\Item\ItemInterface such as Oliverde8\Component\PhpEtl\Item\DataItemInterface. ( Ignorable by Annotation )

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

27
        /** @scrutinizer ignore-call */ 
28
        $entity = $item->getData();
Loading history...
28
29
        $executionWorkDir = $this->chainWorkdDirManager->getWorkDir($entity);
30
        if (!file_exists($executionWorkDir)) {
31
            return $item;
32
        }
33
34
        $parentDir = dirname($executionWorkDir);
35
        $this->deleteDirectory($executionWorkDir);
36
37
        while (basename($parentDir) !== 'var' && empty($this->getDirFiles($parentDir))) {
38
            @rmdir($parentDir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

38
            /** @scrutinizer ignore-unhandled */ @rmdir($parentDir);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
            $parentDir = dirname($parentDir);
40
        }
41
42
        return $item;
43
    }
44
    
45
    protected function deleteDirectory(string $dir) {
46
        if (!file_exists($dir)) {
47
            return true;
48
        }
49
50
        if (!is_dir($dir)) {
51
            return unlink($dir);
52
        }
53
54
        foreach ($this->getDirFiles($dir) as $item) {
55
            if (!$this->deleteDirectory("$dir/$item")) {
56
                return false;
57
            }
58
        }
59
60
        return rmdir($dir);
61
    }
62
63
    protected function getDirFiles(string $dir)
64
    {
65
        if (!file_exists($dir) || !is_dir($dir)) {
66
            return [];
67
        }
68
69
        $files = [];
70
        foreach (scandir($dir) as $item) {
71
            if ($item == '.' || $item == '..') {
72
                continue;
73
            }
74
75
            $files[] =$item;
76
        }
77
78
        return $files;
79
    }
80
81
}