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.

File::getFileNameForId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 1
1
<?php
2
/**
3
 * File.php
4
 *
5
 * @category        AngryBytes
6
 * @package         Cache
7
 * @copyright       Copyright (c) 2010 Angry Bytes BV (http://www.angrybytes.com)
8
 */
9
10
namespace AngryBytes\Cache\Adapter;
11
12
use AngryBytes\Cache\Adapter;
13
14
use AngryBytes\Cache\ResultNotFound;
15
16
use \InvalidArgumentException as InvalidArgumentException;
17
18
/**
19
 * File
20
 *
21
 * Cache backend adapter using the file system
22
 *
23
 * @category        AngryBytes
24
 * @package         Cache
25
 */
26
class File extends Adapter
27
{
28
    /**
29
     * Directory
30
     *
31
     * @var string
32
     **/
33
    private $directory;
34
35
    /**
36
     * Constructor
37
     *
38
     * @param string $directory
39
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
40
     **/
41
    public function __construct($directory)
42
    {
43
        $this->setDirectory($directory);
44
    }
45
46
    /**
47
     * Get the cache directory
48
     *
49
     * @return string
50
     */
51
    public function getDirectory()
52
    {
53
        return $this->directory;
54
    }
55
56
    /**
57
     * Set the cache directory
58
     *
59
     * @param string $directory
60
     * @return File
61
     */
62
    public function setDirectory($directory)
63
    {
64
        // Sanity check
65
        if (!file_exists($directory)) {
66
            throw new InvalidArgumentException(
67
                '"' . $directory . '" does not exist'
68
            );
69
        }
70
71
        $this->directory = $directory;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Save some data
78
     *
79
     * @param  string $data
80
     * @param  string $id
81
     * @param  int    $lifeTime lifetime in seconds
82
     * @return bool
83
     **/
84
    public function save($data, $id, $lifeTime)
85
    {
86
        $bytes = file_put_contents(
87
            $this->getFileNameForId($id),
88
            $this->wrapLifeTime($data, $lifeTime)
89
        );
90
91
        return $bytes > 0;
92
    }
93
94
    /**
95
     * Load an item from the cache
96
     *
97
     * @param  string               $id
98
     * @return mixed|ResultNotFound
99
     **/
100
    public function load($id)
101
    {
102
        // If there's no matching file, remove
103
        if (!file_exists($this->getFileNameForId($id))) {
104
            return new ResultNotFound($id);
105
        }
106
107
        $wrappedData = file_get_contents($this->getFileNameForId($id));
108
109
        list($lifeTime, $data) = $this->unWrapLifeTime($wrappedData);
110
111
        // Invalid time
112
        if ($lifeTime < time()) {
113
114
            // Delete the item
115
            $this->delete($id);
116
117
            // Not found
118
            return new ResultNotFound($id);
119
        }
120
121
        return $data;
122
    }
123
124
    /**
125
     * Delete an item from the cache
126
     *
127
     * @param  string $id
128
     * @return bool
129
     **/
130
    public function delete($id)
131
    {
132
        if (file_exists($this->getFileNameForId($id))) {
133
            return unlink($this->getFileNameForId($id));
134
        }
135
136
        return true;
137
    }
138
139
    /**
140
     * Get the full file name for a cache id
141
     *
142
     * @param  string $id
143
     * @return string
144
     **/
145
    private function getFileNameForId($id)
146
    {
147
        return $this->getDirectory() . '/' . $id . '.cache';
148
    }
149
150
    /**
151
     * Wrap lifetime into data string
152
     *
153
     * @param string $data
154
     * @param int $lifeTime
155
     * @return string
156
     **/
157
    private function wrapLifeTime($data, $lifeTime)
158
    {
159
        return (time() + $lifeTime) . '|' . $data;
160
    }
161
162
    /**
163
     * Unwrap lifetime from data
164
     *
165
     * @param string $wrappedData
166
     * @return void
167
     **/
168
    private function unWrapLifeTime($wrappedData)
169
    {
170
        $time = substr(
171
            $wrappedData,
172
            0,
173
            strpos($wrappedData, '|')
174
        );
175
176
        $data = substr(
177
            $wrappedData,
178
            strpos($wrappedData, '|') + 1
179
        );
180
181
        return array($time, $data);
182
    }
183
}
184
185