Refactor Messages::getCorrespondence

This commit is contained in:
Alma Armas 2020-08-01 14:23:43 +00:00
parent f235338d69
commit 9dc25aa938

View file

@ -17,12 +17,21 @@ class Messages
$this->messages = $this->context->table("messages"); $this->messages = $this->context->table("messages");
} }
function getCorrespondencies(RowModel $correspondent, int $page = 1, ?int $perPage = NULL): \Traversable function get(int $id): ?Message
{
$msg = $this->messages->get($id);
if(!$msg)
return NULL;
return new Message($msg);
}
function getCorrespondencies(RowModel $correspondent, int $page = 1, ?int $perPage = NULL, ?int $offset = NULL): \Traversable
{ {
$id = $correspondent->getId(); $id = $correspondent->getId();
$class = get_class($correspondent); $class = get_class($correspondent);
$limit = $perPage ?? OPENVK_DEFAULT_PER_PAGE; $limit = $perPage ?? OPENVK_DEFAULT_PER_PAGE;
$offset = ($page - 1) * $limit; $offset = $offset ?? ($page - 1) * $limit;
$query = file_get_contents(__DIR__ . "/../sql/get-correspondencies.tsql"); $query = file_get_contents(__DIR__ . "/../sql/get-correspondencies.tsql");
DatabaseConnection::i()->getConnection()->query(file_get_contents(__DIR__ . "/../sql/mysql-msg-fix.tsql")); DatabaseConnection::i()->getConnection()->query(file_get_contents(__DIR__ . "/../sql/mysql-msg-fix.tsql"));
$coresps = DatabaseConnection::i()->getConnection()->query($query, $id, $class, $id, $class, $limit, $offset); $coresps = DatabaseConnection::i()->getConnection()->query($query, $id, $class, $id, $class, $limit, $offset);
@ -35,4 +44,4 @@ class Messages
yield new Correspondence($correspondent, $anotherCorrespondent); yield new Correspondence($correspondent, $anotherCorrespondent);
} }
} }
} }