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

CommandProxy::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.0116
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