Passed
Push — master ( 94ecb9...a83314 )
by Fabio
11:05 queued 05:24
created

TApplicationComponent::getClassFxEvents()   C

Complexity

Conditions 12
Paths 32

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 72.75

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 29
c 2
b 0
f 0
nc 32
nop 1
dl 0
loc 41
ccs 2
cts 8
cp 0.25
crap 72.75
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * TApplicationComponent class
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 */
9
10
namespace Prado;
11
12
use Prado\TApplicationMode;
13
14
/**
15
 * TApplicationComponent class
16
 *
17
 * TApplicationComponent is the base class for all components that are
18
 * application-related, such as controls, modules, services, etc.
19
 *
20
 * TApplicationComponent mainly defines a few properties that are shortcuts
21
 * to some commonly used methods. The {@link getApplication Application}
22
 * property gives the application instance that this component belongs to;
23
 * {@link getService Service} gives the current running service;
24
 * {@link getRequest Request}, {@link getResponse Response} and {@link getSession Session}
25
 * return the request and response modules, respectively;
26
 * And {@link getUser User} gives the current user instance.
27
 *
28
 * Besides, TApplicationComponent defines two shortcut methods for
29
 * publishing private files: {@link publishAsset} and {@link publishFilePath}.
30
 *
31
 * @author Qiang Xue <[email protected]>
32
 * @since 3.0
33
 */
34
class TApplicationComponent extends \Prado\TComponent
35
{
36
	public const FX_CACHE_FILE = 'fxevent.cache';
37
	/**
38
	 * TApplicationComponents auto listen to global events.
39 91
	 *
40
	 * @return bool returns whether or not to listen.
41 91
	 */
42
	public function getAutoGlobalListen()
43
	{
44
		return true;
45
	}
46
47
	/**
48
	 * This caches the 'fx' events for PRADO classes in the application cache
49
	 * @param object $class The object to get the 'fx' events.
50
	 * @return string[] fx events from a specific class
51
	 */
52
	protected function getClassFxEvents($class)
53
	{
54
		static $_classfx = [];
55 12
		static $_classfxSize = 0;
56
		static $_loaded = false;
57 12
58
		$app = $this->getApplication();
59
		$cacheFile = $mode = null;
60
		if ($app) {
0 ignored issues
show
introduced by
$app is of type Prado\TApplication, thus it always evaluated to true.
Loading history...
61
			$cacheFile = $app->getRuntimePath() . DIRECTORY_SEPARATOR . self::FX_CACHE_FILE;
62
			if((($mode = $app->getMode()) === TApplicationMode::Normal || $mode === TApplicationMode::Performance) && !$_loaded) {
63
				$_loaded = true;
64
				if (($content = @file_get_contents($cacheFile)) !== false) {
65
					$_classfx = @unserialize($content) ?? [];
66
					$_classfxSize = count($_classfx);
67
				}
68
			}
69
		}
70
		$className = $class::class;
71
		if (array_key_exists($className, $_classfx)) {
72
			return $_classfx[$className];
73
		}
74
		$fx = parent::getClassFxEvents($class);
75
		$_classfx[$className] = $fx;
76
		if ($cacheFile) {
77
			if ($mode === TApplicationMode::Performance) {
78
				file_put_contents($cacheFile, serialize($_classfx), LOCK_EX);
79
			} elseif ($mode === TApplicationMode::Normal) {
80
				static $_flipClassMap = null;
81
82
				if ($_flipClassMap === null) {
83
					$_flipClassMap = array_flip(Prado::$classMap);
84
				}
85
				$classData = array_intersect_key($_classfx, $_flipClassMap);
86
				if (($c = count($classData)) > $_classfxSize) {
87
					$_classfxSize = $c;
88
					file_put_contents($cacheFile, serialize($_classfx), LOCK_EX);
89
				}
90
			}
91
		}
92
		return $fx;
93
	}
94
95
	/**
96
	 * @return \Prado\TApplication current application instance
97
	 */
98
	public function getApplication()
99
	{
100
		return Prado::getApplication();
101
	}
102
103
	/**
104
	 * @return \Prado\TService the current service
105
	 */
106
	public function getService()
107
	{
108
		return Prado::getApplication()->getService();
109
	}
110
111
	/**
112
	 * @return \Prado\Web\THttpRequest the current user request
113
	 */
114
	public function getRequest()
115
	{
116
		return Prado::getApplication()->getRequest();
117
	}
118
119
	/**
120
	 * @return \Prado\Web\THttpResponse the response
121
	 */
122
	public function getResponse()
123
	{
124
		return Prado::getApplication()->getResponse();
125
	}
126
127
	/**
128
	 * @return \Prado\Web\THttpSession user session
129
	 */
130
	public function getSession()
131
	{
132
		return Prado::getApplication()->getSession();
133
	}
134
135
	/**
136
	 * @return \Prado\Security\IUser information about the current user
137
	 */
138
	public function getUser()
139
	{
140
		return Prado::getApplication()->getUser();
141
	}
142
143
	/**
144
	 * Publishes a private asset and gets its URL.
145
	 * This method will publish a private asset (file or directory)
146
	 * and gets the URL to the asset. Note, if the asset refers to
147
	 * a directory, all contents under that directory will be published.
148
	 * Also note, it is recommended that you supply a class name as the second
149
	 * parameter to the method (e.g. publishAsset($assetPath,__CLASS__) ).
150
	 * By doing so, you avoid the issue that child classes may not work properly
151
	 * because the asset path will be relative to the directory containing the child class file.
152
	 *
153
	 * @param string $assetPath path of the asset that is relative to the directory containing the specified class file.
154
	 * @param string $className name of the class whose containing directory will be prepend to the asset path. If null, it means $this::class.
155
	 * @return string URL to the asset path.
156
	 */
157
	public function publishAsset($assetPath, $className = null)
158
	{
159
		if ($className === null) {
160
			$className = $this::class;
161
		}
162
		$class = new \ReflectionClass($className);
163
		$fullPath = dirname($class->getFileName()) . DIRECTORY_SEPARATOR . $assetPath;
164
		return $this->publishFilePath($fullPath);
165
	}
166
167
	/**
168
	 * Publishes a file or directory and returns its URL.
169
	 * @param string $fullPath absolute path of the file or directory to be published
170
	 * @param mixed $checkTimestamp
171
	 * @return string URL to the published file or directory
172
	 */
173
	public function publishFilePath($fullPath, $checkTimestamp = false)
174
	{
175
		return Prado::getApplication()->getAssetManager()->publishFilePath($fullPath, $checkTimestamp);
176
	}
177
}
178