Completed
Pull Request — master (#15)
by Robbert
02:28
created

cache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 87.5%

Importance

Changes 13
Bugs 3 Features 5
Metric Value
wmc 12
c 13
b 3
f 5
lcom 0
cbo 6
dl 0
loc 86
ccs 28
cts 32
cp 0.875
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
C create() 0 25 7
A getCacheStore() 0 9 2
A __callStatic() 0 9 2
A proxy() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Ariadne Component Library.
5
 *
6
 * (c) Muze <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace arc;
12
13
/**
14
 * Class cache
15
 * @package arc
16
 * @requires \arc\path
17
 * @requires \arc\context
18
 */
19
class cache
20
{
21
    /**
22
     * This method creates a new cache store ( \arc\cache\Store )
23
     * It will store the cache on disk in a folder defined by ARC_CACHE_DIR, or if not
24
     * defined in the system temp dir under arc/cache/.
25
     * @param string $prefix  Optional. A prefix name or path for subsequent cache images
26
     * @param mixed  $timeout Optional. Number of seconds (int) or string parseable by strtotime. Defaults to 7200.
27
     * @return cache\Store
28
     * @throws ExceptionConfigError
29
     */
30 4
    public static function create($prefix = null, $timeout = 7200)
31
    {
32 4
        if (!defined('ARC_CACHE_DIR')) {
33 1
            define( 'ARC_CACHE_DIR', sys_get_temp_dir().'/arc/cache' );
34 1
        }
35 4
        if (!file_exists( ARC_CACHE_DIR )) {
36 1
            @mkdir( ARC_CACHE_DIR, 0770, true );
37 1
        }
38 4
        if (!file_exists( ARC_CACHE_DIR )) {
39
            throw new \arc\ExceptionConfigError('Cache Directory does not exist ( '.ARC_CACHE_DIR.' )', \arc\exceptions::CONFIGURATION_ERROR);
40
        }
41 4
        if (!is_dir( ARC_CACHE_DIR )) {
42
            throw new \arc\ExceptionConfigError('Cache Directory is not a directory ( '.ARC_CACHE_DIR.' )', \arc\exceptions::CONFIGURATION_ERROR);
43
        }
44 4
        if (!is_writable( ARC_CACHE_DIR )) {
45
            throw new \arc\ExceptionConfigError('Cache Directory is not writable ( '.ARC_CACHE_DIR.' )', \arc\exceptions::CONFIGURATION_ERROR);
46
        }
47 4
        if (!$prefix) { // make sure you have a default prefix, so you won't clear other prefixes unintended
48 1
            $prefix = 'default';
49 1
        }
50 4
        $context = \arc\context::$context;
51 4
        $fileStore = new cache\FileStore( ARC_CACHE_DIR . '/' . $prefix, $context->arcPath );
52
53 4
        return new cache\Store( $fileStore, $timeout );
54
    }
55
56
    /**
57
     * This method creates a new cache store, if one is not available in \arc\context yet, stores it in \arc\context
58
     * and returns it.
59
     * @return cache\Store
60
     */
61 2
    public static function getCacheStore()
62
    {
63 2
        $context = \arc\context::$context;
64 2
        if (!$context->arcCacheStore) {
65 1
            $context->arcCacheStore = self::create();
66 1
        }
67
68 2
        return $context->arcCacheStore;
69
    }
70
71
    /**
72
     * This reroutes any static calls to \arc\cache to the cache store instance in \arc\context
73
     * @param $name
74
     * @param $args
75
     * @return mixed
76
     * @throws ExceptionMethodNotFound
77
     */
78 2
    public static function __callStatic($name, $args)
79
    {
80 2
        $store = self::getCacheStore();
81 2
        if (method_exists( $store, $name )) {
82 2
            return call_user_func_array( array( $store, $name), $args);
83
        } else {
84
            throw new \arc\ExceptionMethodNotFound('Method ' . $name . ' not found in Cache Store', \arc\exceptions::OBJECT_NOT_FOUND);
85
        }
86
    }
87
88
    /**
89
     * Creates a new caching proxy object for any given object.
90
     * @param       $object The object to cache
91
     * @param mixed $cacheControl Either an integer with the number of seconds to cache stuff, or a closure that returns an int.
92
     *                            The closure is called with one argument, an array with the following information:
93
     *                            - target      The cached object a method was called on.
94
     *                            - method      The method called
95
     *                            - arguments   The arguments to the method
96
     *                            - output      Any output generated by the method
97
     *                            - result      The result of the method
98
     * @return cache\Proxy
99
     */
100 2
    public static function proxy($object, $cacheControl = 7200)
101
    {
102 2
        return new cache\Proxy( $object, self::getCacheStore(), $cacheControl );
103
    }
104
}
105