unioil-loyalty-app/app/Libraries/StratuscastLibrary.php

125 lines
2.7 KiB
PHP
Executable File

<?php
/**
* Created by PhpStorm.
* User: root
* Date: 10/18/18
* Time: 10:45 AM
*/
namespace App\Libraries;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
use Illuminate\Support\Facades\Config;
class StratuscastLibrary
{
public function __construct() {
$this->url = Config::get('app.stratuscast');
$this->username = Config::get('app.stratuscastUsername');
$this->password = Config::get('app.stratuscastPassword');
$this->uri = new Uri($this->url);
}
protected $uri;
protected $url;
protected $username;
protected $password;
public $response;
public function getAllFuelPrice() {
$this->create_request("stations_fuel_prices/");
}
public function getFuelPriceBystation($station) {
$this->create_request("stations_fuel_prices/read_single/".$station);
}
public function getCityAndProvince() {
$this->create_request("city_provinces/");
}
public function getCity() {
$this->create_request("city/");
}
public function getProvince() {
$this->create_request("province/");
}
public function getAboutUs() {
$this->create_request("about_us/");
}
public function getProducts() {
$this->create_request("products/");
}
public function getProduct($id) {
$this->create_request("products/read_single/".$id);
}
public function create_request($params)
{
$client = new Client();
$request = $client->request('GET', $this->url.'/'.$params, [
'headers' => [
'Username' => $this->username,
'Password' => $this->password
]
]);
switch ($params) {
case 'about_us/':
$indata = true;
break;
default:
$indata = false;
break;
}
$this->setResponse($request,$indata);
}
/**
* @return $response
*/
public function getResponse()
{
return $this->response;
}
/**
* @param $response
*/
function setResponse($response , $indata)
{
$result = $response->getStatusCode() != 200 ? null : json_decode($response->getBody()->getContents());
$return = [];
$return['data'] = $result;
if ($indata){
$return['data'] = $result[0];
}
if (count($result) == 1){
$return['data'] = $result[0];
}
$return['status_code'] = $response->getStatusCode();
$this->response = $return;
if ($indata){
$return['data'] = $result[0][0];
$this->response = $return;
}
}
}