[![Codacy Badge](https://api.codacy.com/project/badge/Grade/032df50a272e4fc1a06576d68130066a)](https://app.codacy.com/app/gaalferov/ext-domain-connect?utm_source=github.com&utm_medium=referral&utm_content=gaalferov/ext-domain-connect&utm_campaign=Badge_Grade_Dashboard)
[![Build Status](https://travis-ci.com/gaalferov/ext-domain-connect.svg?branch=master)](https://travis-ci.com/gaalferov/ext-domain-connect)

# domain-connect
PHP client library for Domain Connect protocol.
For details of the protocol, please visit: https://domainconnect.org
Library offers Service Provider functionality in Sync mode.

## Specification reference
https://github.com/Domain-Connect/spec/blob/master/Domain%20Connect%20Spec%20Draft.adoc
- Version: 2.1
- Revision: 61

## Install
```bash
composer require gaalferov/php-ext-domain-connect
```

## Usage

### Sync flow

Just get the link. Discovery and template query part is solved automatically.
```php
<?php

require 'vendor/autoload.php';

use DomainConnect\Exception\DomainConnectException;
use DomainConnect\Services\TemplateService;

try {
    $applyUrl = (new TemplateService())->getTemplateSyncUrl(
        'foo.connect.domains',
        'exampleservice.domainconnect.org',
        'template1',
        [
            'IP' => '132.148.25.185',
            'RANDOMTEXT' => 'shm:1531371203:Hello world sync',
            'redirect_uri' => 'http://example.com',
            'state' => 'someState'
        ],
        $privateKey,
        $keyId
    );

    print_r($applyUrl);
} catch (DomainConnectException $e) {
    echo (sprintf('An error has occurred: %s', $e->getMessage()));
}

```

Output:
```text
https://domainconnect.1and1.com/sync/v2/domainTemplates/providers/exampleservice.domainconnect.org/services/template1/apply?domain=connect.domains&host=foo&IP=132.148.25.185&RANDOMTEXT=shm%3A1531371203%3AHello+world+sync&redirect_uri=http%3A%2F%2Fexample.com&state=someState
```

### Sync flow with signed request

```php
<?php

require 'vendor/autoload.php';

use DomainConnect\Exception\DomainConnectException;
use DomainConnect\Services\TemplateService;

try {
    $templateService = new TemplateService();
    $privateKey = "-----BEGIN RSA PRIVATE KEY-----\ \n-----END RSA PRIVATE KEY-----";
    $keyId = '_dck1';
    
    $applyUrl = $templateService->getTemplateSyncUrl(
        'foo.connect.domains',
        'exampleservice.domainconnect.org',
        'template1',
        [
            'IP' => '132.148.25.185',
            'RANDOMTEXT' => 'shm:1531371203:Hello world sync',
            'redirect_uri' => 'http://example.com',
            'state' => 'someState'
        ],
        $privateKey,
        $keyId
    );

    print_r($applyUrl);
} catch (DomainConnectException $e) {
    echo (sprintf('An error has occurred: %s', $e->getMessage()));
}

```

Output:
```text
https://domainconnect.1and1.com/sync/v2/domainTemplates/providers/exampleservice.domainconnect.org/services/template1/apply?domain=connect.domains&host=foo&IP=132.148.25.185&RANDOMTEXT=shm%3A1531371203%3AHello+world+sync&redirect_uri=http%3A%2F%2Fexample.com&state=someState&sig=HOoCBTFNIjWK3Qjy7CtWLB9HOcVvaGYFD3XsaI%2BZm%2BHtm%2FgsK66kfEfbi7Y8uoNOAbJXx3xfnbKrdASNa%2FPjQQKLk11NV%2BLNf3iDwkg7cOV9l54L2PqHFymOgu%2BiXr3I5aUkmA%2F19YAqiz3VJrh3K1XGeqc100cyZo%2BWq90bVcyWGVhIf5vYZEsU%2FzprckhmQdipqFfAB6XB4i72RzdxCKpBUFsyWcWYX80cz873pLN42uV%2BwGFUhSQ0qDC8d60n5m1f8h9Fdmik6%2FD%2BnmPpfK4Ge%2BHGnnq%2FpvR3nb95x9DKDHOd%2BfaIVcLhIZEbbVxIa57bGH%2FRUQiMbApBqkM4YQ%3D%3D&key=_dck1
```

### Request Options (guzzle) 
```php
<?php

require 'vendor/autoload.php';

use DomainConnect\Exception\DomainConnectException;
use DomainConnect\Services\TemplateService;

try {
    $templateService = new TemplateService([
        'proxy' => [
            'http'  => 'tcp://localhost:8125',
            'https' => 'tcp://localhost:9124',
            'no' => ['.mit.edu', 'foo.com']
        ]
    ]);
    
    $applyUrl = $templateService->getTemplateSyncUrl(
        'foo.connect.domains',
        'exampleservice.domainconnect.org',
        'template1',
        [
            'IP' => '132.148.25.185',
            'RANDOMTEXT' => 'shm:1531371203:Hello world sync',
            'redirect_uri' => 'http://example.com',
            'state' => 'someState'
        ],
    );

    print_r($applyUrl);
} catch (DomainConnectException $e) {
    echo (sprintf('An error has occurred: %s', $e->getMessage()));
}

```

## Tests
To run tests use next command: `composer tests` or `php vendor/bin/phpunit ./tests`

## TODOs
- Async flow
