DemoAccountPlugin   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 104
Duplicated Lines 8.65 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 9
loc 104
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A Init() 0 8 1
A configMapping() 9 9 1
A FilterAppData() 0 8 5
A FilterActionParams() 0 10 5
A isDemoAccount() 0 4 2
A AjaxActionPreCall() 0 8 3
A FilterSendMessage() 0 7 3
A MainFabrica() 0 19 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class DemoAccountPlugin extends \RainLoop\Plugins\AbstractPlugin
4
{
5
	/**
6
	 * @return void
7
	 */
8
	public function Init()
9
	{
10
		$this->addHook('filter.app-data', 'FilterAppData');
11
		$this->addHook('filter.action-params', 'FilterActionParams');
12
		$this->addHook('ajax.action-pre-call', 'AjaxActionPreCall');
13
		$this->addHook('filter.send-message', 'FilterSendMessage');
14
		$this->addHook('main.fabrica', 'MainFabrica');
15
	}
16
17
	/**
18
	 * @return array
19
	 */
20 View Code Duplication
	protected function configMapping()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
	{
22
		return array(
23
			\RainLoop\Plugins\Property::NewInstance('email')->SetLabel('Demo Email')
24
				->SetDefaultValue('[email protected]'),
25
			\RainLoop\Plugins\Property::NewInstance('password')->SetLabel('Demo Password')
26
				->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD)
27
		);
28
	}
29
30
	/**
31
	 * @return void
32
	 */
33
	public function FilterAppData($bAdmin, &$aResult)
34
	{
35
		if (!$bAdmin && \is_array($aResult) && isset($aResult['Auth']) && !$aResult['Auth'])
36
		{
37
			$aResult['DevEmail'] = $this->Config()->Get('plugin', 'email', $aResult['DevEmail']);
38
			$aResult['DevPassword'] = APP_DUMMY;
39
		}
40
	}
41
42
	/**
43
	 * @return void
44
	 */
45
	public function FilterActionParams($sMethodName, &$aActionParams)
46
	{
47
		if ('DoLogin' === $sMethodName && isset($aActionParams['Email']) && isset($aActionParams['Password']))
48
		{
49
			if ($this->Config()->Get('plugin', 'email') === $aActionParams['Email'])
50
			{
51
				$aActionParams['Password'] = $this->Config()->Get('plugin', 'password');
52
			}
53
		}
54
	}
55
56
	/**
57
	 * @param \RainLoop\Model\Account $oAccount
58
	 *
59
	 * @return bool
60
	 */
61
	public function isDemoAccount($oAccount)
62
	{
63
		return ($oAccount && $oAccount->Email() === $this->Config()->Get('plugin', 'email'));
64
	}
65
66
	public function AjaxActionPreCall($sAction)
67
	{
68
		if ('AccountSetup' === $sAction &&
69
			$this->isDemoAccount($this->Manager()->Actions()->GetAccount()))
70
		{
71
			throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::DemoAccountError);
72
		}
73
	}
74
75
	public function FilterSendMessage(&$oMessage)
76
	{
77
		if ($oMessage && $this->isDemoAccount($this->Manager()->Actions()->GetAccount()))
78
		{
79
			throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::DemoSendMessageError);
80
		}
81
	}
82
83
	/**
84
	 * @param string $sName
85
	 * @param mixed $oDriver
86
	 */
87
	public function MainFabrica($sName, &$oDriver)
88
	{
89
		switch ($sName)
90
		{
91
			case 'storage':
92
			case 'storage-local':
93
				if (\class_exists('\\RainLoop\\Providers\\Storage\\TemproryApcStorage') &&
94
					\function_exists('apc_store'))
95
				{
96
					$oAccount = $this->Manager()->Actions()->GetAccount();
97
					if ($this->isDemoAccount($oAccount))
98
					{
99
						$oDriver = new \RainLoop\Providers\Storage\TemproryApcStorage(APP_PRIVATE_DATA.'storage',
100
							$sName === 'storage-local');
101
					}
102
				}
103
				break;
104
		}
105
	}
106
}
107
108