1 | <?php |
||
20 | class FileCache implements CacheInterface |
||
21 | { |
||
22 | /** |
||
23 | * Default write directory |
||
24 | * |
||
25 | * @var string |
||
26 | * @access protected |
||
27 | */ |
||
28 | protected $directory; |
||
29 | |||
30 | /** |
||
31 | * Default write extension |
||
32 | * |
||
33 | * @var string |
||
34 | * @access protected |
||
35 | */ |
||
36 | protected $extension; |
||
37 | |||
38 | /** |
||
39 | * Internal constructor. |
||
40 | * |
||
41 | * @access public |
||
42 | * @param string $directory |
||
43 | * @param string $extension |
||
44 | */ |
||
45 | 1 | public function __construct($directory, $extension) |
|
50 | |||
51 | /** |
||
52 | * Write data to storage. |
||
53 | * |
||
54 | * @access public |
||
55 | * @param string $id |
||
56 | * @param string $data |
||
57 | * @return string |
||
58 | * @throws \JonnyW\PhantomJs\Exception\NotWritableException |
||
59 | */ |
||
60 | 27 | public function save($id, $data) |
|
61 | { |
||
62 | 27 | $file = $this->getFilename($id); |
|
63 | |||
64 | 27 | if (!$this->isWritable($file)) { |
|
65 | throw new NotWritableException(sprintf('File could not be written to system as target is not writable: %s', $file)); |
||
66 | } |
||
67 | |||
68 | 27 | if ($this->writeData($file, $data) === false) { |
|
69 | |||
70 | $this->delete($file); |
||
71 | |||
72 | throw new NotWritableException(sprintf('Data could not be written to file on system. Please make sure that file is writeable: %s', $file)); |
||
73 | } |
||
74 | |||
75 | 27 | return $file; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Fetch data from file. |
||
80 | * |
||
81 | * @access public |
||
82 | * @param string $id |
||
83 | * @return mixed|void |
||
84 | * @throws \JonnyW\PhantomJs\Exception\NotExistsException |
||
85 | */ |
||
86 | 21 | public function fetch($id) |
|
87 | { |
||
88 | 21 | $file = $this->getFilename($id); |
|
89 | |||
90 | 21 | if (!$this->exists($id)) { |
|
91 | throw new NotExistsException(sprintf('Could not fetch data from file as file does not exist: %s', $file)); |
||
92 | } |
||
93 | |||
94 | 21 | return $this->readData($file); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Delete data from storage. |
||
99 | * |
||
100 | * @access public |
||
101 | * @param string $id |
||
102 | * @return void |
||
103 | */ |
||
104 | 27 | public function delete($id) |
|
112 | |||
113 | /** |
||
114 | * Data exists in storage. |
||
115 | * |
||
116 | * @access public |
||
117 | * @param string $id |
||
118 | * @return boolean |
||
119 | */ |
||
120 | 27 | public function exists($id) |
|
124 | |||
125 | /** |
||
126 | * Is data writeable. |
||
127 | * |
||
128 | * @access protected |
||
129 | * @param $file |
||
130 | * @return boolean |
||
131 | */ |
||
132 | 27 | protected function isWritable($file) |
|
136 | |||
137 | /** |
||
138 | * Write data to file. |
||
139 | * |
||
140 | * @access protected |
||
141 | * @param string $file |
||
142 | * @param string $data |
||
143 | * @return boolean |
||
144 | */ |
||
145 | 27 | protected function writeData($file, $data) |
|
149 | |||
150 | /** |
||
151 | * Read data from file. |
||
152 | * |
||
153 | * @access protected |
||
154 | * @param string $file |
||
155 | * @return mixed |
||
156 | */ |
||
157 | 21 | protected function readData($file) |
|
161 | |||
162 | /** |
||
163 | * Get filename |
||
164 | * |
||
165 | * @access protected |
||
166 | * @param string $id |
||
167 | * @return string |
||
168 | * @throws \JonnyW\PhantomJs\Exception\NotWritableException |
||
169 | */ |
||
170 | 27 | protected function getFileName($id) |
|
188 | } |
||
189 |