Completed
Push — master ( b29d6b...03436d )
by Bjørn
03:06
created

ServeFile   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 63
ccs 16
cts 18
cp 0.8889
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B serve() 0 33 9
1
<?php
2
namespace WebPConvert\Serve;
3
4
//use WebPConvert\Serve\Report;
5
use WebPConvert\Serve\Header;
6
use WebPConvert\Serve\Exceptions\ServeFailedException;
7
8
/**
9
 * Serve a file (send to standard output)
10
 *
11
 * @package    WebPConvert
12
 * @author     Bjørn Rosell <[email protected]>
13
 * @since      Class available since Release 2.0.0
14
 */
15
class ServeFile
16
{
17
18
    public static $defaultOptions = [
19
        'set-cache-control-header' => false,
20
        'set-expires-header' => false,
21
        'cache-control-header' => 'public, max-age=31536000',
22
        'add-vary-accept-header' => false,
23
        'set-content-type-header' => true,
24
        'set-last-modified-header' => true,
25
    ];
26
27
    /**
28
     * Serve existing file.
29
     *
30
     * @param  string  $filename     File to serve (absolute path)
31
     * @param  string  $contentType  Content-type (used to set header).
32
     *                                    Only used when the "set-content-type-header" option is set.
33
     *                                    Set to ie "image/jpeg" for serving jpeg file.
34
     * @param  array   $options      Array of named options (optional).
35
     *       Supported options:
36
     *       'add-vary-accept-header'  => (boolean)   Whether to add *Vary: Accept* header or not. Default: true.
37
     *       'set-content-type-header' => (boolean)   Whether to set *Content-type* header or not. Default: true.
38
     *       'set-last-modified-header' => (boolean)  Whether to set *Last-Modified* header or not. Default: true.
39
     *       'set-cache-control-header' => (boolean)  Whether to set *Cache-Control* header or not. Default: true.
40
     *       'cache-control-header' => string         Cache control header. Default: "public, max-age=86400"
41
     *
42
     * @throws ServeFailedException  if serving failed
43
     * @return  void
44
     */
45 5
    public static function serve($filename, $contentType, $options = [])
46
    {
47 5
        $options = array_merge(self::$defaultOptions, $options);
48
49 5
        if ($options['set-last-modified-header']) {
50 4
            Header::setHeader("Last-Modified: " . gmdate("D, d M Y H:i:s", @filemtime($filename)) ." GMT");
51
        }
52
53 5
        if ($options['set-content-type-header']) {
54 4
            Header::setHeader('Content-type: ' . $contentType);
55
        }
56
57 5
        if ($options['add-vary-accept-header']) {
58 1
            Header::addHeader('Vary: Accept');
59
        }
60
61 5
        if (!empty($options['cache-control-header'])) {
62 5
            if ($options['set-cache-control-header']) {
63 2
                Header::setHeader('Cache-Control: ' . $options['cache-control-header']);
64
            }
65 5
            if ($options['set-expires-header']) {
66
                // Add exprires header too (#126)
67
                // Check string for something like this: max-age:86400
68 1
                if (preg_match('#max-age\\s*=\\s*(\\d*)#', $options['cache-control-header'], $matches)) {
69 1
                    $seconds = $matches[1];
70 1
                    Header::setHeader('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + intval($seconds)));
71
                }
72
            }
73
        }
74
75 5
        if (@readfile($filename) === false) {
76
            Header::addHeader('X-WebP-Convert-Error: Could not read file');
77
            throw new ServeFailedException('Could not read file');
78
        }
79 5
    }
80
}
81