Completed
Push — master ( f21f00...c91d75 )
by Peter
18:47 queued 10:56
created

CommandProxy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
f 0
lcom 1
cbo 3
dl 0
loc 48
ccs 16
cts 17
cp 0.9412
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A isAvailable() 0 8 2
A call() 0 14 3
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Helpers;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Command;
18
use Maslosoft\Mangan\Exceptions\CommandNotFoundException;
19
use Maslosoft\Mangan\Mangan;
20
use Maslosoft\Mangan\Storage\CommandProxyStorage;
21
22
/**
23
 * CommandProxy
24
 * This evalueates commands only if available
25
 *
26
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
27
 */
28
class CommandProxy extends Command
29
{
30
31
	/**
32
	 * Static store of available commands
33
	 * @var CommandProxyStorage
34
	 */
35
	private $available = null;
36
37 1
	public function __construct(AnnotatedInterface $model = null)
38
	{
39 1
		parent::__construct($model);
40 1
		if (empty($model))
41
		{
42
			$mangan = Mangan::fly();
43
		}
44
		else
45
		{
46 1
			$mangan = Mangan::fromModel($model);
47
		}
48 1
		$this->available = new CommandProxyStorage($this, $mangan->connectionId);
49 1
	}
50
51 1
	public function isAvailable($command)
52
	{
53 1
		if (!isset($this->available->$command))
54
		{
55 1
			return true;
56
		}
57 1
		return $this->available->$command;
58
	}
59
60 1
	public function call($command, $arguments = [])
61
	{
62 1
		if ($this->isAvailable($command))
63
		{
64
			try
65
			{
66 1
				return parent::call($command, $arguments);
67
			}
68 1
			catch (CommandNotFoundException $e)
69
			{
70 1
				$this->available->$command = false;
71
			}
72
		}
73 1
	}
74
75
}
76