Completed
Push — develop ( e1f81f...539a89 )
by Adrien
16:11
created

CachedObjectStorageFactory::getCacheStorageClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpSpreadsheet;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
 *
22
 * @category   PhpSpreadsheet
23
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
24
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
25
 * @version    ##VERSION##, ##DATE##
26
 */
27
class CachedObjectStorageFactory
28
{
29
    const CACHE_IN_MEMORY             = 'Memory';
30
    const CACHE_IN_MEMORY_GZIP        = 'MemoryGZip';
31
    const CACHE_IN_MEMORY_SERIALIZED  = 'MemorySerialized';
32
    const CACHE_IGBINARY              = 'Igbinary';
33
    const CACHE_TO_DISCISAM           = 'DiscISAM';
34
    const CACHE_TO_APC                = 'APC';
35
    const CACHE_TO_MEMCACHE           = 'Memcache';
36
    const CACHE_TO_PHPTEMP            = 'PHPTemp';
37
    const CACHE_TO_WINCACHE           = 'Wincache';
38
    const CACHE_TO_SQLITE             = 'SQLite';
39
    const CACHE_TO_SQLITE3            = 'SQLite3';
40
41
    /**
42
     * Name of the method used for cell cacheing
43
     *
44
     * @var string
45
     */
46
    private static $cacheStorageMethod;
47
48
    /**
49
     * Name of the class used for cell cacheing
50
     *
51
     * @var string
52
     */
53
    private static $cacheStorageClass;
54
55
    /**
56
     * List of all possible cache storage methods
57
     *
58
     * @var string[]
59
     */
60
    private static $storageMethods = [
61
        self::CACHE_IN_MEMORY,
62
        self::CACHE_IN_MEMORY_GZIP,
63
        self::CACHE_IN_MEMORY_SERIALIZED,
64
        self::CACHE_IGBINARY,
65
        self::CACHE_TO_PHPTEMP,
66
        self::CACHE_TO_DISCISAM,
67
        self::CACHE_TO_APC,
68
        self::CACHE_TO_MEMCACHE,
69
        self::CACHE_TO_WINCACHE,
70
        self::CACHE_TO_SQLITE,
71
        self::CACHE_TO_SQLITE3,
72
    ];
73
74
    /**
75
     * Default arguments for each cache storage method
76
     *
77
     * @var array of mixed array
78
     */
79
    private static $storageMethodDefaultParameters = [
80
        self::CACHE_IN_MEMORY => [],
81
        self::CACHE_IN_MEMORY_GZIP => [],
82
        self::CACHE_IN_MEMORY_SERIALIZED => [],
83
        self::CACHE_IGBINARY => [],
84
        self::CACHE_TO_PHPTEMP => [
85
            'memoryCacheSize' => '1MB',
86
        ],
87
        self::CACHE_TO_DISCISAM => [
88
            'dir' => null,
89
        ],
90
        self::CACHE_TO_APC => [
91
            'cacheTime' => 600,
92
        ],
93
        self::CACHE_TO_MEMCACHE => [
94
            'memcacheServer' => 'localhost',
95
            'memcachePort' => 11211,
96
            'cacheTime' => 600,
97
        ],
98
        self::CACHE_TO_WINCACHE => [
99
            'cacheTime' => 600,
100
        ],
101
        self::CACHE_TO_SQLITE => [],
102
        self::CACHE_TO_SQLITE3 => [],
103
    ];
104
105
    /**
106
     * Arguments for the active cache storage method
107
     *
108
     * @var mixed[]
109
     */
110
    private static $storageMethodParameters = [];
111
112
113
    /**
114
     * Return the current cache storage method
115
     *
116
     * @return string|null
117
     **/
118
    public static function getCacheStorageMethod()
119
    {
120
        return self::$cacheStorageMethod;
121
    }
122
123
    /**
124
     * Return the current cache storage class
125
     *
126
     * @return \CachedObjectStorage\ICache|null
127
     **/
128
    public static function getCacheStorageClass()
129
    {
130
        return self::$cacheStorageClass;
131
    }
132
133
    /**
134
     * Return the list of all possible cache storage methods
135
     *
136
     * @return string[]
137
     **/
138
    public static function getAllCacheStorageMethods()
139
    {
140
        return self::$storageMethods;
141
    }
142
143
    /**
144
     * Return the list of all available cache storage methods
145
     *
146
     * @return string[]
147
     **/
148
    public static function getCacheStorageMethods()
149
    {
150
        $activeMethods = array();
151
        foreach (self::$storageMethods as $storageMethod) {
152
            $cacheStorageClass = '\\PhpSpreadsheet\\CachedObjectStorage\\' . $storageMethod;
153
            if (call_user_func(array($cacheStorageClass, 'cacheMethodIsAvailable'))) {
154
                $activeMethods[] = $storageMethod;
155
            }
156
        }
157
        return $activeMethods;
158
    }
159
160
    /**
161
     * Identify the cache storage method to use
162
     *
163
     * @param    string     $method       Name of the method to use for cell cacheing
164
     * @param    mixed[]    $arguments    Additional arguments to pass to the cell caching class
165
     *                                        when instantiating
166
     * @return boolean
167
     **/
168
    public static function initialize($method = self::CACHE_IN_MEMORY, $arguments = [])
169
    {
170
        if (!in_array($method, self::$storageMethods)) {
171
            return false;
172
        }
173
174
        $cacheStorageClass = '\\PhpSpreadsheet\\CachedObjectStorage\\'.$method;
175
        if (!call_user_func([$cacheStorageClass, 'cacheMethodIsAvailable'])) {
176
            return false;
177
        }
178
179
        self::$storageMethodParameters[$method] = self::$storageMethodDefaultParameters[$method];
180
        foreach ($arguments as $argument => $value) {
181
            if (array_key_exists($argument, self::$storageMethodParameters[$method])) {
182
                self::$storageMethodParameters[$method][$argument] = $value;
183
            }
184
        }
185
186
        if (self::$cacheStorageMethod === null) {
187
            self::$cacheStorageClass = '\\PhpSpreadsheet\\CachedObjectStorage\\' . $method;
188
            self::$cacheStorageMethod = $method;
189
        }
190
        return true;
191
    }
192
193
    /**
194
     * Initialise the cache storage
195
     *
196
     * @param    Worksheet     $parent    Enable cell caching for this worksheet
197
     * @return   CachedObjectStorage\ICache
198
     **/
199
    public static function getInstance(Worksheet $parent)
200
    {
201
        $cacheMethodIsAvailable = true;
202
        if (self::$cacheStorageMethod === null) {
203
            $cacheMethodIsAvailable = self::initialize();
204
        }
205
206
        if ($cacheMethodIsAvailable) {
207
            $instance = new self::$cacheStorageClass(
208
                $parent,
209
                self::$storageMethodParameters[self::$cacheStorageMethod]
210
            );
211
            if ($instance !== null) {
212
                return $instance;
213
            }
214
        }
215
216
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PhpSpreadsheet\CachedObj...ageFactory::getInstance of type PhpSpreadsheet\CachedObjectStorage\ICache.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
217
    }
218
219
    /**
220
     * Clear the cache storage
221
     *
222
     **/
223
    public static function finalize()
224
    {
225
        self::$cacheStorageMethod = null;
226
        self::$cacheStorageClass = null;
227
        self::$storageMethodParameters = array();
228
    }
229
}
230