1
|
|
|
<?php namespace Simondubois\UnsplashDownloader; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A proxy to handle history operations like : |
7
|
|
|
* - loading history from file |
8
|
|
|
* - checking existence of a entity in history |
9
|
|
|
* - appending data to history |
10
|
|
|
* - saving history to file |
11
|
|
|
*/ |
12
|
|
|
class History |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
// |
16
|
|
|
// Attributes |
17
|
|
|
// |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Has a file content been loaded |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
private $loaded = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Path of the file to use for history |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $path; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* History content |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $content = []; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
|
39
|
|
|
// |
40
|
|
|
// Getters |
41
|
|
|
// |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get path attribute |
45
|
|
|
* @return string Path to file |
46
|
|
|
*/ |
47
|
2 |
|
public function getPath() |
48
|
|
|
{ |
49
|
2 |
|
return $this->path; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get content attribute |
55
|
|
|
* @return array Content data |
56
|
|
|
*/ |
57
|
4 |
|
public function getContent() |
58
|
|
|
{ |
59
|
4 |
|
return $this->content; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
// |
65
|
|
|
// File handling |
66
|
|
|
// |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Read and parse file content |
70
|
|
|
* @param string|null $path Path to file |
71
|
|
|
* @return bool True if the file has been successfully loaded |
72
|
|
|
*/ |
73
|
6 |
|
public function load($path) { |
74
|
6 |
|
if ($this->loaded) { |
75
|
1 |
|
throw new Exception('The file '.$this->path.' has already been loaded into history'); |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
$this->path = $path; |
79
|
|
|
|
80
|
6 |
|
if ($this->loadContent()) { |
81
|
3 |
|
$this->loaded = true; |
82
|
3 |
|
} |
83
|
|
|
|
84
|
6 |
|
return $this->loaded; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Save content to file |
89
|
|
|
* @return bool True if the file has been successfully saved |
90
|
|
|
*/ |
91
|
3 |
|
public function save() { |
92
|
3 |
|
if ($this->loaded === false) { |
93
|
2 |
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return $this->saveContent(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
|
101
|
|
|
// |
102
|
|
|
// Content handling |
103
|
|
|
// |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Check if a string is in history |
107
|
|
|
* @param string $str String to look for |
108
|
|
|
* @return bool True if the string has been found in history |
109
|
|
|
*/ |
110
|
2 |
|
public function has($str) |
111
|
|
|
{ |
112
|
2 |
|
return $this->loaded && in_array($str, $this->content); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Append a string to the history |
117
|
|
|
* @param string $str String to add to history |
118
|
|
|
*/ |
119
|
1 |
|
public function put($str) |
120
|
|
|
{ |
121
|
1 |
|
$this->content[] = $str; |
122
|
1 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Load file content into content attribute |
126
|
|
|
* @return bool True if the file has been successfully loaded |
127
|
|
|
*/ |
128
|
6 |
|
private function loadContent() { |
129
|
6 |
|
$content = @file($this->path, FILE_IGNORE_NEW_LINES); |
130
|
|
|
|
131
|
6 |
|
if ($content === false) { |
132
|
3 |
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
3 |
|
$this->content = $content; |
136
|
|
|
|
137
|
3 |
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Save content attribute into file |
142
|
|
|
* @return bool True if the file has been successfully saved |
143
|
|
|
*/ |
144
|
1 |
|
private function saveContent() { |
145
|
1 |
|
$content = implode(PHP_EOL, $this->content); |
146
|
1 |
|
$writtenBytes = file_put_contents($this->path, $content); |
147
|
|
|
|
148
|
1 |
|
return is_int($writtenBytes); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|