|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Mediapart LaPresseLibre Library. |
|
5
|
|
|
* |
|
6
|
|
|
* CC BY-NC-SA <https://github.com/mediapart/lapresselibre> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Mediapart\LaPresseLibre; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
15
|
|
|
use Psr\Log\NullLogger; |
|
16
|
|
|
|
|
17
|
|
|
abstract class Endpoint implements LoggerAwareInterface |
|
18
|
|
|
{ |
|
19
|
|
|
use \Psr\Log\LoggerAwareTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var callable |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $callback; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Construct a new Endpoint to answer one of the API expected operation |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $operation Class name of the Endpoint |
|
30
|
|
|
* @param callable $callback |
|
31
|
|
|
* |
|
32
|
|
|
* @return Endpoint |
|
33
|
|
|
* @throws \LogicException if $operation is not a child of Endpoint |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function answer($operation, $callback) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!in_array(self::class, class_parents($operation))) { |
|
38
|
|
|
throw new \LogicException(sprintf( |
|
39
|
|
|
'%s is not a child of %s', |
|
40
|
|
|
$operation, |
|
41
|
|
|
self::class |
|
42
|
|
|
)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return new $operation($callback); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return Array |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function all() |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
'verification' => Operation\Verification::class, |
|
55
|
|
|
'account-creation' => Operation\AccountCreation::class, |
|
56
|
|
|
'account-update' => Operation\AccountUpdate::class, |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param callable $callback |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \InvalidArgumentException if the callback is not callable. |
|
64
|
|
|
*/ |
|
65
|
|
|
private function __construct($callback) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->callback = $callback; |
|
68
|
|
|
$this->logger = new NullLogger(); |
|
69
|
|
|
|
|
70
|
|
|
if (!is_callable($callback)) { |
|
71
|
|
|
throw new \InvalidArgumentException('Create endpoint with invalid callback'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Execute the endpoint. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $data |
|
79
|
|
|
* @param bool $isTesting |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
|
|
public function execute($data, $isTesting = false) |
|
84
|
|
|
{ |
|
85
|
|
|
$arguments = $this->resolveInput($data); |
|
86
|
|
|
$response = call_user_func($this->callback, $arguments, $isTesting); |
|
87
|
|
|
$result = $this->resolveOutput($response); |
|
88
|
|
|
|
|
89
|
|
|
$this->logger->debug( |
|
90
|
|
|
sprintf('Executed endpoint %s', get_class($this)), |
|
91
|
|
|
[$arguments, $result, $isTesting] |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
|
|
return $result; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Verify and complete the expected input values |
|
99
|
|
|
* when calling the web service. |
|
100
|
|
|
* |
|
101
|
|
|
* @param array $arguments |
|
102
|
|
|
* |
|
103
|
|
|
* @return array Arguments. |
|
104
|
|
|
*/ |
|
105
|
|
|
abstract protected function resolveInput(array $arguments); |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Verify and complete the expected output values |
|
109
|
|
|
* when calling the web service. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $arguments |
|
112
|
|
|
* |
|
113
|
|
|
* @return array Data |
|
114
|
|
|
*/ |
|
115
|
|
|
abstract protected function resolveOutput(array $data); |
|
116
|
|
|
} |
|
117
|
|
|
|