Created
October 16, 2019 00:30
-
-
Save bayareawebpro/96bfd196a2dc546d89f09f1968c81ef3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\Leads\Leadspedia\Contracts; | |
use GuzzleHttp\Client; | |
use Illuminate\Support\Str; | |
use Illuminate\Support\Facades\Cache; | |
use Illuminate\Contracts\Support\Arrayable; | |
use function GuzzleHttp\Psr7\build_query; | |
class Contract implements Arrayable | |
{ | |
const NAME = 'shared4'; | |
const ID = ''; | |
const KEY = ''; | |
public $id; | |
public $key; | |
public $name; | |
public $offer; | |
public $multiplier; | |
public $minimum_buyers; | |
/** | |
* @var \Illuminate\Contracts\Cache\Repository | |
*/ | |
protected $cache; | |
public function __construct( | |
string $id, | |
string $key, | |
string $name, | |
?int $offer = null, | |
float $multiplier = 1, | |
int $minimum_buyers = 0 | |
) | |
{ | |
$this->id = $id; | |
$this->key = $key; | |
$this->name = $name; | |
$this->offer = $offer; | |
$this->multiplier = $multiplier; | |
$this->minimum_buyers = $minimum_buyers; | |
$this->cache = Cache::store('file'); | |
} | |
/** | |
* Make new instance of self (expects config array entry). | |
* @param array $attributes | |
* @return \Illuminate\Contracts\Foundation\Application|mixed | |
*/ | |
public static function make(array $attributes): Contract | |
{ | |
return app(self::class, $attributes); | |
} | |
/** | |
* Get the array representation of the data. | |
* @return array | |
*/ | |
public function toArray() | |
{ | |
return [ | |
'id' => $this->id, | |
'key' => $this->key, | |
'name' => $this->name, | |
'offer' => $this->offer, | |
'hits' => $this->hits, | |
'minimum_buyers' => $this->minimum_buyers, | |
'available_buyers' => $this->available_buyers, | |
'has_buyers' => $this->has_buyers, | |
'multiplier' => $this->multiplier, | |
'threshold' => $this->threshold, | |
'below_threshold' => $this->below_threshold, | |
'can_sell' => $this->can_sell, | |
]; | |
} | |
/** | |
* Get property value from getter. | |
* @param $property | |
* @return mixed|null | |
*/ | |
public function __get($property) | |
{ | |
$getter = "get" . Str::studly($property); | |
if (method_exists($this, $getter)) { | |
return $this->$getter(); | |
} | |
return $this->$property ?? null; | |
} | |
/** | |
* Get a cache key for the contract. | |
* @param string $key | |
* @return string | |
*/ | |
public function cacheKey(string $key): string | |
{ | |
return "{$this->name}:{$key}"; | |
} | |
/** | |
* Record a hit for distributing to contract. | |
* @return self | |
*/ | |
public function recordHit(): self | |
{ | |
$this->cache->increment($this->cacheKey('hits')); | |
return $this; | |
} | |
/** | |
* Clear recorded distribution hits. | |
* @return self | |
*/ | |
public function clearHits(): self | |
{ | |
$this->cache->put($this->cacheKey('hits'), 1); | |
return $this; | |
} | |
/** | |
* Get the count of total distributions to contract. | |
* @return int | |
* @throws \Psr\SimpleCache\InvalidArgumentException | |
*/ | |
public function getHits(): int | |
{ | |
return (int)$this->cache->get($this->cacheKey('hits'), 1); | |
} | |
/** | |
* Get minimum threshold for distribution allocation. | |
* @return int | |
* @throws \Psr\SimpleCache\InvalidArgumentException | |
*/ | |
public function getThreshold(): int | |
{ | |
return (int)round($this->cache->get("exclusive:hits", 1) * $this->multiplier, 0); | |
} | |
/** | |
* Get count of available buyers. | |
* @return int | |
*/ | |
public function getAvailableBuyers(): int | |
{ | |
if ($this->offer) { | |
return $this->getBuyersFromAPIResponse(); | |
} | |
return 0; | |
} | |
/** | |
* Does the contract have the minimum amount of buyers? | |
* @return bool | |
*/ | |
public function getCanSell(): bool | |
{ | |
return (bool)$this->has_buyers && $this->below_threshold; | |
} | |
/** | |
* Does the contract have the minimum amount of buyers? | |
* @return bool | |
*/ | |
protected function getHasBuyers(): bool | |
{ | |
return (bool)($this->available_buyers >= $this->minimum_buyers); | |
} | |
/** | |
* Does the contract have the minimum amount of buyers? | |
* @return bool | |
*/ | |
public function getBelowThreshold(): bool | |
{ | |
return (bool)($this->hits <= $this->threshold); | |
} | |
/** | |
* Get the available buyers count from the API response. | |
* @return int | |
*/ | |
public function getBuyersFromAPIResponse(): int | |
{ | |
return $this->cache->remember($this->cacheKey('buyers'), 30, function () { | |
try { | |
$client = new Client([ | |
'timeout' => 1, | |
'base_uri' => 'https://api.leadspedia.com/core/v2/custom/15311894/', | |
]); | |
$response = $client->getAsync('getAvailableContracts.do' . "?" . build_query([ | |
'offerID' => $this->offer, | |
'api_key' => '197a6dae99452fd400e67becb6cda0a5', | |
'api_secret' => 'e179d987f8bae9bb6b596262d8f96364', | |
])); | |
$response = \GuzzleHttp\json_decode($response->wait()->getBody()); | |
return data_get($response, 'response.total', 0); | |
} catch (\Exception $exception) { | |
return 0; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment