1
|
|
|
<?php namespace Rossedman\Teamwork; |
2
|
|
|
|
3
|
|
|
use Rossedman\Teamwork\Contracts\RequestableInterface; |
4
|
|
|
use Rossedman\Teamwork\Exceptions\ClassNotCreatedException; |
5
|
|
|
|
6
|
|
|
class Factory { |
7
|
|
|
|
8
|
|
|
protected $client; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @param RequestableInterface $client |
12
|
|
|
*/ |
13
|
|
|
public function __construct(RequestableInterface $client) |
14
|
|
|
{ |
15
|
|
|
$this->client = $client; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param $method |
20
|
|
|
* @param $parameters |
21
|
|
|
* |
22
|
|
|
* @return mixed |
23
|
|
|
* @throws ClassNotCreatedException |
24
|
|
|
*/ |
25
|
|
|
public function __call($method, $parameters) |
26
|
|
|
{ |
27
|
|
|
$class = $this->getQualifiedName($method); |
28
|
|
|
|
29
|
|
|
$this->doesClassExist($class); |
30
|
|
|
|
31
|
|
|
if($this->paramIsId($parameters) == true) |
32
|
|
|
{ |
33
|
|
|
return new $class($this->client, $parameters[0]); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return new $class($this->client); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get Namespace |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
private function getNamespace() |
45
|
|
|
{ |
46
|
|
|
$reflection = new \ReflectionClass($this); |
47
|
|
|
|
48
|
|
|
return $reflection->getNamespaceName(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get Fully Qualified Name |
53
|
|
|
* |
54
|
|
|
* build and return fully qualified name |
55
|
|
|
* for class to instantiate |
56
|
|
|
* |
57
|
|
|
* @param $method |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
protected function getQualifiedName($method) |
62
|
|
|
{ |
63
|
|
|
return $this->getNamespace().'\\'.ucfirst($method); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Parameter Has ID |
68
|
|
|
* |
69
|
|
|
* is there a parameter being passed in, and is it |
70
|
|
|
* an integer? |
71
|
|
|
* |
72
|
|
|
* @param $parameters |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
protected function paramIsId($parameters) |
77
|
|
|
{ |
78
|
|
|
if($parameters == null) return null; |
79
|
|
|
|
80
|
|
|
if ( ! is_int($parameters[0])) |
81
|
|
|
{ |
82
|
|
|
throw new \InvalidArgumentException("This is not a valid ID"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $class |
90
|
|
|
* |
91
|
|
|
* @throws ClassNotCreatedException |
92
|
|
|
*/ |
93
|
|
|
protected function doesClassExist($class) |
94
|
|
|
{ |
95
|
|
|
if ( ! class_exists($class)) |
96
|
|
|
{ |
97
|
|
|
throw new ClassNotCreatedException("Class $class could not be created."); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|