Passed
Push — master ( e877b4...cb3898 )
by Fabio
05:28
created

TApplicationComponent::getClassFxEvents()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 46.4453

Importance

Changes 0
Metric Value
cc 10
eloc 19
nc 17
nop 1
dl 0
loc 27
ccs 2
cts 7
cp 0.2857
crap 46.4453
rs 7.6666
c 0
b 0
f 0

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
 * @package Prado
9
 */
10
11
namespace Prado;
12
13
use Prado\TApplicationMode;
14
15
/**
16
 * TApplicationComponent class
17
 *
18
 * TApplicationComponent is the base class for all components that are
19
 * application-related, such as controls, modules, services, etc.
20
 *
21
 * TApplicationComponent mainly defines a few properties that are shortcuts
22
 * to some commonly used methods. The {@link getApplication Application}
23
 * property gives the application instance that this component belongs to;
24
 * {@link getService Service} gives the current running service;
25
 * {@link getRequest Request}, {@link getResponse Response} and {@link getSession Session}
26
 * return the request and response modules, respectively;
27
 * And {@link getUser User} gives the current user instance.
28
 *
29
 * Besides, TApplicationComponent defines two shortcut methods for
30
 * publishing private files: {@link publishAsset} and {@link publishFilePath}.
31
 *
32
 * @author Qiang Xue <[email protected]>
33
 * @package Prado
34
 * @since 3.0
35
 */
36
class TApplicationComponent extends \Prado\TComponent
37
{
38
	public const APP_COMPONENT_FX_CACHE = 'prado:applicationcomponent:fxcache';
39 91
	/**
40
	 * TApplicationComponents auto listen to global events.
41 91
	 *
42
	 * @return bool returns whether or not to listen.
43
	 */
44
	public function getAutoGlobalListen()
45
	{
46
		return true;
47
	}
48
	
49
	/**
50
	 * This caches the 'fx' events for PRADO classes in the global state
51
	 * @param object $class
52
	 * @return string[] fx events from a specific class
53
	 */
54
	protected function getClassFxEvents($class)
55 12
	{
56
		$app = $this->getApplication();
57 12
		$className = $cache = null;
58
		if ($app && (($mode = $app->getMode()) === TApplicationMode::Normal || $mode === TApplicationMode::Performance) && ($cache = $app->getCache())) {
59
			static $_classfx = null;
60
			if ($_classfx === null) {
61
				$_classfx = $cache->get(self::APP_COMPONENT_FX_CACHE) ?? [];
62
			}
63
			$className = get_class($class);
64
			if (isset($_classfx[$className])) {
65
				return $_classfx[$className];
66
			}
67
		}
68
		$fx = parent::getClassFxEvents($class);
69
		if ($cache) {
70
			if ($pos = strrpos($className, '\\')) {
0 ignored issues
show
Bug introduced by
It seems like $className can also be of type null; however, parameter $haystack of strrpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
			if ($pos = strrpos(/** @scrutinizer ignore-type */ $className, '\\')) {
Loading history...
71
				$baseClassName = substr($className, $pos + 1);
0 ignored issues
show
Bug introduced by
It seems like $className can also be of type null; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
				$baseClassName = substr(/** @scrutinizer ignore-type */ $className, $pos + 1);
Loading history...
72
			} else {
73
				$baseClassName = $className;
74
			}
75
			if (isset(Prado::$classMap[$baseClassName])) {
76
				$_classfx[$className] = $fx;
77
				$cache->set(self::APP_COMPONENT_FX_CACHE, $_classfx);
78
			}
79
		}
80
		return $fx;
81
	}
82
	
83
	/**
84
	 * @return \Prado\TApplication current application instance
85
	 */
86
	public function getApplication()
87
	{
88
		return Prado::getApplication();
89
	}
90
91
	/**
92
	 * @return \Prado\IService the current service
93
	 */
94
	public function getService()
95
	{
96
		return Prado::getApplication()->getService();
97
	}
98
99
	/**
100
	 * @return \Prado\Web\THttpRequest the current user request
101
	 */
102
	public function getRequest()
103
	{
104
		return Prado::getApplication()->getRequest();
105
	}
106
107
	/**
108
	 * @return \Prado\Web\THttpResponse the response
109
	 */
110
	public function getResponse()
111
	{
112
		return Prado::getApplication()->getResponse();
113
	}
114
115
	/**
116
	 * @return \Prado\Web\THttpSession user session
117
	 */
118
	public function getSession()
119
	{
120
		return Prado::getApplication()->getSession();
121
	}
122
123
	/**
124
	 * @return \Prado\Security\IUser information about the current user
125
	 */
126
	public function getUser()
127
	{
128
		return Prado::getApplication()->getUser();
129
	}
130
131
	/**
132
	 * Publishes a private asset and gets its URL.
133
	 * This method will publish a private asset (file or directory)
134
	 * and gets the URL to the asset. Note, if the asset refers to
135
	 * a directory, all contents under that directory will be published.
136
	 * Also note, it is recommended that you supply a class name as the second
137
	 * parameter to the method (e.g. publishAsset($assetPath,__CLASS__) ).
138
	 * By doing so, you avoid the issue that child classes may not work properly
139
	 * because the asset path will be relative to the directory containing the child class file.
140
	 *
141
	 * @param string $assetPath path of the asset that is relative to the directory containing the specified class file.
142
	 * @param string $className name of the class whose containing directory will be prepend to the asset path. If null, it means get_class($this).
143
	 * @return string URL to the asset path.
144
	 */
145
	public function publishAsset($assetPath, $className = null)
146
	{
147
		if ($className === null) {
148
			$className = get_class($this);
149
		}
150
		$class = new \ReflectionClass($className);
151
		$fullPath = dirname($class->getFileName()) . DIRECTORY_SEPARATOR . $assetPath;
152
		return $this->publishFilePath($fullPath);
153
	}
154
155
	/**
156
	 * Publishes a file or directory and returns its URL.
157
	 * @param string $fullPath absolute path of the file or directory to be published
158
	 * @param mixed $checkTimestamp
159
	 * @return string URL to the published file or directory
160
	 */
161
	public function publishFilePath($fullPath, $checkTimestamp = false)
162
	{
163
		return Prado::getApplication()->getAssetManager()->publishFilePath($fullPath, $checkTimestamp);
164
	}
165
}
166