Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
30 / 31
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
File
96.77% covered (success)
96.77%
30 / 31
85.71% covered (warning)
85.71%
6 / 7
13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCacheDir
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 options2string
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
4.01
 cacheName
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 isCached
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCached
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCache
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Karla ImageMagick wrapper library
5 *
6 * PHP Version 8.0<
7 *
8 * @category Utility
9 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
10 * @license  http://www.opensource.org/licenses/mit-license.php MIT
11 * @link     https://github.com/localgod/karla Karla
12 * @since    2012-04-05
13 */
14
15declare(strict_types=1);
16
17namespace Karla\Cache;
18
19/**
20 * Class for file caching
21 *
22 * @category Utility
23 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
24 * @license  http://www.opensource.org/licenses/mit-license.php MIT
25 * @link     https://github.com/localgod/karla Karla
26 */
27class File implements \Karla\Cache
28{
29    /**
30     * Cache directory
31     *
32     * @var string
33     */
34    private string $cacheDir;
35
36    /**
37     * Create a new file cache
38     *
39     * @param string $dirName Path to cache directory
40     *
41     * @throws \InvalidArgumentException
42     */
43    public function __construct(string $dirName)
44    {
45        $this->setCacheDir($dirName);
46    }
47
48    /**
49     * Set the cache directory
50     *
51     * @param string $dirName Path to the cache directory
52     *
53     * @throws \InvalidArgumentException if path was not found
54     * @throws \InvalidArgumentException if path was not writeable
55     * @throws \InvalidArgumentException if path was not a directory
56     */
57    private function setCacheDir(string $dirName): void
58    {
59        if (! file_exists($dirName)) {
60            throw new \InvalidArgumentException("Path not found", 0);
61        }
62        if (! is_writeable($dirName)) {
63            throw new \InvalidArgumentException("Path not writable", 1);
64        }
65        if (! is_dir($dirName)) {
66            throw new \InvalidArgumentException("Path not a directory", 2);
67        }
68        $this->cacheDir = $dirName;
69    }
70
71    /**
72     * Create a string representation of the options used
73     *
74     * @param array<string> $options Options
75     */
76    private function options2string(array $options): string
77    {
78        $output = array();
79        foreach ($options as $option) {
80            if (strstr($option, 'resize')) {
81                $option = str_replace('\>', '', $option);
82                $option = str_replace('\<', '', $option);
83            }
84            if (strstr($option, 'crop')) {
85                $option = str_replace(' +repage', '', $option);
86            }
87            $option = trim($option);
88            $option = str_replace(' ', '_', $option);
89            $option = str_replace('-', '', $option);
90            $output[] = $option;
91        }
92        return implode('&', $output);
93    }
94
95    /**
96     * Generate cache name
97     *
98     * @param string $inputFile Path to file
99     * @param string $outputFile Path to file
100     * @param array<string> $options Options
101     */
102    private function cacheName(string $inputFile, string $outputFile, array $options): string
103    {
104        $inputFile = str_replace('"', '', $inputFile);
105        $outputFile = str_replace('"', '', $outputFile);
106        $ext = pathinfo(basename($outputFile), PATHINFO_EXTENSION);
107        $filename = $inputFile . $this->options2string($options);
108        return $this->cacheDir . '/' . md5($filename) . '.' . $ext;
109    }
110
111    /**
112     * Check if there exists a cached version of the file
113     *
114     * @param string $inputFile Path to file
115     * @param string $outputFile Path to file
116     * @param array<string> $options Options
117     */
118    public function isCached(string $inputFile, string $outputFile, array $options): bool
119    {
120        $filename = $this->cacheName($inputFile, $outputFile, $options);
121        return file_exists($filename);
122    }
123
124    /**
125     * Get cached version of the file
126     *
127     * @param string $inputFile Path to file
128     * @param string $outputFile Path to file
129     * @param array<string> $options Options
130     */
131    public function getCached(string $inputFile, string $outputFile, array $options): string
132    {
133        return $this->cacheName($inputFile, $outputFile, $options);
134    }
135
136    /**
137     * Set cached version of the file
138     *
139     * @param string $inputFile Path to file
140     * @param string $outputFile Path to file
141     * @param array<string> $options Options
142     */
143    public function setCache(string $inputFile, string $outputFile, array $options): void
144    {
145        $filename = $this->cacheName($inputFile, $outputFile, $options);
146        file_put_contents($filename, file_get_contents(str_replace('"', '', $outputFile)));
147        shell_exec('chmod 666 ' . $filename);
148    }
149}