Passed
Push — master ( d5adc1...45b033 )
by Fabio
06:12
created

TFlushCachesAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A performAction() 0 26 6
1
<?php
2
/**
3
 * TFlushCachesAction class file
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 * @package Prado\Shell\Actions
9
 */
10
11
namespace Prado\Shell\Actions;
12
13
use Prado\Caching\ICache;
14
use Prado\Shell\TShellAction;
15
16
/**
17
 * This command clears all application modules implementing ICache.
18
 *
19
 * @author Brad Anderson <belisoful[at]icloud[dot]com>
20
 * @package Prado\Shell\Actions
21
 * @since 4.2.0
22
 */
23
class TFlushCachesAction extends TShellAction
24
{
25
	protected $action = 'flushcaches';
26
	protected $parameters = [];
27
	protected $optional = ['directory'];
28
	protected $description = 'Flushes all application TCache modules. Use case: upgrading a performance mode website by clearing out the old cache.';
29
30
	/**
31
	 * @param array $args parameters
32
	 * @return bool
33
	 */
34
	public function performAction($args)
35
	{
36
		$app = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $app is dead and can be removed.
Loading history...
37
		if (count($args) <= 1) {
38
			$args[1] = '.';
39
		}
40
		if (false === ($xml = $this->getAppConfigFile($args[1]))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $xml is dead and can be removed.
Loading history...
41
			return false;
42
		}
43
		$app = $this->initializePradoApplication($args[1]);
44
		
45
		$app->onLoadState();
46
		$app->onLoadStateComplete();
47
		$cachesFlushed = [];
48
		foreach ($app->getModulesByType('Prado\\Caching\\ICache') as $id => $module) {
49
			$module = (!$module) ? $app->getModule($id) : $module;
50
			$module->flush();
51
			$cachesFlushed[] = get_class($module);
52
		}
53
		$app->onSaveState();
54
		$app->onSaveStateComplete();
55
		if (!count($cachesFlushed)) {
56
			$cachesFlushed[] = 'no caches (none were found)';
57
		}
58
		echo "** Application flushed " . implode(', ', $cachesFlushed) . "\n\n";
59
		return true;
60
	}
61
}
62