Limpieza de objetos externos

This commit is contained in:
2022-08-08 22:36:04 -04:00
parent a9968dec58
commit cdb4b382b7
57 changed files with 1210 additions and 339 deletions

View File

@ -1,151 +0,0 @@
<?php
namespace Common\Alias;
use Common\Concept\Database as DatabaseInterface;
abstract class Database implements DatabaseInterface
{
protected string $host;
protected int $port;
public function setHost(string $host, ?int $port = null): DatabaseInterface
{
$this->host = $host;
if ($port !== null) {
$this->port = $port;
}
return $this;
}
public function getHost(): string
{
return $this->host;
}
public function getPort(): int
{
return $this->port;
}
protected string $name;
public function setName(string $database_name): Database
{
$this->name = $database_name;
return $this;
}
public function getName(): string
{
return $this->name;
}
protected string $username;
protected string $password;
public function setUser(string $username, string $password): Database
{
$this->username = $username;
$this->password = $password;
return $this;
}
public function getUser(): string
{
return $this->username;
}
public function getPassword(): string
{
return $this->password;
}
protected \PDO $connection;
public function connect(): Database
{
if ($this->needsUser()) {
$this->connection = new \PDO($this->getDsn(), $this->getUser(), $this->getPassword());
return $this;
}
$this->connection = new \PDO($this->getDsn());
return $this;
}
public function getConnection(): \PDO
{
if (!isset($this->connection)) {
return $this->connect()->connection;
}
return $this->connection;
}
public function query(string $query): array
{
$st = $this->getConnection()->query($query);
if (!$st) {
throw new \PDOException("Could not retrieve anything with '{$query}'.");
}
$results = $st->fetchAll(\PDO::FETCH_ASSOC);
if (!$results) {
throw new \PDOException('Could not retrieve any results.');
}
return $results;
}
public function beginTransaction(): void
{
if (!$this->getConnection()->beginTransaction()) {
throw new \PDOException('Could not begin transaction.');
}
}
public function commit(): void
{
if (!$this->getConnection()->commit()) {
throw new \PDOException('Could not commit');
}
}
public function rollBack(): void
{
if (!$this->getConnection()->rollBack()) {
throw new \PDOException('Could not rollback.');
}
}
protected \PDOStatement $prepared_statement;
public function prepare(string $query): Database
{
$st = $this->getConnection()->prepare($query);
if (!$st) {
throw new \PDOException("Could not prepare query '{$query}'.");
}
$this->prepared_statement = $st;
return $this;
}
public function execute(array $data): array
{
if (!isset($this->prepared_statement)) {
throw new \Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($data)) {
throw new \PDOException('Could not execute prepared statement.');
}
$results = $this->prepared_statement->fetchAll(\PDO::FETCH_ASSOC);
if (!$results) {
throw new \PDOException('Could not retrieve any results.');
}
return $results;
}
public function insert(array $values): void
{
if (!isset($this->prepared_statement)) {
throw new \Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($values)) {
throw new \PDOException('Could not insert.');
}
}
public function update(array $data): void
{
if (!isset($this->prepared_statement)) {
throw new \Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($data)) {
throw new \PDOException('Could not update.');
}
}
public function delete(array $data): void
{
if (!isset($this->prepared_statement)) {
throw new \Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($data)) {
throw new \PDOException('Could not delete.');
}
}
}

View File

@ -1,26 +0,0 @@
<?php
namespace Common\Alias\Factory;
use Common\Concept\Factory\Model as FactoryInterface;
use Common\Concept\Model as ModelInterface;
use Common\Concept\Repository;
use Psr\Container\ContainerInterface;
abstract class Model implements FactoryInterface
{
protected ContainerInterface $container;
public function setContainer(ContainerInterface $container): Model
{
$this->container = $container;
return $this;
}
public function getContainer(): ContainerInterface
{
return $this->container;
}
public function find(ModelInterface $model_name): Repository
{
$class = str_replace('Model', 'Repository', get_class($model_name));
return $this->getContainer()->get($class);
}
}

View File

@ -1,46 +0,0 @@
<?php
namespace Common\Alias;
use function Safe\{fopen,fclose,fwrite};
use Common\Concept\File as FileInterface;
abstract class File implements FileInterface
{
protected string $filename;
public function setFilename(string $filename): FileInterface
{
$this->filename = $filename;
return $this;
}
public function getFilename(): string
{
return $this->filename;
}
public function isDir(): bool
{
return is_dir($this->getFilename());
}
public function isReadable(): bool
{
return is_readable($this->getFilename());
}
public function isWriteable(): bool
{
return is_writeable($this->getFilename());
}
public function read(?int $length = null): string
{
$fh = fopen($this->getFilename(), 'r');
$str = fgets($fh, $length);
fclose($fh);
return $str;
}
public function write(string $data, ?int $length = null): void
{
$fh = fopen($this->getFilename(), 'r');
fwrite($fh, $data, $length);
fclose($fh);
}
}

View File

@ -1,45 +0,0 @@
<?php
namespace Common\Alias;
use function Safe\{touch,mkdir,unlink};
use Common\Concept\Filesystem as FilesystemInterface;
abstract class Filesystem implements FilesystemInterface
{
protected string $base_path;
public function setBasePath(string $path): FilesystemInterface
{
$this->base_path = $path;
return $this;
}
public function getBasePath(): string
{
return $this->base_path;
}
public function buildPath(string $relative_path): string
{
return implode(DIRECTORY_SEPARATOR, [
$this->getBasePath(),
$relative_path
]);
}
public function has(string $relative_path): bool
{
return file_exists($this->buildPath($relative_path));
}
public function mkdir(string $relative_path): void
{
mkdir($this->buildPath($relative_path));
}
public function create(string $relative_path): void
{
touch($this->buildPath($relative_path));
}
public function delete(string $relative_path): void
{
unlink($this->buildPath($relative_path));
}
}

View File

@ -1,8 +0,0 @@
<?php
namespace Common\Alias;
use Common\Concept\Model as ModelInterface;
abstract class Model implements ModelInterface
{
}

View File

@ -1,62 +0,0 @@
<?php
namespace Common\Alias;
use Common\Concept\Database;
use Common\Concept\Model;
use Common\Concept\Repository as RepositoryInterface;
abstract class Repository implements RepositoryInterface
{
protected string $table;
public function setTable(string $table): RepositoryInterface
{
$this->table = $table;
return $this;
}
public function getTable(): string
{
return $this->table;
}
protected array $properties;
public function addProperty(string $name): RepositoryInterface
{
$this->properties []= $name;
return $this;
}
public function setProperties(array $properties): RepositoryInterface
{
foreach ($properties as $property) {
$this->addProperty($property);
}
return $this;
}
public function getProperties(): array
{
return $this->properties;
}
protected Database $database;
public function setDatabase(Database $database): RepositoryInterface
{
$this->database = $database;
return $this;
}
public function getDatabase(): Database
{
return $this->database;
}
public function fetchAll(): array
{
$query = "SELECT * FROM {$this->getTable()}";
return array_map([$this, 'load'], $this->getDatabase()->query($query));
}
public function save(Model $model): void
{
$columns = implode(', ', array_map(function($item) {return "'{$item}'";}, $this->getProperties()));
$values = implode(', ', array_map(function($item) {return '?';}, $this->getProperties()));
$query = "INSERT INTO {$this->getTable()} ({$columns}) VALUES ({$values})";
$values = array_map(function($item) use ($model) {$method = str_replace(' ', '', ucwords(str_replace('_', '', $item)));return $model->{"get{$method}"}();}, $this->getProperties());
$this->getDatabase()->prepare($query)->insert($values);
}
}

View File

@ -1,27 +0,0 @@
<?php
namespace Common\Concept;
interface Database
{
public function setHost(string $host, ?int $port = null): Database;
public function getHost(): string;
public function getPort(): int;
public function setName(string $database_name): Database;
public function getName(): string;
public function setUser(string $username, string $password): Database;
public function getUser(): string;
public function getPassword(): string;
public function getDsn(): string;
public function needsUser(): bool;
public function connect(): Database;
public function getConnection(): \PDO;
public function beginTransaction(): void;
public function commit(): void;
public function rollBack(): void;
public function query(string $query): array;
public function prepare(string $query): Database;
public function execute(array $data): array;
public function insert(array $values): void;
public function update(array $data): void;
public function delete(array $data): void;
}

View File

@ -1,10 +0,0 @@
<?php
namespace Common\Concept\Factory;
use Common\Concept\Model as ModelInterface;
use Common\Concept\Repository;
interface Model
{
public function find(ModelInterface $model_name): Repository;
}

View File

@ -1,16 +0,0 @@
<?php
namespace Common\Concept;
interface File
{
public function setFilename(string $filename): File;
public function getFilename(): string;
public function isDir(): bool;
public function isReadable(): bool;
public function isWriteable(): bool;
public function read(?int $length = null): string;
public function write(string $data, ?int $length = null);
public function append(string $data, ?int $length = null);
}

View File

@ -1,17 +0,0 @@
<?php
namespace Common\Concept;
interface Filesystem
{
public function setBasePath(string $path): Filesystem;
public function getBasePath(): string;
public function buildPath(string $relative_path): string;
public function has(string $relative_path): bool;
public function mkdir(string $relative_path): void;
public function create(string $relative_path): void;
public function get(string $relative_path): File;
public function delete(string $relative_path): void;
}

View File

@ -1,6 +0,0 @@
<?php
namespace Common\Concept;
interface Model
{
}

View File

@ -1,11 +0,0 @@
<?php
namespace Common\Concept;
interface Repository
{
public function fetchAll(): array;
public function fetchByKey($key): Model;
public function load(array $data_row): Model;
public function save(Model $model): void;
public function delete(Model $model): void;
}

View File

@ -0,0 +1,24 @@
<?php
namespace Common\Controller;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use function Safe\{json_decode, file_get_contents};
use ProVM\Alias\Controller\Json;
class Base
{
use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, ContainerInterface $container): ResponseInterface
{
$folder = $container->get('folders')->documentation;
$filename = implode(DIRECTORY_SEPARATOR, [
$folder,
'base.json'
]);
$documentation = json_decode(file_get_contents($filename));
return $this->withJson($response, $documentation);
}
}

View File

@ -0,0 +1,125 @@
<?php
namespace Common\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use PDOException;
use function Safe\{json_decode,error_log};
use ProVM\Alias\Controller\Json;
use Contabilidad\Repository\Cuenta;
class Cuentas
{
use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
{
$cuentas = [];
try {
$cuentas = $repository->fetchAll();
} catch (PDOException $e) {
error_log($e);
}
return $this->withJson($response, compact('cuentas'));
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository, $cuenta_id): ResponseInterface
{
$cuenta = null;
try {
$cuenta = $repository->fetchById($cuenta_id);
} catch (PDOException $e) {
error_log($e);
}
return $this->withJson($response, compact('cuenta'));
}
public function add(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
{
$body = $request->getBody();
$contents = $body->getContents();
$json = json_decode($contents);
if (!is_array($json)) {
$json = [$json];
}
$output = [
'input' => $json,
'cuentas' => []
];
foreach ($json as $data) {
$cuenta = $repository->create((array) $data);
$status = true;
$exists = true;
if ($cuenta->isNew()) {
$exists = false;
try {
$repository->save($cuenta);
} catch (PDOException $e) {
error_log($e);
$status = false;
}
}
$output['cuentas'] []= [
'cuenta' => $cuenta,
'exists' => $exists,
'added' => $status
];
}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
{
$body = $request->getBody();
$contents = $body->getContents();
$json = json_decode($contents);
if (!is_array($json)) {
$json = [$json];
}
$output = [
'input' => $json,
'cuentas' => []
];
foreach ($json as $data) {
$cuenta = $repository->fetchById($data->id);
$old = clone $cuenta;
try {
$cuenta->edit((array) $data);
$status = $cuenta->isDirty();
if ($status) {
$repository->save($cuenta);
}
} catch (PDOException $e) {
error_log($e);
$status = false;
}
$output['cuentas'] []= [
'antes' => $old,
'cuenta' => $cuenta,
'edited' => $status
];
}
return $this->withJson($response, $output);
}
public function editOne(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository, $cuenta_id): ResponseInterface
{
$cuenta = $repository->fetchById($cuenta_id);
$body = $request->getBody();
$contents = $body->getContents();
$json = json_decode($contents, JSON_OBJECT_AS_ARRAY);
$output = [
'input' => $json,
'old' => clone $cuenta
];
try {
$cuenta->edit((array) $json);
$status = $cuenta->isDirty();
if ($status) {
$repository->save($cuenta);
}
} catch (PDOException $e) {
error_log($e);
$status = false;
}
$output['cuenta'] = $cuenta;
$output['edited'] = $status;
return $this->withJson($response, $output);
}
}

View File

@ -1,11 +1,11 @@
<?php
namespace Contabilidad\Common\Middleware;
namespace Common\Middleware;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ResponseFactoryInterface as Factory;
use Contabilidad\Common\Service\Auth as Service;
use Common\Service\Auth as Service;
class Auth {
protected Factory $factory;

View File

@ -1,5 +1,5 @@
<?php
namespace Contabilidad\Common\Service;
namespace Common\Service;
use Psr\Http\Message\ServerRequestInterface as Request;