PHP code example of apsconnect / connect-sdk-migration-framework

1. Go to this page and download the library: Download apsconnect/connect-sdk-migration-framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

apsconnect / connect-sdk-migration-framework example snippets




namespace App\Providers;

use GuzzleHttp\Client;
use Connect\Config;
use Connect\Request;
use Connect\Fail;
use Connect\Middleware\Migration\Handler as MigrationHandler;
use Connect\Runtime\ServiceProvider;
use Pimple\Container;
use Psr\Log\LoggerInterface;
use Connect\Middleware\Migration\Exceptions\MigrationParameterFailException;
use Connect\Middleware\Migration\Exceptions\MigrationAbortException;

/**
 * Class MigrationServiceProvider
 * @package App\Providers
 */
class MigrationServiceProvider extends ServiceProvider
{
    /**
     * Create a Migrate middleware
     * @param Container $container
     * @return MigrationHandler
     */
    public function register(Container $container)
    {
        return new MigrationHandler([
            'logger' => $container['logger'],
            'config' => $container['config'],
            'transformations' => [
                'email' => function ($migrationData, Request $request, Config $config, LoggerInterface $logger) {
                    $logger->info("[MIGRATION::{$request->id}] Processing teamAdminEmail parameter.");
                    
                    $client = new Client();
                    $response = $client->request('GET', strtr($config->service->migration->url, [
                        '{instance}' => $migrationData->instance,
                        '{subscription}' => $migrationData->subscription
                    ]) . '/teamAdminEmail', [
                        'headers' => [
                            'http_errors' => false,
                            'Authorization' => 'Basic ' . $migrationData->token
                        ]
                    ]);
                    
                    if ($response->getStatusCode() !== 200) {
                        throw new MigrationParameterFailException("Missing field teamAdminEmail", $response->getStatusCode());
                    }
                    
                    $data = json_decode($response->getBody()->getContents());
                    
                    if(empty($data->value)) {
                        throw new MigrationParameterFailException("Missing field teamAdminEmail.", 400);
                    }
                    
                    if(!filter_var($data->value, FILTER_VALIDATE_EMAIL)) {
                        throw new MigrationParameterFailException("Wrong field teamAdminEmail must be an email.", 400);
                    }
                    
                    return strtolower($data->value);
                },
                'team_id' => function ($migrationData, Request $request, Config $config, LoggerInterface $logger) {
                    $logger->info("[MIGRATION::{$request->id}] Processing teamId parameter.");
                    
                    $client = new Client();
                    $response = $client->request('GET', strtr($config->service->migration->url, [
                        '{instance}' => $migrationData->instance,
                        '{subscription}' => $migrationData->subscription
                    ]) . '/teamId', [
                        'headers' => [
                            'http_errors' => false,
                            'Authorization' => 'Basic ' . $migrationData->token
                        ]
                    ]);
                    
                    if ($response->getStatusCode() !== 200) {
                        throw new MigrationParameterFailException("Missing field teamId", $response->getStatusCode());
                    }
                    
                    $data = json_decode($response->getBody()->getContents());
                    
                    if(empty($data->value)) {
                        throw new MigrationParameterFailException("Missing field teamId.", 400);
                    }
                    
                    return strtolower($data->value);
                },
                'team_name' => function ($migrationData, Request $request, Config $config, LoggerInterface $logger) {
                    $logger->info("[MIGRATION::{$request->id}] Processing teamName parameter.");
                    
                    $client = new Client();
                    $response = $client->request('GET', strtr($config->service->migration->url, [
                        '{instance}' => $migrationData->instance,
                        '{subscription}' => $migrationData->subscription
                    ]) . '/teamName', [
                        'headers' => [
                            'http_errors' => false,
                            'Authorization' => 'Basic ' . $migrationData->token
                        ]
                    ]);
                    
                    if ($response->getStatusCode() !== 200) {
                        throw new MigrationParameterFailException("Missing field teamName", $response->getStatusCode());
                    }
                    
                    $data = json_decode($response->getBody()->getContents());
                    
                    if(empty($data->value)) {
                        throw new MigrationParameterFailException("Missing field teamName.", 400);
                    }
                    
                    return ucwords($data->teamName);
                },
            ],
            'onSuccess' => function($migrationData, Request $request, Config $config, LoggerInterface $logger) {
                $logger->info("Migration for request {$request->id} successful!");
            },
            'onFail' => function($migrationData, Request $request, Config $config, LoggerInterface $logger, MigrationAbortException $e) {
                throw new Fail("Failing request {$request->id} due: " . $e->getMessage());
            }
        ]);
    }
}



namespace App;

use Connect\Logger;
use Connect\Middleware\Migration\Handler;
use Connect\FulfillmentAutomation;

/**
 * Class ProductFulfillment
 * @package App
 * @property Logger $logger
 * @property Handler $migration
 */
class ProductFulfillment extends FulfillmentAutomation
 {
    public function processRequest($request)
    {
        switch ($request->type) {
            case "purchase":
                
                $request = $this->migration->migrate($request);
                
                // the migrate() method returns a new request object with the
                // migrated data populated, we only need to update the params 
                // and approve the fulfillment to complete the migration.
                
                $this->updateParameters($request, $request->asset->params);
                
                // more code...
        }

    }
    
    public function processTierConfigRequest($tierConfigRequest)
    {
        // NOT MIGRABLE! (YET)
    }
}