# ImageEngine PHP SDK library

A simple SDK library for Authentication and Control API(sm-auth-service - https://auth.imageengine.io (production)
or https://staging-auth.imageengine.io (staging) , ie-microservice - https://control-api.imageengine.io (production)
or https://staging-control-api.imageengine.io (staging)), written with PHP.

## Features

- [x] Simple and easy to use
- [x] Sign up with the Control API
- [x] Login with the Authentication API
- [x] Get the token from the Authentication API
- [x] Subscribe with the Control API
- [x] Create Engine with the Control API
- [x] Get Delivery addresses with the Control API
- [x] Register and get Delivery Address
- [x] Login and get Delivery Address
- [x] Get the Delivery Addresses
- [x] Get the Statistics

## Requirements

* PHP >= 7.4
* A [HTTP client](https://packagist.org/providers/php-http/client-implementation)
* A [PSR-7 implementation](https://packagist.org/providers/psr/http-message-implementation)
* API data from ImageEngine
* A storage to store the required information
* (optional) PHPUnit to run tests.

## Install

```bash
$ composer require imageengine/php-sdk php-http/guzzle7-adapter nyholm/psr7
```

Why `php-http/guzzle6-adapter`? We are decoupled from any HTTP messaging client with help
by [HTTPlug](http://httplug.io/). Read about clients in [Customize](#customize).

Copy `config/APIData.sample.php` to `config/APIData.php` and fill in the required fields.

```bash
$ mkdir config
$ cp vendor/imageengine/php-sdk/config/.gitignore config/.gitignore
$ cp vendor/imageengine/php-sdk/config/APIData.sample.php config/APIData.php
```

Add the following to your composer.json file "autoload" section

```text
"psr-4": {
    "ImageEngine\\PhpSdk\\Config\\": "config/"
}
```

Create a storage to store the required information. The storage must implement the `ImageEngine\PhpSdk\StorageInterface` interface.

```php
<?php
/**
 * This file contains the Option Storage class.
 *
 * @package ImageCDN
 */

namespace ImageEngine;

use ImageEngine\PhpSdk\Storage\StorageInterface;

class OptionStorage implements StorageInterface {
	public const PREFIX = 'image_cdn_';

	/**
	 * Get the value of an option.
	 *
	 * @param string $key The name of the option.
	 *
	 * @return mixed The value of the option.
	 */
	public function get( string $key ) {
		return get_option( self::PREFIX . $key );
	}

	/**
	 * Set the value of an option.
	 *
	 * @param string $key The name of the option.
	 * @param mixed  $value       The value of the option.
	 *
	 * @return bool True if the option was updated, false otherwise.
	 */
	public function set(string $key, string $value ) {
		return update_option( self::PREFIX . $key, $value );
	}

	/**
	 * Delete an option.
	 *
	 * @param string $key The name of the option.
	 *
	 * @return bool True if the option was deleted, false otherwise.
	 */
	public function delete($key ) {
		return delete_option( self::PREFIX . $key );
	}
}
```

### Customize

`imageengine/php-sdk` relies on `php-http/discovery` to find an installed HTTP client. 
You may specify a HTTP client yourself through the `ImageEngine\PhpSdk\ClientBuilder` constructor. 
A HTTP client must implement `Http\Client\HttpClient`. A list of community provided clients is found here:
https://packagist.org/providers/php-http/client-implementation

```php
$clientBuilder = new ClientBuilder(new Http\Adapter\Guzzle7\Client());
```

### Configure the HTTP client

Want to change, let's say, add default Accept header? You can add or create a plugin. Read more about
[HTTPlug plugins here](http://docs.php-http.org/en/latest/plugins/introduction.html#how-it-works).

```php
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;

$clientBuilder = new ClientBuilder(new Http\Adapter\Guzzle6\Client());
$clientBuilder->addPlugin(new HeaderDefaultsPlugin([
	'Accept' => 'application/json',
]));

$options = new Options([
	'client_builder' => $clientBuilder,
]);

$sdk = new Sdk($options);
```

## Basic usage of `imageengine/php-sdk` client

```php
<?php
...

// This file is generated by Composer
require_once __DIR__ . '/../vendor/autoload.php';

$client = new ImageEngine\PhpSdk\IEClient( new OptionStorage() );

try {
    $response = $client->login( $username, $password );
    ...
} catch ( \Exception $e ) {
    ...
}
```

From `$client` object, you can access to all ImageEngine PhpSdk functions.
