Completed
Push — master ( fb03b4...646b2a )
by smiley
02:45
created

Gateway::mapMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
/**
3
 * Class Gateway
4
 *
5
 * @filesource   Gateway.php
6
 * @created      02.04.2016
7
 * @package      chillerlan\Threema
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Threema;
14
15
use chillerlan\Threema\Endpoint\EndpointInterface;
16
use ReflectionClass;
17
use ReflectionMethod;
18
19
/**
20
 * EndpointInterface methods
21
 *
22
 * @method checkCredits():int
23
 * @method checkCapabilities(string $threemaID):string
24
 * @method getIdByPhone(string $phoneno):string
25
 * @method getIdByPhoneHash(string $phonenoHash):string
26
 * @method getIdByEmail(string $email):string
27
 * @method getIdByEmailHash(string $emailHash):string
28
 * @method getPublicKey(string $threemaID):string
29
 * @method sendSimple(string $to, string $message)
30
 * @method sendE2E(string $threemaID, string $box, string $nonce)
31
 * @method upload(string $blob)
32
 * @method download(string $blobID)
33
 */
34
class Gateway{
35
36
	/**
37
	 * @var \chillerlan\Threema\Endpoint\EndpointInterface
38
	 */
39
	protected $endpointInterface;
40
41
	/**
42
	 * @var array[\ReflectionMethod]
43
	 */
44
	protected $endpointInterfaceMap = [];
45
46
	/**
47
	 * Gateway constructor.
48
	 *
49
	 * @param \chillerlan\Threema\Endpoint\EndpointInterface $endpointInterface
50
	 */
51
	public function __construct(EndpointInterface $endpointInterface){
52
		$this->endpointInterface = $endpointInterface;
53
		$this->mapMethods();
54
	}
55
56
	/**
57
	 * @inheritdoc
58
	 */
59
	protected function mapMethods(){
60
		foreach((new ReflectionClass(EndpointInterface::class))->getMethods() as $method){
61
			$this->endpointInterfaceMap[$method->name] = $method;
62
		}
63
	}
64
65
	/**
66
	 * @param string $method
67
	 * @param array  $params
68
	 *
69
	 * @return mixed
70
	 * @throws \chillerlan\Threema\GatewayException
71
	 */
72
	public function __call(string $method, array $params){
73
74
		if(array_key_exists($method, $this->endpointInterfaceMap)){
75
			$reflectionMethod = new ReflectionMethod($this->endpointInterface, $this->endpointInterfaceMap[$method]->name);
76
77
			return $reflectionMethod->invokeArgs($this->endpointInterface, $params);
78
		}
79
80
		throw new GatewayException('method "'.$method.'" does not exist');
81
	}
82
83
}
84