Files
crypto/backend/api/common/Service/Update.php
2021-07-14 10:08:48 -04:00

178 lines
5.5 KiB
PHP

<?php
namespace ProVM\Crypto\Common\Service;
use GuzzleHttp\Client;
use Carbon\Carbon;
use ProVM\Common\Factory\Model as Factory;
use ProVM\Crypto\Coin;
use ProVM\Crypto\Value;
class Update {
protected $factory;
protected $client;
public function __construct(Factory $factory, Client $client) {
$this->factory = $factory;
$this->client = $client;
$this->load();
}
public function load() {
$this->coins = [[], []];
$results = \ORM::for_table('coin_registers')->find_many();
foreach ($results as $result) {
$this->coins[$result->type] []= $result->coin_id;
}
}
protected $coins;
public function register(int $coin_id, int $type = 0) {
if (array_search($coin_id, $this->coins[$type]) !== false) {
return false;
}
$this->coins[$type] []= $coin_id;
$this->getHistorical($coin_id, $type);
$check = \ORM::for_table('coin_registers')->where('coin_id', $coin_id)->find_one();
if (!$check) {
return \ORM::raw_execute("INSERT INTO coin_registers (coin_id, type, date_Time) VALUES (?, ?, NOW())", [$coin_id, $type]);
}
return true;
}
protected function getHistorical(int $coin_id, int $type) {
$coin = $this->factory->find(Coin::class)->one($coin_id);
$f = Carbon::now();
$url = [];
switch ($type) {
case 0:
return $this->getHistoricalCrypto($coin);
case 1:
return $this->getHistoricalIndicador($coin);
default:
return false;
}
}
protected function getHistoricalCrypto(Coin $coin) {
$f = Carbon::now();
$url = [];
$url []= 'crypto';
$url []= 'historical';
$url []= $coin->identifier;
$url []= $f->copy()->subYears(10)->timestamp;
$url []= $f->timestamp;
$url = implode('/', $url);
$to = $this->factory->find(Coin::class)->where([['code', 'USD']])->one();
$response = $this->client->get($url);
$results = json_decode($response->getBody()->getContents());
$created = [];
foreach ($results->prices as $result) {
$d = Carbon::createFromTimestamp(substr($result[0], 0, -3));
$value = $result[1];
$data = [
'date_time' => $d->format('Y-m-d H:i:s'),
'coin_id' => $coin->id,
'value' => $value,
'unit_id' => $to->id
];
$value = Value::add($this->factory, $data);
$status = $value->save();
$created []= [
'value' => $value->toArray(),
'created' => $status
];
}
return $created;
}
protected function getHistoricalIndicador(Coin $coin) {
$f = Carbon::now();
$urls = [];
for ($i = 10; $i >= 0; $i --) {
$url = [];
$url []= 'indicador';
$url []= 'historical';
$url []= $coin->identifier;
$url []= $f->copy()->subYears($i)->year;
$urls []= implode('/', $url);
}
$to = $this->factory->find(Coin::class)->where([['code', 'CLP']])->one();
$created = [];
foreach ($urls as $url) {
$response = $this->client->get($url);
$results = json_decode($response->getBody()->getContents());
foreach ($results->serie as $result) {
$d = Carbon::parse($result->fecha);
$value = $result->valor;
$data = [
'date_time' => $d->format('Y-m-d H:i:s'),
'coin_id' => $coin->id,
'value' => $value,
'unit_id' => $to->id
];
$value = Value::add($this->factory, $data);
$status = $value->save();
$created []= [
'value' => $value->toArray(),
'created' => $status
];
}
}
return $created;
}
public function run() {
$created = [];
$created = array_merge($created, $this->getCryptos($this->coins[0]));
$created = array_merge($created, $this->getIndicadores($this->coins[1]));
return $created;
}
protected function getCryptos(array $coins) {
$created = [];
foreach ($coins as $coin_id) {
$coin = $this->factory->find(Coin::class)->one($coin_id);
$url = [];
$url []= 'crypto';
$url []= $coin->identifier;
$url = implode('/', $url);
$to = $this->factory->find(Coin::class)->where([['code', 'USD']])->one();
$response = $this->client->get($url);
$results = json_decode($response->getBody()->getContents());
$data = [
'date_time' => $results->n->last_updated_at,
'coin_id' => $coin->id,
'value' => 1 / $results->n->usd,
'unit_id' => $to->id
];
$value = Value::add($this->factory, $data);
$status = $value->save();
$created []= [
'value' => $value->toArray(),
'created' => $status
];
}
return $created;
}
protected function getIndicadores(array $coins) {
$created = [];
foreach ($coins as $coin_id) {
$coin = $this->factory->find(Coin::class)->one($coin_id);
$f = Carbon::now();
$url = [];
$url []= 'indicador';
$url []= $coin->identifier;
$url []= $f->format('d-m-Y');
$url = implode('/', $url);
$to = $this->factory->find(Coin::class)->where([['code', 'CLP']])->one();
$response = $this->client->get($url);
$results = json_decode($response->getBody()->getContents());
$data = [
'date_time' => $results->serie[0]->fecha,
'coin_id' => $coin->id,
'value' => $results->serie[0]->valor,
'unit_id' => $to->id
];
$value = Value::add($this->factory, $data);
$status = $value->save();
$created []= [
'value' => $value->toArray(),
'created' => $status
];
}
return $created;
}
}