|
2 | 2 |
|
3 | 3 | require '../vendor/autoload.php';
|
4 | 4 |
|
5 |
| -use Zend\Diactoros\ServerRequestFactory; |
6 |
| -use Zend\Diactoros\Response; |
| 5 | +use Zend\Diactoros\ServerRequestFactory; |
| 6 | +use Zend\Diactoros\Response; |
7 | 7 | use Zend\Diactoros\Response\SapiEmitter;
|
8 | 8 | use Mediapart\LaPresseLibre\Security\Identity;
|
9 | 9 | use Mediapart\LaPresseLibre\Security\Encryption;
|
10 | 10 | use Mediapart\LaPresseLibre\Subscription\Type as SubscriptionType;
|
11 |
| -use Mediapart\LaPresseLibre\Transaction; |
12 |
| -use Mediapart\LaPresseLibre\Endpoint; |
| 11 | +use Mediapart\LaPresseLibre\Transaction; |
| 12 | +use Mediapart\LaPresseLibre\Endpoint; |
13 | 13 |
|
14 | 14 | /*
|
15 | 15 | Configuration :
|
|
29 | 29 | * @param string $operation
|
30 | 30 | * @param callable $callback
|
31 | 31 | */
|
32 |
| -$handle = function($operation, $callback) use ($identity, $encryption, $public_key) |
33 |
| -{ |
34 |
| - try { |
35 |
| - $request = ServerRequestFactory::fromGlobals(); |
36 |
| - $transaction = new Transaction($identity, $encryption, $request); |
37 |
| - $endpoint = Endpoint::answer($operation, $callback); |
38 |
| - $result = $transaction->process($endpoint); |
39 |
| - $status = 200; |
40 |
| - } catch (\InvalidArgumentException $e) { |
41 |
| - $result = $e->getMessage(); |
42 |
| - $status = 400; |
43 |
| - } catch (\UnexpectedValueException $e) { |
44 |
| - $result = $e->getMessage(); |
45 |
| - $status = 401; |
46 |
| - } catch (\Exception $e) { |
47 |
| - $result = 'Internal Error'; |
48 |
| - $status = 500; |
49 |
| - } finally { |
50 |
| - $response = (new Response()) |
51 |
| - ->withStatus($status) |
52 |
| - ->withHeader('X-PART', (string) $public_key) |
53 |
| - ->withHeader('X-LPL', $identity->sign($public_key)) |
54 |
| - ->withHeader('X-TS', (string) $identity->getDatetime()->getTimestamp()) |
55 |
| - ; |
56 |
| - $response->getBody()->write(200 != $status ? json_encode(['error' => $result]) : $result); |
57 |
| - } |
| 32 | +$handle = function ($operation, $callback) use ($identity, $encryption, $public_key) { |
| 33 | + try { |
| 34 | + $request = ServerRequestFactory::fromGlobals(); |
| 35 | + $transaction = new Transaction($identity, $encryption, $request); |
| 36 | + $endpoint = Endpoint::answer($operation, $callback); |
| 37 | + $result = $transaction->process($endpoint); |
| 38 | + $status = 200; |
| 39 | + } catch (\InvalidArgumentException $e) { |
| 40 | + $result = $e->getMessage(); |
| 41 | + $status = 400; |
| 42 | + } catch (\UnexpectedValueException $e) { |
| 43 | + $result = $e->getMessage(); |
| 44 | + $status = 401; |
| 45 | + } catch (\Exception $e) { |
| 46 | + $result = 'Internal Error'; |
| 47 | + $status = 500; |
| 48 | + } finally { |
| 49 | + $response = (new Response()) |
| 50 | + ->withStatus($status) |
| 51 | + ->withHeader('X-PART', (string) $public_key) |
| 52 | + ->withHeader('X-LPL', $identity->sign($public_key)) |
| 53 | + ->withHeader('X-TS', (string) $identity->getDatetime()->getTimestamp()) |
| 54 | + ; |
| 55 | + $response->getBody()->write(200 != $status ? json_encode(['error' => $result]) : $result); |
| 56 | + } |
58 | 57 |
|
59 |
| - $emitter = new SapiEmitter(); |
60 |
| - $emitter->emit($response); |
| 58 | + $emitter = new SapiEmitter(); |
| 59 | + $emitter->emit($response); |
61 | 60 | };
|
0 commit comments