Passed
Push — master ( 32dee0...5d2de3 )
by Andrea Marco
13:08 queued 11s
created

ManipulatedFile   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 127
ccs 0
cts 22
cp 0
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A needsManualUpdateTo() 0 5 1
A getOriginalContent() 0 3 1
A needsManualUpdate() 0 3 1
A getRelativePath() 0 5 1
A wasUpdated() 0 3 1
A getManualUpdateReason() 0 3 1
A __construct() 0 5 2
A getPath() 0 3 1
A wasCreated() 0 3 1
1
<?php
2
3
namespace Cerbero\ConsoleTasker;
4
5
use Cerbero\ConsoleTasker\Traits\AddsLines;
6
use Illuminate\Container\Container;
7
8
/**
9
 * The manipulated file.
10
 *
11
 */
12
class ManipulatedFile
13
{
14
    use AddsLines;
15
16
    /**
17
     * The file path.
18
     *
19
     * @var string
20
     */
21
    protected $path;
22
23
    /**
24
     * Whether the file was created.
25
     *
26
     * @var bool
27
     */
28
    protected $wasCreated;
29
30
    /**
31
     * The original content.
32
     *
33
     * @var string|null
34
     */
35
    protected $originalContent;
36
37
    /**
38
     * The reason why the file needs to be updated manually.
39
     *
40
     * @var string|null
41
     */
42
    protected $manualUpdateReason;
43
44
    /**
45
     * Instantiate the class.
46
     *
47
     * @param string $path
48
     */
49
    public function __construct(string $path)
50
    {
51
        $this->path = $path;
52
        $this->wasCreated = !file_exists($path);
53
        $this->originalContent = $this->wasCreated ? null : file_get_contents($path);
54
    }
55
56
    /**
57
     * Retrieve the file path
58
     *
59
     * @return string
60
     */
61
    public function getPath(): string
62
    {
63
        return $this->path;
64
    }
65
66
    /**
67
     * Retrieve the file relative path
68
     *
69
     * @return string
70
     */
71
    public function getRelativePath(): string
72
    {
73
        $basePath = Container::getInstance()->make('path.base');
74
75
        return substr($this->path, strlen($basePath) + 1);
76
    }
77
78
    /**
79
     * Determine whether the file was created
80
     *
81
     * @return bool
82
     */
83
    public function wasCreated(): bool
84
    {
85
        return $this->wasCreated;
86
    }
87
88
    /**
89
     * Determine whether the file was updated
90
     *
91
     * @return bool
92
     */
93
    public function wasUpdated(): bool
94
    {
95
        return !$this->wasCreated();
96
    }
97
98
    /**
99
     * Retrieve the original content
100
     *
101
     * @return string|null
102
     */
103
    public function getOriginalContent(): ?string
104
    {
105
        return $this->originalContent;
106
    }
107
108
    /**
109
     * Retrieve the reason why the file needs to be updated manually
110
     *
111
     * @return string|null
112
     */
113
    public function getManualUpdateReason(): ?string
114
    {
115
        return $this->manualUpdateReason;
116
    }
117
118
    /**
119
     * Set the reason why the file needs to be updated manually
120
     *
121
     * @param string $reason
122
     * @return self
123
     */
124
    public function needsManualUpdateTo(string $reason): self
125
    {
126
        $this->manualUpdateReason = $reason;
127
128
        return $this;
129
    }
130
131
    /**
132
     * Determine whether the file needs to be updated manually
133
     *
134
     * @return bool
135
     */
136
    public function needsManualUpdate(): bool
137
    {
138
        return isset($this->manualUpdateReason);
139
    }
140
}
141