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.

FileCache::fetch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
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 22
    public function __construct($directory, $extension)
46
    {
47 22
        $this->directory = rtrim($directory, DIRECTORY_SEPARATOR);
48 22
        $this->extension = $extension;
49 22
    }
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 57
    public function save($id, $data)
61
    {
62 57
        $file = $this->getFilename($id);
63
64 56
        if (!$this->isWritable($file)) {
65 2
            throw new NotWritableException(sprintf('File could not be written to system as target is not writable: %s', $file));
66
        }
67
68 54
        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 54
        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 50
    public function delete($id)
105
    {
106 50
        $files = glob($this->getFilename($id));
107
108 50
        if (count($files)) {
109 50
            array_map('unlink', $files);
110 50
        }
111 50
    }
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 56
    protected function isWritable($file)
133
    {
134 56
        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 54
    protected function writeData($file, $data)
146
    {
147 54
        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 60
    protected function getFileName($id)
171
    {
172 60
        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 59
        $dirName = dirname($id);
177
178 59
        if (!file_exists($id) && $dirName === '.') {
179 57
             return sprintf('%1$s/%2$s', $this->directory, $id);
180
        }
181
182 50
        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 49
        return $id;
187
    }
188
}
189