Completed
Push — CI ( ee6bd7...0f01dd )
by Adam
22:32
created

CacheClearCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 67
rs 10
c 1
b 0
f 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 4 1
A execute() 0 6 1
A executeCommand() 0 6 1
B deleteDirectory() 0 16 5
A recreateCacheFolder() 0 3 1
1
<?php
2
/**
3
 * Created by Adam Jakab.
4
 * Date: 07/10/15
5
 * Time: 14.26
6
 */
7
8
namespace SuiteCrm\Command;
9
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Filesystem\Filesystem;
14
15
class CacheClearCommand extends Command implements CommandInterface {
16
    const COMMAND_NAME = 'cache:clear';
17
    const COMMAND_DESCRIPTION = 'Empty the cache folder by removing all files.';
18
19
    public function __construct() {
20
        $this->enableConsoleLogging(true);
21
        parent::__construct(NULL);
22
    }
23
24
    /**
25
     * Configure command
26
     */
27
    protected function configure() {
28
        $this->setName(static::COMMAND_NAME);
29
        $this->setDescription(static::COMMAND_DESCRIPTION);
30
    }
31
32
    /**
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     * @return bool
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output) {
38
        parent::_execute($input, $output);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (_execute() instead of execute()). Are you sure this is correct? If so, you might want to change this to $this->_execute().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
39
        $this->log("Starting command " . static::COMMAND_NAME . "...");
40
        $this->executeCommand();
41
        $this->log("Command " . static::COMMAND_NAME . " done.");
42
    }
43
44
    /**
45
     * Execute Command
46
     */
47
    protected function executeCommand() {
48
        $cachePath = realpath(PROJECT_ROOT . '/cache');
49
        $this->log("Emptying cache folder: " . $cachePath);
50
        $this->deleteDirectory($cachePath);
51
        $this->recreateCacheFolder($cachePath);
52
    }
53
54
    /**
55
     * Recursively delete directory content and the directory itself
56
     * @param string $dir
57
     */
58
    protected function deleteDirectory($dir) {
59
        if (is_dir($dir)) {
60
            $elements = scandir($dir);
61
            foreach ($elements as $element) {
62
                if (!in_array($element, ['.','..'])) {
63
                    $elementFullPath = $dir . "/" . $element;
64
                    if (is_dir($elementFullPath)) {
65
                        $this->deleteDirectory($elementFullPath);
66
                    } else {
67
                        unlink($dir."/".$element);
68
                    }
69
                }
70
            }
71
            rmdir($dir);
72
        }
73
    }
74
75
    /**
76
     * @param string $dir
77
     */
78
    protected function recreateCacheFolder($dir) {
79
        mkdir($dir);
80
    }
81
}