Total Complexity | 8 |
Total Lines | 55 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
2 | trait FileTrait |
||
3 | { |
||
4 | /** |
||
5 | * This function takes care of actually saving the user data to a JSON file. |
||
6 | * @param String $userId |
||
7 | * @param array $data |
||
8 | */ |
||
9 | protected function _saveUser($userId, $data) |
||
16 | } |
||
17 | |||
18 | /** |
||
19 | * This function takes care of loading the user data from a JSON file. |
||
20 | * |
||
21 | * @param string $userId |
||
22 | * @param boolean $failIfNotFound |
||
23 | * |
||
24 | * @return false if the data is not present, or an array containing the data. |
||
25 | * |
||
26 | * @throws Exception when the data can not be found and failIfNotFound is set to true |
||
27 | */ |
||
28 | protected function _loadUser($userId, $failIfNotFound = TRUE) |
||
29 | { |
||
30 | $fileName = $this->getPath().$userId.".json"; |
||
31 | |||
32 | $data = NULL; |
||
33 | if (file_exists($fileName)) { |
||
34 | $data = json_decode(file_get_contents($this->getPath().$userId.".json"), true); |
||
35 | } |
||
36 | |||
37 | if ($data === NULL) { |
||
38 | if ($failIfNotFound) { |
||
39 | throw new Exception('Error loading data for user: ' . var_export($userId, TRUE)); |
||
40 | } else { |
||
41 | $this->logger->error('Error loading data for user from user storage (file storage)'); |
||
42 | return false; |
||
43 | } |
||
44 | } else { |
||
45 | return $data; |
||
46 | } |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Retrieve the path where the json files are stored. |
||
51 | * @return String |
||
52 | */ |
||
53 | public function getPath() |
||
57 | } |
||
58 | } |
||
59 |