Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
improve examples, doc and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas GASC committed Sep 20, 2017
1 parent 148a5e7 commit 0b6283f
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 203 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/coverage/
/vendor/
/composer.lock
/composer.lock
/examples/credentials.txt
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down
20 changes: 20 additions & 0 deletions examples/account-creation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* Web service de création de comptes.
*
* @see https://github.com/NextINpact/LaPresseLibreSDK/wiki/Fonctionnement-des-web-services#web-service-de-cr%C3%A9ation-de-comptes
*/

require 'bootstrap.php';

use Mediapart\LaPresseLibre\Operation\AccountCreation;

$handle(AccountCreation::class, function ($data, $is_testing) use ($public_key) {
return [
'IsValid' => true,
'PartenaireID' => $public_key,
'CodeUtilisateur' => $data['CodeUtilisateur'],
'CodeEtat' => AccountCreation::SUCCESS,
];
});
20 changes: 20 additions & 0 deletions examples/account-update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* Web service de mise à jour de comptes.
*
* @see https://github.com/NextINpact/LaPresseLibreSDK/wiki/Fonctionnement-des-web-services#web-service-de-mise-%C3%A0-jour-de-comptes
*/

require 'bootstrap.php';

use Mediapart\LaPresseLibre\Operation\AccountUpdate;

$handle(AccountUpdate::class, function ($data, $is_testing) use ($public_key) {
return [
'IsValid' => true,
'PartenaireID' => $public_key,
'CodeUtilisateur' => $data['CodeUtilisateur'],
'CodeEtat' => AccountUpdate::SUCCESS,
];
});
61 changes: 61 additions & 0 deletions examples/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

require '../vendor/autoload.php';

use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\SapiEmitter;
use Mediapart\LaPresseLibre\Security\Identity;
use Mediapart\LaPresseLibre\Security\Encryption;
use Mediapart\LaPresseLibre\Subscription\Type as SubscriptionType;
use Mediapart\LaPresseLibre\Transaction;
use Mediapart\LaPresseLibre\Endpoint;

/*
Configuration :
download the file : `https://partenaire.lapresselibre.fr/gestion/credentials`
into `credentials.txt`
*/
$config = json_decode(file_get_contents('credentials.txt'));

$public_key = $config->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);
};
25 changes: 25 additions & 0 deletions examples/verification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Web-Service de vérification de comptes existants.
*
* @see https://github.com/NextINpact/LaPresseLibreSDK/wiki/Fonctionnement-des-web-services#web-service-de-v%C3%A9rification-de-comptes-existants
*/

require 'bootstrap.php';

use Mediapart\LaPresseLibre\Operation\Verification;

$handle(Verification::class, function ($data, $is_testing) use ($public_key) {
$now = new DateTime('next year');

return [
'Mail' => $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,
];
});
60 changes: 0 additions & 60 deletions exemples/account-creation.php

This file was deleted.

55 changes: 0 additions & 55 deletions exemples/account-update.php

This file was deleted.

14 changes: 0 additions & 14 deletions exemples/register.php

This file was deleted.

66 changes: 0 additions & 66 deletions exemples/verification.php

This file was deleted.

7 changes: 7 additions & 0 deletions tests/EndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 0b6283f

Please sign in to comment.