GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#194)
by
unknown
02:24
created

FileCache::writeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the php-phantomjs.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
namespace JonnyW\PhantomJs\Cache;
10
11
use JonnyW\PhantomJs\StringUtils;
12
use JonnyW\PhantomJs\Exception\NotWritableException;
13
use JonnyW\PhantomJs\Exception\NotExistsException;
14
15
/**
16
 * PHP PhantomJs
17
 *
18
 * @author Jon Wenmoth <[email protected]>
19
 */
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 21
    public function __construct($directory, $extension)
46
    {
47 21
        $this->directory = rtrim($directory, DIRECTORY_SEPARATOR);
48 21
        $this->extension = $extension;
49 21
    }
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 55
    public function save($id, $data)
61
    {
62 55
        $file = $this->getFilename($id);
63
64 54
        if (!$this->isWritable($file)) {
65 1
            throw new NotWritableException(sprintf('File could not be written to system as target is not writable: %s', $file));
66
        }
67
68 53
        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 53
        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 33
    public function fetch($id)
87
    {
88 33
        $file = $this->getFilename($id);
89
90 33
        if (!$this->exists($id)) {
91 1
            throw new NotExistsException(sprintf('Could not fetch data from file as file does not exist: %s', $file));
92
        }
93
94 32
        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 49
    public function delete($id)
105
    {
106 49
        $files = glob($this->getFilename($id));
107
108 49
        if (count($files)) {
109 49
            array_map('unlink', $files);
110
        }
111 49
    }
112
113
    /**
114
     * Data exists in storage.
115
     *
116
     * @access public
117
     * @param  string  $id
118
     * @return boolean
119
     */
120 45
    public function exists($id)
121
    {
122 45
        return (bool) (file_exists($this->getFilename($id)));
123
    }
124
125
    /**
126
     * Is data writeable.
127
     *
128
     * @access protected
129
     * @param $file
130
     * @return boolean
131
     */
132 54
    protected function isWritable($file)
133
    {
134 54
        return (bool) ((file_exists($file) && is_writable($file)) || (!file_exists($file) && is_writable(dirname($file))));
135
    }
136
137
    /**
138
     * Write data to file.
139
     *
140
     * @access protected
141
     * @param  string  $file
142
     * @param  string  $data
143
     * @return boolean
144
     */
145 53
    protected function writeData($file, $data)
146
    {
147 53
        return file_put_contents($file, $data);
148
    }
149
150
    /**
151
     * Read data from file.
152
     *
153
     * @access protected
154
     * @param  string $file
155
     * @return mixed
156
     */
157 32
    protected function readData($file)
158
    {
159 32
        return file_get_contents($file);
160
    }
161
162
    /**
163
     * Get filename
164
     *
165
     * @access protected
166
     * @param  string                                           $id
167
     * @return string
168
     * @throws \JonnyW\PhantomJs\Exception\NotWritableException
169
     */
170 58
    protected function getFileName($id)
171
    {
172 58
        if (is_dir($id)) {
173 1
            return sprintf('%1$s/%2$s.%3$s', rtrim($id, DIRECTORY_SEPARATOR), StringUtils::random(20), $this->extension);
174
        }
175
176 57
        $dirName = dirname($id);
177
178 57
        if (!file_exists($id) && $dirName === '.') {
179 55
             return sprintf('%1$s/%2$s', $this->directory, $id);
180
        }
181
182 49
        if (!file_exists($id) && !is_writable($dirName)) {
183 1
            throw new NotWritableException(sprintf('File could not be written to system as target is not writable: %s', $id));
184
        }
185
186 48
        return $id;
187
    }
188
}
189