CloudAssets_NullLogger::info()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Central interface for cloud assets module.
4
 *
5
 * @author Mark Guinn <[email protected]>
6
 * @date 01.10.2014
7
 * @package cloudassets
8
 */
9
class CloudAssets extends SS_Object
10
{
11
    /** @var bool - kill switch via config - if true the module will ignore all cloud buckets */
12
    private static $disabled = false;
13
14
    /** @var bool - kill switch for uploading local changes to the cdn - useful for safeguarding local development environments */
15
    private static $uploads_disabled = false;
16
17
    /** @var bool - if this is set to true, the uploads will only occur when running UpdateCloudAssetsTask or CloudAssetsFullCheckTask */
18
    // COMING SOON
19
    //private static $offline_uploads_only = false;
20
21
    /** @var bool - you probably want this true overall, but in some cases like BuildTasks we may not want that */
22
    // COMING SOON
23
    //private static $fail_silently = true;
24
25
    /** @var string - if you have Monolog or something registered with the Injector - anything with info, error, and debug methods */
26
    private static $logger = '';
27
28
    /** @var array */
29
    private static $map = array(
30
        //'assets/folder/path' => array(
31
        //  'Type'      => 'RackspaceBucket',
32
        //  'BaseURL'   => 'http://cdnurl.com/',
33
        //  'SecureURL' => 'https://cdnurl.com/',
34
        //  'Container' => 'container-name',
35
        //  'UserID'    => 'username',
36
        //  'ApiKey'    => 'key',
37
        //  'LocalCopy' => true,
38
        //);
39
    );
40
41
    /** @var array - merged in with all bucket configs */
42
    private static $defaults = array();
43
44
    /** @var array - add to this if you have other file subclasses floating around */
45
    private static $wrappers = array(
46
        'File'              => 'CloudFile',
47
        'Image'             => 'CloudImage',
48
        'CloudImageCached'  => 'CloudImageCached', // this is awkward but prevents it from trying to transform Image_Cached
49
    );
50
51
    /** @var string - placeholder string used for local files */
52
    private static $file_placeholder = 'CloudFile';
53
54
    /** @var string - if an image is missing on the remote (usually when creating a thumbnail) use this instead */
55
    private static $missing_image = 'cloudassets/images/missing.svg';
56
57
    /** @var array - only keep one instance of each bucket */
58
    protected $bucketCache = array();
59
60
61
    /**
62
     * @return CloudAssets
63
     */
64
    public static function inst()
65
    {
66
        return Injector::inst()->get('CloudAssets');
67
    }
68
69
70
    /**
71
     * @param string|File $filename
72
     * @return CloudBucket
73
     */
74
    public function map($filename)
75
    {
76
        if (Config::inst()->get('CloudAssets', 'disabled')) {
77
            return null;
78
        }
79
        if (is_object($filename)) {
80
            $filename = $filename->getFilename();
81
        }
82
        $maps = Config::inst()->get('CloudAssets', 'map');
83
84
        foreach ($maps as $path => $cfg) {
0 ignored issues
show
Bug introduced by
The expression $maps of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
85
            $this->getLogger()->debug("checking $path against $filename => ".strpos($filename, $path));
86
            if (strpos($filename, $path) === 0) {
87
                if (!isset($this->bucketCache[$path])) {
88
                    // merge in default config if needed
89
                    $defaults = Config::inst()->get('CloudAssets', 'defaults');
90
                    if (!empty($defaults) && is_array($defaults)) {
91
                        $cfg = array_merge($defaults, $cfg);
92
                    }
93
                    if (empty($cfg[ CloudBucket::TYPE ])) {
94
                        continue;
95
                    }
96
97
                    // instantiate the bucket
98
                    $this->bucketCache[$path] = Injector::inst()->create($cfg[CloudBucket::TYPE], $path, $cfg);
99
                }
100
101
                $this->getLogger()->debug("CloudAssets: Mapping $filename to bucket $path");
102
                return $this->bucketCache[$path];
103
            }
104
        }
105
106
        $this->getLogger()->debug("CloudAssets: No mapping found for $filename - ");
107
        return null;
108
    }
109
110
111
    /**
112
     * Updates the cloud status of all files (used in tests and cron job)
113
     */
114
    public function updateAllFiles()
115
    {
116
        foreach (File::get() as $f) {
117
            $f->updateCloudStatus();
118
        }
119
    }
120
121
122
    /**
123
     * @param string $className
124
     * @return string
125
     */
126
    public function getWrapperClass($className)
127
    {
128
        $wrappers = Config::inst()->get('CloudAssets', 'wrappers');
129
        // Needs to be wrapped
130
        if (isset($wrappers[$className])) {
131
            return $wrappers[$className];
132
        }
133
        // Already wrapped
134
        if (in_array($className, $wrappers, true)) {
135
            return $className;
136
        }
137
        // Can't be wrapped
138
        return null;
139
    }
140
141
142
    /**
143
     * Wipes out any buckets we've saved
144
     */
145
    public function clearBucketCache()
146
    {
147
        $this->bucketCache = array();
148
    }
149
150
151
    /**
152
     * @return object
153
     */
154
    public function getLogger()
155
    {
156
        $service = Config::inst()->get('CloudAssets', 'logger');
157
        $inj = Injector::inst();
158
        if (!empty($service) && $inj->hasService($service)) {
159
            return $inj->get($service);
160
        } else {
161
            return $inj->get('CloudAssets_NullLogger');
162
        }
163
    }
164
}
165
166
167
class CloudAssets_NullLogger
168
{
169
    public function log($str)
0 ignored issues
show
Unused Code introduced by
The parameter $str is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
    {
171
    }
172
    public function info($str)
0 ignored issues
show
Unused Code introduced by
The parameter $str is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
173
    {
174
    }
175
    public function debug($str)
0 ignored issues
show
Unused Code introduced by
The parameter $str is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
    {
177
    }
178
    public function warn($str)
0 ignored issues
show
Unused Code introduced by
The parameter $str is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179
    {
180
    }
181
    public function error($str)
0 ignored issues
show
Unused Code introduced by
The parameter $str is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
    {
183
    }
184
}
185