Create MotivConnector service #519

This commit is contained in:
Kendrick Chan 2020-10-09 22:57:44 +08:00
parent 518121211c
commit c44fa66195
4 changed files with 97 additions and 4 deletions

View file

@ -11,3 +11,7 @@ test_gmap:
test_distance: test_distance:
path: /test/distance path: /test/distance
controller: App\Controller\TestController::distance controller: App\Controller\TestController::distance
test_motiv_connector:
path: /test/motiv_connector
controller: App\Controller\TestController::motivConnector

View file

@ -242,3 +242,10 @@ services:
App\EventSubscriber\LogSubscriber: App\EventSubscriber\LogSubscriber:
arguments: arguments:
$api_log_flag: "%env(API_LOGGING)%" $api_log_flag: "%env(API_LOGGING)%"
# motiv connector
App\Service\MotivConnector:
arguments:
$base_url: "%env(MOTIV_BASE_URL)%"
$sub_key: "%env(MOTIV_KEY)%"
$token: "%env(MOTIV_TOKEN)%"

View file

@ -6,14 +6,17 @@ use Catalyst\AuthBundle\Service\ACLGenerator;
use CrEOF\Spatial\PHP\Types\Geometry\Point; use CrEOF\Spatial\PHP\Types\Geometry\Point;
use App\Entity\Outlet;
use GuzzleHttp\Client as GuzzleClient;
use App\Service\MapTools;
use Doctrine\Common\Util\Debug;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\Common\Util\Debug;
use GuzzleHttp\Client as GuzzleClient;
use Catalyst\MenuBundle\Annotation\Menu; use Catalyst\MenuBundle\Annotation\Menu;
use App\Entity\Outlet;
use App\Service\MapTools;
use App\Service\MotivConnector;
class TestController extends Controller class TestController extends Controller
{ {
/** /**
@ -63,4 +66,20 @@ class TestController extends Controller
return $this->render('home.html.twig'); return $this->render('home.html.twig');
} }
/**
* @Menu(selected="home")
*/
public function motivConnector(MotivConnector $motiv)
{
$branches = [
'ZL0133',
'ZL0033',
];
$skus = [
'ECHD26AL-SPN00-L',
'ECHD26AL-SPN00-LX',
];
$motiv->getInventory($branches, $skus);
}
} }

View file

@ -0,0 +1,63 @@
<?php
namespace App\Service;
class MotivConnector
{
protected $base_url;
protected $sub_key;
protected $token;
public function __construct($base_url, $sub_key, $token)
{
$this->base_url = $base_url;
$this->sub_key = $sub_key;
$this->token = $token;
}
public function generateToken()
{
// NOTE: no need for now, since we use the pre-generated token
}
public function getInventory($branch_codes = [], $skus = [])
{
$body = [
'BranchCodes' => $branch_codes,
'Skus' => $skus
];
$body_text = json_encode($body);
$res = $this->curlPost('InventoryService', $body_text);
}
protected function curlPost($url, $body)
{
$curl = curl_init();
$options = [
CURLOPT_URL => $this->base_url . '/' . $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Ocp-Apim-Subscription-Key: ' . $this->sub_key,
'Authorization: Bearer ' . $this->token,
],
];
curl_setopt_array($curl, $options);
$res = curl_exec($curl);
curl_close($curl);
error_log('MOTIV API connector');
error_log(print_r($options, true));
error_log($res);
return $res;
}
}