ClearCacheController   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 64
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B clearCacheAction() 0 18 7
A getCacheDir() 0 3 1
A clearCacheDir() 0 16 5
A canDeleteCacheFolder() 0 3 3
A canDeleteCacheFile() 0 4 1
A delTree() 0 7 4
1
<?php
2
3
namespace ExpressApi\V1\Rpc\ClearCache;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use ZF\ApiProblem\ApiProblem;
7
use ZF\ApiProblem\ApiProblemResponse;
8
9
class ClearCacheController extends AbstractActionController {
10
11
    private $dirsWhiteList = array(
12
        'api',
13
        'module'
14
    );
15
16
    public function clearCacheAction() {
17
        $params = $this->bodyParams();
0 ignored issues
show
Bug introduced by
The method bodyParams() does not exist on ExpressApi\V1\Rpc\ClearCache\ClearCacheController. Did you maybe mean params()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
18
        $dirs = array_key_exists('dir', $params) ? explode(';', $params['dir']) : array();
19
        $result = array(
20
            'success' => true
21
        );
22
        if(is_array($dirs) && count($dirs)) {
23
            foreach($dirs as $dir) {
24
                if(in_array($dir, $this->dirsWhiteList)) {
25
                    $result['success'] = $this->clearCacheDir($dir) && $result['success'];
26
                }
27
                else {
28
                    return new ApiProblemResponse(new ApiProblem(400, "The dir ".$dir." is not allowed."));
29
                }
30
            }
31
        }
32
        return $result;
33
    }
34
35
    private function getCacheDir() {
36
        return dirname(__FILE__).'/../../../../../../../data/cache/';
37
    }
38
39
    private function clearCacheDir($dir) {
40
        $cacheDir = $this->getCacheDir().$dir;
41
        if(is_dir($cacheDir)) {
42
            $dirContent = array_diff(scandir($cacheDir), array('.', '..'));
43
            foreach($dirContent as $cacheNode) {
44
                $cacheRealPath = $cacheDir.'/'.$cacheNode;
45
                if($this->canDeleteCacheFolder($cacheDir, $cacheNode)) {
46
                    $this->delTree($cacheRealPath);
47
                }
48
                else if($this->canDeleteCacheFile($cacheDir, $cacheNode)) {
49
                    unlink($cacheRealPath);
50
                }
51
            }
52
        }
53
        return true;
54
    }
55
56
    private function canDeleteCacheFolder($path, $nodeCache) {
57
        return (is_dir($path.'/'.$nodeCache) && $nodeCache !== '.' && $nodeCache !== '..');
58
    }
59
60
    private function canDeleteCacheFile($path, $nodeCache) {
0 ignored issues
show
Unused Code introduced by
The parameter $path 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...
61
        $ext = pathinfo($nodeCache, PATHINFO_EXTENSION);
62
        return ($ext === 'php');
63
    }
64
65
    public static function delTree($dir) {
66
        $files = array_diff(scandir($dir), array('.', '..'));
67
        foreach($files as $file) {
68
            (is_dir("$dir/$file") && !is_link($dir)) ? self::delTree("$dir/$file") : unlink("$dir/$file");
69
        }
70
        return rmdir($dir);
71
    }
72
}
73