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

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas GASC committed Sep 11, 2017
0 parents commit 708b018
Show file tree
Hide file tree
Showing 30 changed files with 1,645 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/coverage/
/vendor/
/composer.lock
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: php

php:
- '5.6'
- '7.0'

before_script:
- composer self-update
- composer install --no-interaction --prefer-source --dev

script: ./vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-text --coverage-clover=coverage.clover
3 changes: 3 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CC BY-NC-SA

See https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode for more informations.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# `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.

## Usage

```php
$account_always_exists = 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,
];
};
$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)

## Installation

Simply install this package with [Composer](http://getcomposer.org/).

```bash
composer require mediapart/lapresselibre
```

## Read More

- Official `La Presse Libre` [documentation](https://github.com/NextINpact/LaPresseLibreSDK/wiki/) (fr).
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "mediapart/lapresselibre",
"type": "library",
"description": "Unofficial Client API of La Presse Libre project",
"license": "CC BY-NC-SA",
"authors": [
{
"name": "Thomas GASC",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Mediapart\\LaPresseLibre\\": "src/",
"Mediapart\\LaPresseLibre\\Tests\\": "tests/"
}
},
"require": {
"php": "^5.6|^7.0",
"ext-json": "*",
"ext-openssl": "*",
"ext-hash": "*",
"psr/http-message": "^1.0",
"psr/log": "^1.0",
"symfony/options-resolver": "^2.8|^3.3"
},
"require-dev": {
"zendframework/zend-diactoros": "^1.4",
"phpunit/phpunit": "^5.7|^6.3"
},
"suggest": {
"monolog/monolog": "Allows more advanced logging of the application flow"
}
}
60 changes: 60 additions & 0 deletions exemples/account-creation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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 'vendor/autoload.php';

use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
use Phly\Http\Response\SapiEmitter;
use Mediapart\LaPresseLibre\Security\Identity;
use Mediapart\LaPresseLibre\Security\Encryption;
use Mediapart\LaPresseLibre\Transaction;
use Mediapart\LaPresseLibre\Endpoint;
use Mediapart\LaPresseLibre\Operation\AccountCreation;

$public_key = 2;
$identity = new Identity('mGoMuzoX8u');
$encryption = new Encryption('UKKzV7sxiGx3uc0auKrUO2kJTT2KSCeg', '7405589013321961');
$request = ServerRequestFactory::fromGlobals();

try {
$transaction = new Transaction($identity, $encryption, $request);
$account_always_created = function ($data, $is_testing) use ($public_key) {
return [
'IsValid' => 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);
55 changes: 55 additions & 0 deletions exemples/account-update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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 'vendor/autoload.php';

use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
use Phly\Http\Response\SapiEmitter;
use Mediapart\LaPresseLibre\Security\Identity;
use Mediapart\LaPresseLibre\Security\Encryption;
use Mediapart\LaPresseLibre\Transaction;
use Mediapart\LaPresseLibre\Endpoint;
use Mediapart\LaPresseLibre\Operation\AccountUpdate;

$public_key = 2;
$identity = new Identity('mGoMuzoX8u');
$encryption = new Encryption('UKKzV7sxiGx3uc0auKrUO2kJTT2KSCeg', '7405589013321961');
$request = ServerRequestFactory::fromGlobals();

try {
$transaction = new Transaction($identity, $encryption, $request);
$account_always_updated = function ($data, $is_testing) use ($public_key) {
return [];
};
$account_update = Endpoint::answer(AccountUpdate::class, $account_always_updated);
$result = $transaction->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);
14 changes: 14 additions & 0 deletions exemples/register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require 'vendor/autoload.php';

use Mediapart\LaPresseLibre\Security\Encryption;
use Mediapart\LaPresseLibre\Registration;

$public_key = 2;
$encryption = new Encryption('UKKzV7sxiGx3uc0auKrUO2kJTT2KSCeg', '7405589013321961');
$registration = new Registration($public_key, $encryption);

$link = $registration->generateLink('[email protected]', 'username');

echo '<a href="'.$link.'">register into La Presse Libre</a>';
66 changes: 66 additions & 0 deletions exemples/verification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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 'vendor/autoload.php';

use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
use Phly\Http\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;
use Mediapart\LaPresseLibre\Operation\Verification;

$public_key = 2;
$identity = new Identity('mGoMuzoX8u');
$encryption = new Encryption('UKKzV7sxiGx3uc0auKrUO2kJTT2KSCeg', '7405589013321961');
$request = ServerRequestFactory::fromGlobals();

try {
$transaction = new Transaction($identity, $encryption, $request);
$account_always_exists = 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,
];
};
$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);
22 changes: 22 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<phpunit
colors="true"
bootstrap="vendor/autoload.php"
>

<testsuites>
<testsuite name="default">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
<exclude>
<directory>./vendor</directory>
<directory>./tests</directory>
</exclude>
</whitelist>
</filter>

</phpunit>
Loading

0 comments on commit 708b018

Please sign in to comment.