Test Failed
Push — resize ( fbaf05...026e01 )
by Mikael
02:27
created

CFastTrackCache   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 233
Duplicated Lines 3.43 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 8
loc 233
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A enable() 0 5 1
A setCacheDir() 0 10 2
A setFilename() 0 20 3
A addHeader() 0 5 1
A addHeaderOnOutput() 0 5 1
A setSource() 0 5 1
A setLastModified() 0 5 1
A getFilename() 0 4 1
A writeToCache() 0 13 4
D output() 8 36 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mos\CImage;
4
5
/**
6
 * Enable a fast track cache with a json representation of the image delivery.
7
 *
8
 */
9
class CFastTrackCache
10
{
11
    /**
12
     * Cache is disabled to start with.
13
     */
14
    private $enabled = false;
15
16
17
18
    /**
19
     * Path to the cache directory.
20
     */
21
    private $path;
22
23
24
25
    /**
26
     * Filename of current cache item.
27
     */
28
    private $filename;
29
30
31
32
    /**
33
     * Container with items to store as cached item.
34
     */
35
    private $container;
36
37
38
39
    /**
40
     * Enable or disable cache.
41
     *
42
     * @param boolean $enable set to true to enable, false to disable
0 ignored issues
show
Documentation introduced by
There is no parameter named $enable. Did you maybe mean $enabled?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
43
     *
44
     * @return $this
45
     */
46
    public function enable($enabled)
47
    {
48
        $this->enabled = $enabled;
49
        return $this;
50
    }
51
52
53
54
    /**
55
     * Set the path to the cache dir which must exist.
56
     *
57
     * @param string $path to the cache dir.
58
     *
59
     * @throws Exception when $path is not a directory.
60
     *
61
     * @return $this
62
     */
63
    public function setCacheDir($path)
64
    {
65
        if (!is_dir($path)) {
66
            throw new Exception("Cachedir is not a directory.");
67
        }
68
69
        $this->path = rtrim($path, "/");
70
71
        return $this;
72
    }
73
74
75
76
    /**
77
     * Set the filename to store in cache, use the querystring to create that
78
     * filename.
79
     *
80
     * @param array $clear items to clear in $_GET when creating the filename.
81
     *
82
     * @return string as filename created.
83
     */
84
    public function setFilename($clear)
85
    {
86
        $query = $_GET;
87
88
        // Remove parts from querystring that should not be part of filename
89
        foreach ($clear as $value) {
90
            unset($query[$value]);
91
        }
92
93
        arsort($query);
94
        $queryAsString = http_build_query($query);
95
96
        $this->filename = md5($queryAsString);
97
98
        if (CIMAGE_DEBUG) {
99
            $this->container["query-string"] = $queryAsString;
100
        }
101
102
        return $this->filename;
103
    }
104
105
106
107
    /**
108
     * Add header items.
109
     *
110
     * @param string $header add this as header.
111
     *
112
     * @return $this
113
     */
114
    public function addHeader($header)
115
    {
116
        $this->container["header"][] = $header;
117
        return $this;
118
    }
119
120
121
122
    /**
123
     * Add header items on output, these are not output when 304.
124
     *
125
     * @param string $header add this as header.
126
     *
127
     * @return $this
128
     */
129
    public function addHeaderOnOutput($header)
130
    {
131
        $this->container["header-output"][] = $header;
132
        return $this;
133
    }
134
135
136
137
    /**
138
     * Set path to source image to.
139
     *
140
     * @param string $source path to source image file.
141
     *
142
     * @return $this
143
     */
144
    public function setSource($source)
145
    {
146
        $this->container["source"] = $source;
147
        return $this;
148
    }
149
150
151
152
    /**
153
     * Set last modified of source image, use to check for 304.
154
     *
155
     * @param string $lastModified
156
     *
157
     * @return $this
158
     */
159
    public function setLastModified($lastModified)
160
    {
161
        $this->container["last-modified"] = $lastModified;
162
        return $this;
163
    }
164
165
166
167
    /**
168
     * Get filename of cached item.
169
     *
170
     * @return string as filename.
171
     */
172
    public function getFilename()
173
    {
174
        return $this->path . "/" . $this->filename;
175
    }
176
177
178
179
    /**
180
     * Write current item to cache.
181
     *
182
     * @return boolean if cache file was written.
183
     */
184
    public function writeToCache()
185
    {
186
        if (!$this->enabled) {
187
            return false;
188
        }
189
190
        if (is_dir($this->path) && is_writable($this->path)) {
191
            $filename = $this->getFilename();
192
            return file_put_contents($filename, json_encode($this->container)) !== false;
193
        }
194
195
        return false;
196
    }
197
198
199
200
    /**
201
     * Output current item from cache, if available.
202
     *
203
     * @return void
204
     */
205
    public function output()
206
    {
207
        $filename = $this->getFilename();
208
        if (!is_readable($filename)) {
209
            return;
210
        }
211
212
        $item = json_decode(file_get_contents($filename), true);
213
214
        if (!is_readable($item["source"])) {
215
            return;
216
        }
217
218
        foreach ($item["header"] as $value) {
219
            header($value);
220
        }
221
222 View Code Duplication
        if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
            && strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) == $item["last-modified"]) {
224
            header("HTTP/1.0 304 Not Modified");
225
            if (CIMAGE_DEBUG) {
226
                trace(__CLASS__ . " 304");
227
            }
228
            exit;
229
        }
230
231
        foreach ($item["header-output"] as $value) {
232
            header($value);
233
        }
234
235
        if (CIMAGE_DEBUG) {
236
            trace(__CLASS__ . " 200");
237
        }
238
        readfile($item["source"]);
239
        exit;
240
    }
241
}
242