Current path: home/webdevt/cryptoimpot.fr/module/Application/src/Service/ApiHistorical/
?? Go up: /home/webdevt/cryptoimpot.fr/module/Application/src/Service
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Application\Service\ApiHistorical;
use Application\Entity\CoursDevise;
use Application\Entity\Devise;
use Application\Entity\DeviseMinApiCryptoCompare;
use Application\Entity\Source;
use Application\Mapper\CoursDeviseMapper;
use Application\Mapper\DeviseMapper;
use Application\Mapper\SourceMapper;
use Application\Service\ExchangeRateService;
use Doctrine\ORM\EntityManager;
/**
* Description of CoinAPIService
* https://docs.coinapi.io/#md-docs
*
* @author jimmy
*/
class MinAPICryptoCompareService
{
public function __construct(EntityManager $entityManager, CoursDeviseMapper $coursDeviseMapper, SourceMapper $sourceMapper) {
$this->entityManager = $entityManager;
//$this->exchangeRateService = new ExchangeRateService($this->entityManager);
$this->sourceMapper = $sourceMapper;
$this->mapperCoursDevise = $coursDeviseMapper;
$this->source = $this->sourceMapper->findByUrl('https://min-api.cryptocompare.com');
}
public function insertCoursDevise(Devise $devise, \DateTime $dateTime, $prixUnitaireEuro, Source $source)
{
$insertCoursDevise = new CoursDevise();
$insertCoursDevise->setDateCours($dateTime);
$insertCoursDevise->setDevise($devise);
$insertCoursDevise->setCoursEuro($prixUnitaireEuro);
$insertCoursDevise->setSource($source);
$this->mapperCoursDevise->insert($insertCoursDevise);
}
/*
* https://min-api.cryptocompare.com/data/pricehistorical?fsym=' . $asset&tsyms=EUR&ts=' . $dateTime->format('U')
*/
public function getUnitPriceInEur(Devise $devise, \DateTime $dateTime)
{
if (empty($devise->getDeviseMinApiCryptoCompare())) {
//$this->getIdFromCode($devise);
//$this->updateIds();
throw new \Exception('Absence d id Min API Crypto Compare pour : ' . $devise->getCode());
}
$urlApi = 'https://min-api.cryptocompare.com/data/pricehistorical?';
// asset fsym
$urlApi .= 'fsym=' . $devise->getDeviseMinApiCryptoCompare()->getSymbol();
$urlApi .= '&tsyms=EUR';
$urlApi .= '&ts=' . $dateTime->format('U');
$urlApi .= '&api_key=fd3aea02118639009c4a4de12b5f8a0f4619b08a814e04e9fa0aafc678127ea2';
// fetch data
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $urlApi);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$rawData = curl_exec($curl);
curl_close($curl);
// decode to array
$data = json_decode($rawData, true);
//var_dump($data[$devise->getDeviseMinApiCryptoCompare()->getSymbol()]);
//var_dump($data[$devise->getDeviseMinApiCryptoCompare()->getSymbol()]['EUR']);
if (!empty($data[$devise->getDeviseMinApiCryptoCompare()->getSymbol()]) && !empty($data[$devise->getDeviseMinApiCryptoCompare()->getSymbol()]['EUR'])) {
$prixUnitaireEuro = $data[$devise->getDeviseMinApiCryptoCompare()->getSymbol()]['EUR'];
$this->insertCoursDevise($devise, $dateTime, $prixUnitaireEuro, $this->source);
return $prixUnitaireEuro;
} else {
throw new \Exception('Prix en EUR le ' . $dateTime->format('Y-m-d H:i:s') . ' de : ' . $devise->getCode() . ' non trouvé sur ' . $this->source->getUrl());
}
}
// public function getIdFromCode($devise) {
// $urlApi = ' https://min-api.cryptocompare.com/data/v2/pair/mapping/fsym?';
// // asset fsym
//
//
// $urlApi .= 'fsym=' . $devise->getCode();
// $urlApi .= '&api_key=fd3aea02118639009c4a4de12b5f8a0f4619b08a814e04e9fa0aafc678127ea2';
// echo $urlApi;
// exit;
// // fetch data
// $curl = curl_init();
// curl_setopt($curl, CURLOPT_URL, $urlApi);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// $rawData = curl_exec($curl);
// curl_close($curl);
//
//// decode to array
// $data = json_decode($rawData, true);
// }
/*
* $summary true donne un résumé de la liste des coins
*
*
* okokokstring(686310) "{"Response":"Success","Message":"Summary coin list succesfully returned!","Data":{
* "42":{"Id":"4321","ImageUrl":"/media/35650717/42.jpg","Symbol":"42","FullName":"42 Coin (42)"},
*/
public function getAllCoinInfos($summary='true')
{
$urlApi = 'https://min-api.cryptocompare.com/data/all/coinlist';
$urlApi .= '?summary='.$summary;
$urlApi .= '&api_key=fd3aea02118639009c4a4de12b5f8a0f4619b08a814e04e9fa0aafc678127ea2';
// echo $urlApi; exit;
// fetch data
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $urlApi);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$rawData = curl_exec($curl);
// decode to array
$data = json_decode($rawData, true);
curl_close($curl);
if (empty($data || $data['Response'] != 'Success')) {
throw new \Exception('Liste non récupérée : ' . $data['Message'] . ' ('.$data['Response'].').');
}
return $data['Data'];
}
public function updateIds() {
try {
$allCoin = $this->getAllCoinInfos('false');
} catch(\Exception $e) {
echo $e->getMessage();
}
$mapperDevise = new DeviseMapper($this->entityManager);
foreach($allCoin as $row) {
$deviseFind = $mapperDevise->findByCode($row['Symbol']);
if (!empty($deviseFind)) {
// var_dump($deviseFind);
// var_dump($row['FullName']);
$deviseMiniCryptoCompareToInsert = new DeviseMinApiCryptoCompare($row);
$deviseFind->setDeviseMinApiCryptoCompare($deviseMiniCryptoCompareToInsert);
$mapperDevise->update($deviseFind);
// $deviseMiniCryptoCompareToInsert->setN
// $deviseFind->set
}
//if ($row['Symbol'] ==
}
echo 'update finished';
}
}