Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ TRUSTED_PROXIES=127.0.0.1,REMOTE_ADDR

###> app ###
API_TOKEN=abcxyz
###> app ###
# Token de apiperu.dev - Registrate gratis en https://apiperu.dev/auth/register
CONSULT_TOKEN=1dfaa87bade0100f2a439394a31dd031c3cae1fc984d5cafe05a4165fe0c877d
###> app ###
10 changes: 10 additions & 0 deletions .phpactor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "/phpactor.schema.json",
"symfony.enabled": true,
"indexer.exclude_patterns": [
"/vendor/**/Tests/**/*",
"/vendor/**/tests/**/*",
"/var/cache/**/*",
"/vendor/composer/**/*"
]
}
16 changes: 9 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ RUN apk update && apk add --no-cache \

COPY . .

RUN git apply docker/drift-kernel.patch
RUN git apply docker/drift-adapter.patch
RUN composer install --no-interaction --no-dev --optimize-autoloader --ignore-platform-reqs && \
composer require drift/server:0.1.25 --ignore-platform-reqs && \
composer require react/filesystem:^0.1.2 --ignore-platform-reqs && \
composer dump-autoload --optimize --no-dev --classmap-authoritative && \
composer dump-env prod --empty && \
RUN git init && git add -A && \
git apply docker/drift-kernel.patch && \
git apply docker/drift-adapter.patch
RUN composer install --no-plugins --no-interaction --no-dev --optimize-autoloader --ignore-platform-reqs && \
composer require drift/server:0.1.25 --no-plugins --ignore-platform-reqs && \
composer require react/filesystem:^0.1.2 --no-plugins --ignore-platform-reqs && \
composer dump-autoload --no-plugins --optimize --no-dev --classmap-authoritative && \
echo "<?php" > vendor/composer/platform_check.php && \
echo "<?php return [];" > .env.local.php && \
find -name "[Tt]est*" -type d -exec rm -rf {} + && \
find -type f -name '*.md' -delete;

Expand Down
8 changes: 2 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,11 @@
"symfony/polyfill-php72": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
"php bin/console cache:clear"
],
"post-update-cmd": [
"@auto-scripts"
"php bin/console cache:clear"
]
},
"conflict": {
Expand Down
11 changes: 11 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
token: '%env(API_TOKEN)%'
consult_token: '%env(CONSULT_TOKEN)%'

services:
# default configuration for services in *this* file
Expand Down Expand Up @@ -49,6 +50,16 @@ services:
Peru\Jne\DniParser:
Peru\Jne\Async\Dni: ~

App\Service\ApiPeruDniService:
arguments:
$client: '@Peru\Http\Async\ClientInterface'
$token: '%consult_token%'

App\Service\ApiPeruRucService:
arguments:
$client: '@Peru\Http\Async\ClientInterface'
$token: '%consult_token%'

GraphQL\Type\Schema:
factory: ['@App\Factory\GraphqlSchemaFactory', 'create']
GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter: ~
Expand Down
13 changes: 7 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
version: '3'

services:
symfony:
app:
build: .
working_dir: /var/dev
command: vendor/bin/server watch 0.0.0.0:8080 --adapter=App\\AppKernelAdapter
ports:
- 8080:8080
volumes:
- .:/var/dev
- "8080:8080"
environment:
- APP_ENV=prod
- API_TOKEN=${API_TOKEN:-abcxyz}
- CONSULT_TOKEN=${CONSULT_TOKEN}
restart: unless-stopped
14 changes: 3 additions & 11 deletions src/Controller/DniController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@

namespace App\Controller;

use Peru\Jne\Async\Dni;
use App\Service\ApiPeruDniService;
use Peru\Reniec\Person;
use React\Promise\PromiseInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class DniController
{
/**
* @var Dni
*/
private $service;
private ApiPeruDniService $service;

/**
* DniController constructor.
*
* @param Dni $service
*/
public function __construct(Dni $service)
public function __construct(ApiPeruDniService $service)
{
$this->service = $service;
}
Expand Down
14 changes: 3 additions & 11 deletions src/Controller/RucController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,17 @@

namespace App\Controller;

use Peru\Sunat\Async\Ruc;
use App\Service\ApiPeruRucService;
use Peru\Sunat\Company;
use React\Promise\PromiseInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class RucController
{
/**
* @var Ruc
*/
private $service;
private ApiPeruRucService $service;

/**
* RucController constructor.
*
* @param Ruc $service
*/
public function __construct(Ruc $service)
public function __construct(ApiPeruRucService $service)
{
$this->service = $service;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Resolver/DniResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@

namespace App\Resolver;

use Peru\Jne\Async\Dni;
use App\Service\ApiPeruDniService;
use React\Promise\PromiseInterface;

class DniResolver
{
/**
* @var Dni
*/
private $service;
private ApiPeruDniService $service;

public function __construct(Dni $service)
public function __construct(ApiPeruDniService $service)
{
$this->service = $service;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Resolver/RucResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@

namespace App\Resolver;

use Peru\Sunat\Async\Ruc;
use App\Service\ApiPeruRucService;
use React\Promise\PromiseInterface;

class RucResolver
{
/**
* @var Ruc
*/
private $service;
private ApiPeruRucService $service;

public function __construct(Ruc $service)
public function __construct(ApiPeruRucService $service)
{
$this->service = $service;
}
Expand Down
51 changes: 51 additions & 0 deletions src/Service/ApiPeruDniService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace App\Service;

use Peru\Http\Async\ClientInterface;
use Peru\Reniec\Person;
use React\Promise\PromiseInterface;

class ApiPeruDniService
{
private const BASE_URL = 'https://apiperu.dev/api/dni/';

private ClientInterface $client;
private string $token;

public function __construct(ClientInterface $client, string $token)
{
$this->client = $client;
$this->token = $token;
}

public function get(string $dni): PromiseInterface
{
return $this->client
->getAsync(self::BASE_URL . $dni, [
'Authorization' => 'Bearer ' . $this->token,
'Accept' => 'application/json',
])
->then(function ($json) use ($dni) {
$result = json_decode($json, true);

if (!$result || !($result['success'] ?? false) || empty($result['data'])) {
return null;
}

$data = $result['data'];
$person = new Person();
$person->dni = $data['numero'] ?? $dni;
$person->nombres = $data['nombres'] ?? '';
$person->apellidoPaterno = $data['apellido_paterno'] ?? '';
$person->apellidoMaterno = $data['apellido_materno'] ?? '';
$person->codVerifica = (string) ($data['cod_verificacion'] ?? '');

return $person;
}, function (\Throwable $e) {
return null;
});
}
}
63 changes: 63 additions & 0 deletions src/Service/ApiPeruRucService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace App\Service;

use Peru\Http\Async\ClientInterface;
use Peru\Sunat\Company;
use React\Promise\PromiseInterface;

class ApiPeruRucService
{
private const BASE_URL = 'https://apiperu.dev/api/ruc/';

private ClientInterface $client;
private string $token;

public function __construct(ClientInterface $client, string $token)
{
$this->client = $client;
$this->token = $token;
}

public function get(string $ruc): PromiseInterface
{
return $this->client
->getAsync(self::BASE_URL . $ruc, [
'Authorization' => 'Bearer ' . $this->token,
'Accept' => 'application/json',
])
->then(function ($json) use ($ruc) {
$result = json_decode($json, true);

if (!$result || !($result['success'] ?? false) || empty($result['data'])) {
return null;
}

$data = $result['data'];
$company = new Company();
$company->ruc = $data['ruc'] ?? $ruc;
$company->razonSocial = $data['nombre_o_razon_social'] ?? ($data['razon_social'] ?? '');
$company->nombreComercial = $data['nombre_comercial'] ?? '';
$company->estado = $data['estado'] ?? '';
$company->condicion = $data['condicion'] ?? '';
$company->direccion = $data['direccion'] ?? '';
$company->departamento = $data['departamento'] ?? '';
$company->provincia = $data['provincia'] ?? '';
$company->distrito = $data['distrito'] ?? '';
$company->fechaInscripcion = $data['fecha_inscripcion'] ?? '';
$company->tipo = $data['tipo'] ?? '';
$company->telefonos = $data['telefonos'] ?? [];
$company->actEconomicas = $data['actividades_economicas'] ?? [];
$company->cpPago = $data['comprobantes_pago'] ?? [];
$company->sistElectronica = [];
$company->cpeElectronico = [];
$company->padrones = [];

return $company;
}, function (\Throwable $e) {
return null;
});
}
}
Loading