AbstractFilesManipulatorTask   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 42
ccs 0
cts 10
cp 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A revert() 0 7 3
A getManipulatedFiles() 0 3 1
A file() 0 3 1
1
<?php
2
3
namespace Cerbero\ConsoleTasker\Tasks;
4
5
use Cerbero\ConsoleTasker\ManipulatedFile;
6
7
/**
8
 * The abstract files manipulator task.
9
 *
10
 */
11
abstract class AbstractFilesManipulatorTask extends AbstractTask
12
{
13
    /**
14
     * The files being manipulated.
15
     *
16
     * @var ManipulatedFile[]
17
     */
18
    protected $manipulatedFiles = [];
19
20
    /**
21
     * Retrieve the files being manipulated
22
     *
23
     * @return ManipulatedFile[]
24
     */
25
    public function getManipulatedFiles(): array
26
    {
27
        return $this->manipulatedFiles;
28
    }
29
30
    /**
31
     * Retrieve an instance of manipulated file with the given path
32
     *
33
     * @param string $path
34
     * @return ManipulatedFile
35
     */
36
    protected function file(string $path): ManipulatedFile
37
    {
38
        return $this->manipulatedFiles[] = new ManipulatedFile($path);
39
    }
40
41
    /**
42
     * Revert this task
43
     *
44
     * @return void
45
     */
46
    protected function revert(): void
47
    {
48
        foreach ($this->getManipulatedFiles() as $file) {
49
            if ($file->wasCreated()) {
50
                unlink($file->getPath());
51
            } else {
52
                file_put_contents($file->getPath(), $file->getOriginalContent());
53
            }
54
        }
55
    }
56
}
57