Completed
Push — master ( 28d05b...16bc73 )
by Raphaël
02:44
created

FileChangesHelper::getJsonContentFromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Wabel\CertainAPI\Helpers;
4
5
6
class FileChangesHelper
7
{
8
    /**
9
     * Create the directory if it doesn't exist. Right:0775
10
     * @param $dirPath
11
     * @return bool
12
     */
13
    public static function createDirectory($dirPath){
14
        if(!file_exists($dirPath)){
15
            return mkdir($dirPath, 0775, true, null);
16
        }
17
        return false;
18
    }
19
20
    /**
21
     * Write the file. The content is already in json.
22
     * @param string $path
23
     * @param string $msg
24
     * @return void
25
     */
26
    public static function writeFile($path,$msg){
27
        $f = fopen($path, "w+");
28
        fwrite($f, $msg);
29
        fclose($f);
30
    }
31
32
    /**
33
     * Save appointments with the timestamp in filename.
34
     * @param string $filePath
35
     * @param string $contents
36
     * @return void
37
     */
38
    public static function saveAppointmentsFileByHistory($filePath,$contents){
39
        $time = time();
40
        $file = pathinfo($filePath);
41
        $fileNamebase = $file['filename'];
42
        $filePath = $file['dirname'].'/'.$fileNamebase.'_'.$time.'.json';
43
        if(!file_exists($filePath)){
44
            self::writeFile($filePath,'');
45
        }
46
        self::writeFile($filePath,$contents);
47
    }
48
49
    /**
50
     * Get the content from a file.
51
     * @param $path
52
     * @return string
53
     */
54
    public static function getContentFile($path){
55
        if(!file_exists($path)){
56
            self::writeFile($path,'');
57
        }
58
        $handle = fopen($path, "rb");
59
        if (FALSE === $handle) {
60
            $handle = fopen($path, "rb");
61
        }
62
        $contents = '';
63
        while (!feof($handle)) {
64
            $contents .= fread($handle, 8192);
65
        }
66
        fclose($handle);
67
        return $contents;
68
    }
69
70
    /**
71
     * Get the content from a json file by decoding.
72
     * @param string $path
73
     * @return mixed
74
     */
75
    public static function getJsonContentFromFile($path){
76
        $contents = self::getContentFile($path);
77
        return json_decode($contents, true);
78
    }
79
80
    /**
81
     * Get an  file list of appointments by eventCode
82
     * @param string $eventCode
83
     * @param string $pathDir
84
     * @return array
85
     */
86
    public static function getFilesListHistoryAppointmentsByEventCode($eventCode,$pathDir){
87
        $fileList = array();
88
        $files = glob($pathDir.'/appointments_'.$eventCode.'_*.json');
89
        foreach ($files as $file) {
90
            $fileList[filemtime($file)] = $file;
91
        }
92
        ksort($fileList);
93
        $fileList = array_reverse($fileList, TRUE);
94
        return $fileList;
95
    }
96
97
    /**
98
     * Get the last list of appointments.
99
     * @param string $eventCode
100
     * @param string $pathDir
101
     * @return string
102
     */
103
    public static function getTheLastAppointmentsSaved($eventCode,$pathDir){
104
        $fileList = self::getFilesListHistoryAppointmentsByEventCode($eventCode,$pathDir);
105
        return array_shift($fileList);
106
    }
107
}