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