Passed
Push — main ( 1b3827...987cb0 )
by De Cramer
03:06
created

ChainWorkDirManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 2
b 0
f 0
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getWorkDir() 0 6 1
A __construct() 0 4 1
A listFiles() 0 13 4
1
<?php
2
3
4
namespace Oliverde8\PhpEtlBundle\Services;
5
6
7
use Oliverde8\PhpEtlBundle\Entity\EtlExecution;
8
use Symfony\Component\Filesystem\Exception\IOException;
9
use Symfony\Component\Filesystem\Filesystem;
10
11
class ChainWorkDirManager
12
{
13
    /** @var string  */
14
    protected $baseDir;
15
16
    /** @var Filesystem */
17
    protected $fileSystem;
18
19
    /**
20
     * ChainWorkDirManager constructor.
21
     * @param string $directory
22
     */
23
    public function __construct(string $baseDir, Filesystem $filesystem)
24
    {
25
        $this->baseDir = $baseDir;
26
        $this->fileSystem = $filesystem;
27
    }
28
29
    /**
30
     * @param EtlExecution $execution
31
     * @return string
32
     *
33
     * @throws IOException if directory can't be created.
34
     */
35
    public function getWorkDir(EtlExecution $execution): string
36
    {
37
        $dir = $this->baseDir . "/" . $execution->getStartTime()->format("y/m/d") . "/id-" . $execution->getId() . "/";
38
        $this->fileSystem->mkdir($dir);
39
40
        return $dir;
41
    }
42
43
    public function listFiles(EtlExecution $execution): array
44
    {
45
        $files = [];
46
        $dir = $this->getWorkDir($execution);
47
48
        $handle = opendir($dir);
49
        while (false !== ($entry = readdir($handle))) {
50
            if ($entry != "." && $entry != "..") {
51
                $files[] = $entry;
52
            }
53
        }
54
55
        return $files;
56
    }
57
}