AbstractFilesManipulatorTask::file()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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