CacheCleanupExtension::runCleanupJob()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace ByTIC\Codeception\Extensions;
4
5
use Codeception\Events;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Finder\Finder;
8
9
/**
10
 * Class CacheCleanupExtension
11
 * @package KM42\Reviews\Tests\Extensions
12
 *
13
 * Can be configured in suite config:
14
 *
15
 * ```yaml
16
 * # acceptance.suite.yml
17
 * extensions:
18
 *     enabled:
19
 *         - ByTIC\Codeception\Extensions\CacheCleanupExtension:
20
 *             jobs:
21
 *                 bytic:
22
 *                     delete:
23
 *                           - /bootstrap/cache/routes
24
 *                           - /bootstrap/cache/migrations.php
25
 *                           - /storage/cache/dispatcher.php
26
 *                           - /storage/cache/@
27
 *                     empty:
28
 *                           - /storage/cache/db-metadata
29
 *                 job_name_2:
30
 *                     delete:
31
 *                          - file_path
32
 *                          - dir_path
33
 *                     empty:
34
 *                          - dir_path1
35
 *                          - dir_path2
36
 *             afterSuite: bytic
37
 *             beforeSuite:
38
 *                  - bytic
39
 *                  - job_name_2
40
 *             beforeTest:
41
 *                  - bytic
42
 *                  - job_name_2
43
 *             afterTest:
44
 *                  - bytic
45
 *                  - job_name_2
46
 * ```
47
 */
48
class CacheCleanupExtension extends \Codeception\Extension
49
{
50
51
    /**
52
     * @var Filesystem
53
     */
54
    protected $filesystem;
55
56
    protected $finder;
57
58
    // list events to listen to
59
    // Codeception\Events constants used to set the event
60
    public static $events = [
61
        Events::SUITE_AFTER => 'afterSuite',
62
        Events::SUITE_BEFORE => 'beforeSuite',
63
        Events::TEST_BEFORE => 'beforeTest',
64
        Events::TEST_AFTER => 'afterTest',
65
    ];
66
67
    public function __construct($config, $options)
68
    {
69
        parent::__construct($config, $options);
70
        $this->filesystem = new Filesystem();
71
        $this->finder = new Finder();
72
    }
73
74
75
    // methods that handle events
76
77
    /**
78
     * @param \Codeception\Event\SuiteEvent $e
79
     */
80
    public function afterSuite(\Codeception\Event\SuiteEvent $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e 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...
81
    {
82
        $this->cleanupCache('afterSuite');
83
    }
84
85
    /**
86
     * @param \Codeception\Event\TestEvent $e
87
     */
88
    public function beforeSuite(\Codeception\Event\SuiteEvent $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e 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...
89
    {
90
        $this->cleanupCache('beforeSuite');
91
    }
92
93
    /**
94
     * @param \Codeception\Event\TestEvent $e
95
     */
96
    public function beforeTest(\Codeception\Event\TestEvent $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e 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...
97
    {
98
        $this->cleanupCache('beforeTest');
99
    }
100
101
    /**
102
     * @param \Codeception\Event\TestEvent $e
103
     */
104
    public function afterTest(\Codeception\Event\TestEvent $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e 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...
105
    {
106
        $this->cleanupCache('afterTest');
107
    }
108
109
110
    /**
111
     * @param string $type
112
     */
113
    protected function cleanupCache($type)
114
    {
115
        $this->output->debug('[CacheCleanupExtension] Running ' . $type);
116
        if (!isset($this->config[$type])) {
117
            return;
118
        }
119
        $jobs = $this->config[$type];
120
        if (is_string($jobs)) {
121
            $jobs = explode(',', $jobs);
122
        }
123
124
        foreach ($jobs as $job) {
125
            $this->runCleanupJob($job);
126
        }
127
    }
128
129
    /**
130
     * @param $name
131
     */
132
    protected function runCleanupJob($name)
133
    {
134
        if (!isset($this->config['jobs'][$name])) {
135
            return;
136
        }
137
        $actions = $this->config['jobs'][$name];
138
        foreach ($actions as $action => $params) {
139
            $this->runCleanupJobAction($action, $params);
140
        }
141
    }
142
143
    /**
144
     * @param $action
145
     * @param $params
146
     */
147
    protected function runCleanupJobAction($action, $params)
148
    {
149
        switch ($action) {
150
            case 'delete':
151
                $this->runCleanupJobDelete($params);
152
                return;
153
            case 'empty':
154
                $this->runCleanupJobEmpty($params);
155
                return;
156
        }
157
    }
158
159
    /**
160
     * @param $params
161
     */
162
    protected function runCleanupJobDelete($params)
163
    {
164
        foreach ($params as $path) {
165
            $fullPath = $this->getRootDir() . $path;
166
            if (is_file($fullPath)) {
167
                $this->filesystem->remove($fullPath);
168
            } elseif (is_dir($fullPath)) {
169
                $this->filesystem->remove($fullPath);
170
            }
171
        }
172
    }
173
174
    /**
175
     * @param $params
176
     */
177
    protected function runCleanupJobEmpty($params)
178
    {
179
        foreach ($params as $path) {
180
            $fullPath = $this->getRootDir() . $path;
181
            if (is_dir($fullPath)) {
182
                $files = $this->finder->files()->in($fullPath);
183
                $this->filesystem->remove($files);
0 ignored issues
show
Documentation introduced by
$files is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
184
            }
185
        }
186
    }
187
}