1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Phraseanet SDK. |
5
|
|
|
* |
6
|
|
|
* (c) Alchemy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhraseanetSDK; |
13
|
|
|
|
14
|
|
|
use PhraseanetSDK\Http\APIGuzzleAdapter; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @method Monitor getScheduler() |
19
|
|
|
* @method Monitor startTask($task_id) |
20
|
|
|
* @method Monitor stopTask($task_id) |
21
|
|
|
* @method Monitor getTask($task_id) |
22
|
|
|
* @method Monitor getTasks() |
23
|
|
|
*/ |
24
|
|
|
class Monitor |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var APIGuzzleAdapter |
28
|
|
|
*/ |
29
|
|
|
private $adapter; |
30
|
|
|
|
31
|
|
|
private static $mappings = array( |
32
|
|
|
'getScheduler' => array( |
33
|
|
|
'path' => 'v1/monitor/scheduler/', |
34
|
|
|
'entity' => 'PhraseanetSDK\Monitor\Scheduler', |
35
|
|
|
'query-keys' => array(), |
36
|
|
|
'result-property' => 'scheduler', |
37
|
|
|
'method' => 'GET', |
38
|
|
|
), |
39
|
|
|
'getTask' => array( |
40
|
|
|
'path' => 'v1/monitor/task/{task_id}/', |
41
|
|
|
'entity' => 'PhraseanetSDK\Monitor\Task', |
42
|
|
|
'query-keys' => array('task_id'), |
43
|
|
|
'result-property' => 'task', |
44
|
|
|
'method' => 'GET', |
45
|
|
|
), |
46
|
|
|
'startTask' => array( |
47
|
|
|
'path' => 'v1/monitor/task/{task_id}/start/', |
48
|
|
|
'entity' => 'PhraseanetSDK\Monitor\Task', |
49
|
|
|
'query-keys' => array('task_id'), |
50
|
|
|
'result-property' => 'task', |
51
|
|
|
'method' => 'POST', |
52
|
|
|
), |
53
|
|
|
'stopTask' => array( |
54
|
|
|
'path' => 'v1/monitor/task/{task_id}/stop/', |
55
|
|
|
'entity' => 'PhraseanetSDK\Monitor\Task', |
56
|
|
|
'query-keys' => array('task_id'), |
57
|
|
|
'result-property' => 'task', |
58
|
|
|
'method' => 'POST', |
59
|
|
|
), |
60
|
|
|
'getTasks' => array( |
61
|
|
|
'path' => 'v1/monitor/tasks/', |
62
|
|
|
'entity' => 'PhraseanetSDK\Monitor\Task', |
63
|
|
|
'query-keys' => array(), |
64
|
|
|
'result-property' => 'tasks', |
65
|
|
|
'method' => 'GET', |
66
|
|
|
), |
67
|
|
|
); |
68
|
|
|
|
69
|
6 |
|
public function __construct(APIGuzzleAdapter $adapter) |
70
|
|
|
{ |
71
|
6 |
|
$this->adapter = $adapter; |
72
|
6 |
|
} |
73
|
|
|
|
74
|
5 |
|
public function __call($name, $arguments) |
75
|
|
|
{ |
76
|
5 |
|
if (!isset(static::$mappings[$name])) { |
|
|
|
|
77
|
|
|
throw new \BadMethodCallException(sprintf('Method "%s::%s" does not exist.', get_class($this), $name)); |
78
|
|
|
} |
79
|
|
|
|
80
|
5 |
|
return $this->doCall($name, $arguments); |
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
|
private function doCall($name, $arguments) |
84
|
|
|
{ |
85
|
5 |
|
$parameters = array(); |
86
|
|
|
|
87
|
5 |
|
$n = 0; |
88
|
5 |
|
foreach (static::$mappings[$name]['query-keys'] as $key) { |
|
|
|
|
89
|
3 |
|
$parameters[$key] = $arguments[$n]; |
90
|
3 |
|
$n++; |
91
|
5 |
|
} |
92
|
|
|
|
93
|
5 |
|
$response = $this->adapter->call( |
94
|
5 |
|
static::$mappings[$name]['method'], |
|
|
|
|
95
|
5 |
|
array(static::$mappings[$name]['path'], $parameters) |
|
|
|
|
96
|
5 |
|
); |
97
|
|
|
$result = $response->getResult()->{static::$mappings[$name]['result-property']}; |
|
|
|
|
98
|
|
|
|
99
|
|
|
if (is_array($result)) { |
100
|
|
|
$output = new ArrayCollection(); |
101
|
|
|
foreach ($result as $entityData) { |
102
|
|
|
$output->add($entity = $this->getEntity($name, $entityData)); |
103
|
|
|
} |
104
|
|
|
} else { |
105
|
|
|
$output = $this->getEntity($name, $result); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $output; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function getEntity($name, $data) |
112
|
|
|
{ |
113
|
|
|
$entity = new static::$mappings[$name]['entity'](); |
|
|
|
|
114
|
|
|
|
115
|
|
|
array_walk($data, function ($value, $property) use ($entity) { |
116
|
|
|
$method = 'set'.implode('', array_map(function ($chunk) { |
117
|
|
|
return ucfirst($chunk); |
118
|
|
|
}, preg_split('/[-_]/', $property))); |
119
|
|
|
|
120
|
|
|
$ref = new \ReflectionParameter(array($entity, $method), 0); |
121
|
|
|
if (null !== $ref->getClass()) { |
122
|
|
|
if ('DateTime' === $ref->getClass()->name) { |
123
|
|
|
$value = \DateTime::createFromFormat(DATE_ATOM, $value) ?: null; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
call_user_func(array($entity, $method), $value); |
128
|
|
|
}); |
129
|
|
|
|
130
|
|
|
return $entity; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: