This commit is contained in:
Juan Pablo Vial
2024-07-26 23:15:48 -04:00
parent 43bb7a83c8
commit 84861b5e57
24 changed files with 457 additions and 18 deletions

View File

@ -1,10 +1,12 @@
<?php
namespace Incoviba\Common\Alias;
use Incoviba\Common\Implement\Message;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Application as Base;
use Symfony\Component\Console;
use Incoviba\Common\Concept;
class Application extends Base
class Application extends Console\Application
{
public function __construct(protected ContainerInterface $container, string $name = 'UNKNOWN', string $version = 'UNKNOWN')
{

View File

@ -2,10 +2,10 @@
"name": "incoviba/cli",
"type": "project",
"require": {
"symfony/console": "^6.3",
"php-di/php-di": "^7.0",
"guzzlehttp/guzzle": "^7.8",
"monolog/monolog": "^3.5"
"monolog/monolog": "^3.5",
"php-di/php-di": "^7.0",
"symfony/console": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "^10.4",

View File

@ -1,3 +1,4 @@
<?php
$app->add($app->getContainer()->get(Incoviba\Command\Money\UF::class));
$app->add($app->getContainer()->get(Incoviba\Command\Money\IPC::class));
$app->add($app->getContainer()->get(Incoviba\Command\Money\UF\Update::class));

View File

@ -10,6 +10,10 @@ return [
$arr['resources'],
'commands'
]);
$arr['cache'] = implode(DIRECTORY_SEPARATOR, [
$arr['base'],
'cache'
]);
return (object) $arr;
}
];

View File

@ -2,8 +2,8 @@
use Psr\Container\ContainerInterface;
return [
Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) {
return new GuzzleHttp\Client([
Incoviba\Service\Login::class => function(ContainerInterface $container) {
$client = new GuzzleHttp\Client([
'base_uri' => $container->get('API_URL'),
'headers' => [
'Authorization' => [
@ -11,5 +11,23 @@ return [
]
]
]);
return new Incoviba\Service\Login(
$client,
$container->get(Psr\Log\LoggerInterface::class),
implode(DIRECTORY_SEPARATOR, [$container->get('folders')->cache, 'token']),
$container->get('API_USERNAME'),
$container->get('API_PASSWORD')
);
},
Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) {
$login = $container->get(Incoviba\Service\Login::class);
return new GuzzleHttp\Client([
'base_uri' => $container->get('API_URL'),
'headers' => [
'Authorization' => [
"Bearer {$login->getKey($container->get('API_KEY'))}"
]
]
]);
}
];

View File

@ -15,6 +15,7 @@ class Full extends Command
'comunas',
'money:ipc',
'money:uf',
'money:uf:update',
'proyectos:activos',
'ventas:cierres:vigentes',
'ventas:cuotas:hoy',

View File

@ -0,0 +1,47 @@
<?php
namespace Incoviba\Command\Money\UF;
use DateTimeImmutable;
use Symfony\Component\Console;
use Incoviba\Common\Alias\Command;
#[Console\Attribute\AsCommand(
name: 'money:uf:update'
)]
class Update extends Command
{
public function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
{
$this->logger->debug("Running {$this->getName()}");
$url = '/api/money/ufs';
$response = $this->client->get($url);
$output->writeln("GET {$url}");
if ($response->getStatusCode() !== 200) {
$this->logger->error("Error: [{$response->getStatusCode()}] {$response->getReasonPhrase()}");
return Console\Command\Command::FAILURE;
}
$data = json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY)['ufs'];
$zeros = [];
foreach ($data as $date => $value) {
if ($value === 0) {
$zeros[] = $date;
}
}
if (count($zeros) > 0) {
$output->writeln('Updating ' . count($zeros) . ' UFs');
$uri = '/api/money/ufs';
$response = $this->client->post($uri, [
'body' => http_build_query(['fechas' => $zeros]),
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded']
]);
$output->writeln("POST {$uri}");
if ($response->getStatusCode() !== 200) {
$this->logger->error("Error: [{$response->getStatusCode()}] {$response->getReasonPhrase()}");
return Console\Command\Command::FAILURE;
}
$body = json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY);
$output->writeln('Updated ' . count($body['ufs']) . ' UFs');
}
return Console\Command\Command::SUCCESS;
}
}

59
cli/src/Service/Login.php Normal file
View File

@ -0,0 +1,59 @@
<?php
namespace Incoviba\Service;
use Exception;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
class Login
{
public function __construct(protected ClientInterface $client, protected LoggerInterface $logger,
protected string $tokenFilename,
protected string $username, protected string $password) {}
public function login(): string
{
$url = '/api/login';
try {
$response = $this->client->request('POST', $url, [
'body' => http_build_query([
'username' => $this->username,
'password' => $this->password
]),
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded']
]);
} catch (ClientExceptionInterface $exception) {
$this->logger->error($exception);
return '';
}
if ($response->getStatusCode() !== 200) {
return '';
}
$body = $response->getBody()->getContents();
$data = json_decode($body, true);
if (!key_exists('token', $data)) {
$this->logger->error('Token not found');
return '';
}
file_put_contents($this->tokenFilename, $data['token']);
return $data['token'];
}
public function retrieveToken(): string
{
if (!file_exists($this->tokenFilename)) {
throw new Exception('Token file not found');
}
return file_get_contents($this->tokenFilename);
}
public function getKey(string $apiKey, string $separator = 'g'): string
{
try {
$token = $this->retrieveToken();
} catch (Exception) {
$token = $this->login();
}
return implode('', [md5($apiKey), $separator, $token]);
}
}