Migrate once when loaded

This commit is contained in:
2021-04-13 21:02:23 -04:00
parent 60cd73078c
commit b6bc007590
8 changed files with 165 additions and 87 deletions

View File

@ -4,83 +4,14 @@ namespace ProVM\Money\Common\Controller;
use Psr\Container\ContainerInterface as Container; use Psr\Container\ContainerInterface as Container;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use GuzzleHttp\ClientInterface as Client;
use Carbon\Carbon;
use ProVM\Common\Define\Controller\Json; use ProVM\Common\Define\Controller\Json;
use ProVM\Common\Factory\Model as ModelFactory; use ProVM\Money\Common\Service\Update as Updater;
use ProVM\Money\Currency;
use ProVM\Money\Source;
use ProVM\Money\Value;
class Update { class Update {
use Json; use Json;
protected function get(Client $client, string $url, bool $exception = true) { public function __invoke(Request $request, Response $response, Updater $updater): Response {
$res = $client->get($url); $output = $updater->update();
if ($res->getStatusCode() < 200 or $res->getStatusCode() >= 300) {
if ($exception) {
throw new \Exception('Url ' . $url . ' not connected.');
}
return false;
}
return json_decode($res->getBody());
}
protected function baseMap($unit) {
$map = [
'Dólar' => 'US Dollar',
'Pesos' => 'Peso Chileno'
];
return $map[$unit] ?? $unit;
}
public function __invoke(Request $request, Response $response, Client $client, ModelFactory $factory, Container $container): Response {
ini_set('max_execution_time', 300);
$sources = $factory->find(Source::class)->many();
$date = Carbon::now();
$output = ['count' => 0, 'values' => []];
foreach ($sources as $source) {
$url = str_replace([
'{year}',
'{month}',
'{day}',
'{hour}',
'{minute}',
'{second}'
], [
$date->year,
$date->month,
$date->day,
$date->hour,
$date->minute,
$date->second
], $source->url);
$b = $this->get($client, $url, false);
if ($b === false) {
continue;
}
$base = $factory->find(Currency::class)->where([
['name', '%' . $this->baseMap($b->unidad_medida) . '%', 'like']
])->one();
if (!$base) {
continue;
}
foreach ($b->serie as $info) {
$f = Carbon::parse($info->fecha);
$data = [
'currency_id' => $source->currency()->id,
'date_time' => $f->format('Y-m-d H:i:s'),
'value' => $info->valor,
'base_id' => $base->id
];
$result = Value::add($factory, $data);
$output['values'] []= $result;
if ($result->created === true) {
$output['count'] ++;
}
}
}
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
} }

View File

@ -0,0 +1,31 @@
<?php
namespace ProVM\Money\Common\Middleware;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Http\Message\ResponseInterface as Response;
use Phinx\Wrapper\TextWrapper;
use GuzzleHttp\ClientInterface as Client;
use ProVM\Money\Common\Service\Update as Updater;
class Migrate {
protected $phinx;
protected $updater;
public function __construct(TextWrapper $phinx, Updater $updater) {
$this->phinx = $phinx;
$this->updater = $updater;
}
public function __invoke(Request $request, Handler $handler): Response {
$query = "SHOW TABLES";
$st = \ORM::get_db()->query($query);
$r = $st->fetchAll(\PDO::FETCH_ASSOC);
if (count($r) == 0) {
$this->phinx->getMigrate();
$this->phinx->getSeed();
$this->updater->update();
}
return $handler->handle($request);
}
}

View File

@ -0,0 +1,90 @@
<?php
namespace ProVM\Money\Common\Service;
use GuzzleHttp\ClientInterface as Client;
use Carbon\Carbon;
use ProVM\Common\Factory\Model as ModelFactory;
use ProVM\Money\Currency;
use ProVM\Money\Source;
use ProVM\Money\Value;
class Update {
protected $factory;
protected $client;
public function __construct(ModelFactory $factory, Client $client) {
$this->factory = $factory;
$this->client = $client;
}
protected function get(Client $client, string $url, bool $exception = true) {
$res = $client->get($url);
if ($res->getStatusCode() < 200 or $res->getStatusCode() >= 300) {
if ($exception) {
throw new \Exception('Url ' . $url . ' not connected.');
}
return false;
}
return json_decode($res->getBody());
}
protected function baseMap($unit) {
$map = [
'Dólar' => 'US Dollar',
'Pesos' => 'Peso Chileno'
];
return $map[$unit] ?? $unit;
}
protected function buildUrl(Source $source) {
$date = Carbon::now();
return str_replace([
'{year}',
'{month}',
'{day}',
'{hour}',
'{minute}',
'{second}'
], [
$date->year,
$date->month,
$date->day,
$date->hour,
$date->minute,
$date->second
], $source->url);
}
public function update() {
ini_set('max_execution_time', 300);
$sources = $this->factory->find(Source::class)->many();
$output = ['count' => 0, 'values' => []];
foreach ($sources as $source) {
$url = $this->buildUrl($source);
$b = $this->get($this->client, $url, false);
if ($b === false) {
continue;
}
$base = $this->factory->find(Currency::class)->where([
['name', '%' . $this->baseMap($b->unidad_medida) . '%', 'like']
])->one();
if ($base === false) {
continue;
}
foreach ($b->serie as $info) {
$f = Carbon::parse($info->fecha);
$data = [
'currency_id' => $source->currency()->id,
'date_time' => $f->format('Y-m-d H:i:s'),
'value' => $info->valor,
'base_id' => $base->id
];
$result = Value::add($this->factory, $data);
$output['values'] []= $result;
if ($result->created === true) {
$output['count'] ++;
}
}
}
return $output;
}
}

View File

@ -11,6 +11,7 @@
"provm/controller": "dev-master", "provm/controller": "dev-master",
"vlucas/phpdotenv": "^5.3", "vlucas/phpdotenv": "^5.3",
"guzzlehttp/guzzle": "^7.3", "guzzlehttp/guzzle": "^7.3",
"robmorgan/phinx": "^0.12.5",
"nesbot/carbon": "^2.46" "nesbot/carbon": "^2.46"
}, },
"license": "MIT", "license": "MIT",
@ -22,7 +23,6 @@
], ],
"require-dev": { "require-dev": {
"phpunit/phpunit": "^8.5", "phpunit/phpunit": "^8.5",
"robmorgan/phinx": "^0.12.5",
"odan/phinx-migrations-generator": "^5.4" "odan/phinx-migrations-generator": "^5.4"
}, },
"autoload": { "autoload": {

View File

@ -12,28 +12,28 @@ return
'default_environment' => 'development', 'default_environment' => 'development',
'production' => [ 'production' => [
'adapter' => 'mysql', 'adapter' => 'mysql',
'host' => 'localhost', 'host' => $_ENV['DB_HOST'],
'name' => 'production_db', 'name' => $_ENV['DB_NAME'],
'user' => 'root', 'user' => $_ENV['DB_USER'],
'pass' => '', 'pass' => $_ENV['DB_PASSWORD'],
'port' => '3306', 'port' => '3306',
'charset' => 'utf8', 'charset' => 'utf8',
], ],
'development' => [ 'development' => [
'adapter' => 'mysql', 'adapter' => 'mysql',
'host' => 'localhost', 'host' => $_ENV['DB_HOST'],
'name' => 'money_dev', 'name' => $_ENV['DB_NAME'],
'user' => 'money', 'user' => $_ENV['DB_USER'],
'pass' => 'money_pass', 'pass' => $_ENV['DB_PASSWORD'],
'port' => '3307', 'port' => '3306',
'charset' => 'utf8', 'charset' => 'utf8',
], ],
'testing' => [ 'testing' => [
'adapter' => 'mysql', 'adapter' => 'mysql',
'host' => 'localhost', 'host' => $_ENV['DB_HOST'],
'name' => 'testing_db', 'name' => $_ENV['DB_NAME'],
'user' => 'root', 'user' => $_ENV['DB_USER'],
'pass' => '', 'pass' => $_ENV['DB_PASSWORD'],
'port' => '3306', 'port' => '3306',
'charset' => 'utf8', 'charset' => 'utf8',
] ]

View File

@ -0,0 +1,2 @@
<?php
$app->add($app->getContainer()->get(ProVM\Money\Common\Middleware\Migrate::class));

View File

@ -22,5 +22,11 @@ return [
'routes' 'routes'
]); ]);
return (object) $arr; return (object) $arr;
}) }),
'phinx' => function(Container $c) {
return implode(DIRECTORY_SEPARATOR, [
$c->get('locations')->base,
'phinx.php'
]);
}
]; ];

View File

@ -4,5 +4,23 @@ use Psr\Container\ContainerInterface as Container;
return [ return [
GuzzleHttp\ClientInterface::class => function(Container $c) { GuzzleHttp\ClientInterface::class => function(Container $c) {
return new GuzzleHttp\Client(); return new GuzzleHttp\Client();
},
ProVM\Common\Factory\Model::class => function(Container $c) {
return new ProVM\Common\Factory\Model();
},
ProVM\Money\Common\Service\Update::class => function(Container $c) {
return new ProVM\Money\Common\Service\Update($c->get(ProVM\Common\Factory\Model::class), $c->get(GuzzleHttp\ClientInterface::class));
},
ProVM\Money\Common\Middleware\Migrate::class => function(Container $c) {
return new ProVM\Money\Common\Middleware\Migrate($c->get(Phinx\Wrapper\TextWrapper::class), $c->get(ProVM\Money\Common\Service\Update::class));
},
Phinx\Console\PhinxApplication::class => function(Container $c) {
return new Phinx\Console\PhinxApplication();
},
Phinx\Wrapper\TextWrapper::class => function(Container $c) {
$options = [
'configuration' => $c->get('phinx')
];
return new Phinx\Wrapper\TextWrapper($c->get(Phinx\Console\PhinxApplication::class), $options);
} }
]; ];