diff --git a/.gitignore b/.gitignore index 3f1ca35..588b16e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /coverage/ /vendor/ -/composer.lock \ No newline at end of file +/composer.lock +/examples/credentials.txt \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index a5b9754..de32114 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,3 +9,8 @@ before_script: - composer install --no-interaction --prefer-source --dev script: ./vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-text --coverage-clover=coverage.clover + +after_success: + - wget https://scrutinizer-ci.com/ocular.phar + - php ocular.phar code-coverage:upload --format=php-clover coverage.clover + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/README.md b/README.md index d60b8e4..18187df 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # `La Presse Libre` Client Library -Unofficial PHP SDK for the project [La Presse Libre](https://github.com/NextINpact/LaPresseLibreSDK). The difference with the official package offered by NextINpact is compatibility with PSR4, PSR7 and php7 environment. +[![Build Status](https://secure.travis-ci.org/mediapart/lapresselibre.svg?branch=master)](http://travis-ci.org/mediapart/lapresselibre) [![Code Coverage](https://codecov.io/gh/mediapart/lapresselibre/branch/master/graph/badge.svg)](https://scrutinizer-ci.com/g/mediapart/lapresselibre) [![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/mediapart/lapresselibre/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mediapart/lapresselibre) [![Total Downloads](https://poser.pugx.org/mediapart/lapresselibre/downloads.png)](https://packagist.org/packages/mediapart/lapresselibre) [![Latest Stable Version](https://poser.pugx.org/mediapart/lapresselibre/v/stable.png)](https://packagist.org/packages/mediapart/lapresselibre) + +Unofficial PHP SDK for the project [La Presse Libre](https://github.com/NextINpact/LaPresseLibreSDK). The difference with [the official package offered by NextINpact](https://github.com/NextINpact/LaPresseLibreSDK/tree/master/php) is compatibility with PSR4, PSR7 and php7 environment. ## Usage @@ -21,12 +23,7 @@ $account_always_exists = function ($data, $is_testing) use ($public_key) { $verification = Endpoint::answer(Verification::class, $account_always_exists); ``` -Detailed examples for each endpoints are available : - -- [exemples/verification.php](exemples/verification.php) -- [exemples/account-creation.php](exemples/account-creation.php) -- [exemples/account-update.php](exemples/account-update.php) -- [exemples/register.php](exemples/register.php) +Detailed examples for each endpoints are available in [examples/](examples/). ## Installation diff --git a/examples/account-creation.php b/examples/account-creation.php new file mode 100644 index 0000000..0bb4093 --- /dev/null +++ b/examples/account-creation.php @@ -0,0 +1,20 @@ + true, + 'PartenaireID' => $public_key, + 'CodeUtilisateur' => $data['CodeUtilisateur'], + 'CodeEtat' => AccountCreation::SUCCESS, + ]; +}); diff --git a/examples/account-update.php b/examples/account-update.php new file mode 100644 index 0000000..18f0da8 --- /dev/null +++ b/examples/account-update.php @@ -0,0 +1,20 @@ + true, + 'PartenaireID' => $public_key, + 'CodeUtilisateur' => $data['CodeUtilisateur'], + 'CodeEtat' => AccountUpdate::SUCCESS, + ]; +}); diff --git a/examples/bootstrap.php b/examples/bootstrap.php new file mode 100644 index 0000000..eeed3b2 --- /dev/null +++ b/examples/bootstrap.php @@ -0,0 +1,61 @@ +CodePartenaire; +$identity = new Identity($config->secret); +$encryption = new Encryption($config->Aes, $config->Iv); + +/** + * Handle an api endpoint of La Presse Libre. + * + * @param string $operation + * @param callable $callback + */ +$handle = function($operation, $callback) use ($identity, $encryption, $public_key) +{ + try { + $request = ServerRequestFactory::fromGlobals(); + $transaction = new Transaction($identity, $encryption, $request); + $endpoint = Endpoint::answer($operation, $callback); + $result = $transaction->process($endpoint); + $status = 200; + } catch (\InvalidArgumentException $e) { + $result = $e->getMessage(); + $status = 400; + } catch (\UnexpectedValueException $e) { + $result = $e->getMessage(); + $status = 401; + } catch (\Exception $e) { + $result = 'Internal Error'; + $status = 500; + } finally { + $response = (new Response()) + ->withStatus($status) + ->withHeader('X-PART', (string) $public_key) + ->withHeader('X-LPL', $identity->sign($public_key)) + ->withHeader('X-TS', (string) $identity->getDatetime()->getTimestamp()) + ; + $response->getBody()->write(200 != $status ? json_encode(['error' => $result]) : $result); + } + + $emitter = new SapiEmitter(); + $emitter->emit($response); +}; diff --git a/examples/verification.php b/examples/verification.php new file mode 100644 index 0000000..00f9da9 --- /dev/null +++ b/examples/verification.php @@ -0,0 +1,25 @@ + $data['Mail'], + 'CodeUtilisateur' => $data['CodeUtilisateur'], + 'TypeAbonnement' => SubscriptionType::MONTHLY, + 'DateExpiration' => $now->format("Y-m-d\TH:i:sO"), + 'DateSouscription' => $now->format("Y-m-d\TH:i:sO"), + 'AccountExist' => true, + 'PartenaireID' => $public_key, + ]; +}); diff --git a/exemples/account-creation.php b/exemples/account-creation.php deleted file mode 100644 index d3a7fcf..0000000 --- a/exemples/account-creation.php +++ /dev/null @@ -1,60 +0,0 @@ - true, - 'PartenaireID' => $public_key, - 'CodeUtilisateur' => $data['CodeUtilisateur'], - 'CodeEtat' => AccountCreation::SUCCESS, - ]; - }; - $account_creation = Endpoint::answer(AccountCreation::class, $account_always_created); - $result = $transaction->process($account_creation); - $status = 200; -} catch (\InvalidArgumentException $e) { - $result = $e->getMessage(); - $status = 400; -} catch (\UnexpectedValueException $e) { - $result = $e->getMessage(); - $status = 401; -} catch (\Exception $e) { - $result = 'Internal Error'; - $status = 500; -} finally { - $response = new Response( - 200 != $status ? json_encode(['error' => $result]) : $result, - $status, - [ - 'X-PART' => (string) $public_key, - 'X-LPL' => $identity->sign($public_key), - 'X-TS' => (string) $identity->getDatetime()->getTimestamp(), - ] - ); -} - -$emitter = new SapiEmitter(); -$emitter->emit($response); diff --git a/exemples/account-update.php b/exemples/account-update.php deleted file mode 100644 index 96f5f8e..0000000 --- a/exemples/account-update.php +++ /dev/null @@ -1,55 +0,0 @@ -process($account_update); - $status = 200; -} catch (\InvalidArgumentException $e) { - $result = $e->getMessage(); - $status = 400; -} catch (\UnexpectedValueException $e) { - $result = $e->getMessage(); - $status = 401; -} catch (\Exception $e) { - $result = 'Internal Error'; - $status = 500; -} finally { - $response = new Response( - 200 != $status ? json_encode(['error' => $result]) : $result, - $status, - [ - 'X-PART' => (string) $public_key, - 'X-LPL' => $identity->sign($public_key), - 'X-TS' => (string) $identity->getDatetime()->getTimestamp(), - ] - ); -} - -$emitter = new SapiEmitter(); -$emitter->emit($response); diff --git a/exemples/register.php b/exemples/register.php deleted file mode 100644 index 10c1192..0000000 --- a/exemples/register.php +++ /dev/null @@ -1,14 +0,0 @@ -generateLink('username@domain.tld', 'username'); - -echo 'register into La Presse Libre'; diff --git a/exemples/verification.php b/exemples/verification.php deleted file mode 100644 index 3dc5057..0000000 --- a/exemples/verification.php +++ /dev/null @@ -1,66 +0,0 @@ - $data['Mail'], - 'CodeUtilisateur' => $data['CodeUtilisateur'], - 'TypeAbonnement' => SubscriptionType::MONTHLY, - 'DateExpiration' => $now->format("Y-m-d\TH:i:sO"), - 'DateSouscription' => $now->format("Y-m-d\TH:i:sO"), - 'AccountExist' => true, - 'PartenaireID' => $public_key, - ]; - }; - $verification = Endpoint::answer(Verification::class, $account_always_exists); - $result = $transaction->process($verification); - $status = 200; -} catch (\InvalidArgumentException $e) { - $result = $e->getMessage(); - $status = 400; -} catch (\UnexpectedValueException $e) { - $result = $e->getMessage(); - $status = 401; -} catch (\Exception $e) { - $result = 'Internal Error'; - $status = 500; -} finally { - $response = new Response( - 200 != $status ? json_encode(['error' => $result]) : $result, - $status, - [ - 'X-PART' => (string) $public_key, - 'X-LPL' => $identity->sign($public_key), - 'X-TS' => (string) $identity->getDatetime()->getTimestamp(), - ] - ); -} - -$emitter = new SapiEmitter(); -$emitter->emit($response); diff --git a/tests/EndpointTest.php b/tests/EndpointTest.php index 018e970..dc4702e 100644 --- a/tests/EndpointTest.php +++ b/tests/EndpointTest.php @@ -37,4 +37,11 @@ public function testAnswerWithInvalidCallback() $endpoint = Endpoint::answer(Verification::class, 42); } + + public function testListAll() + { + $list = Endpoint::all(); + + $this->assertEquals(['verification', 'account-creation', 'account-update'], array_keys($list)); + } }