mirror of
https://github.com/openvk/openvk
synced 2025-02-02 21:15:42 +03:00
Alexander Minkin
6ec54a379d
* feat(lint): add php-cs-fixer for linting Removing previous CODE_STYLE as it was not enforced anyway and using PER-CS 2.0. This is not the reformatting commit. * style: format code according to PER-CS 2.0 with php-cs-fixer * ci(actions): add lint action Resolves #1132.
63 lines
2.2 KiB
PHP
63 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Repositories;
|
|
|
|
use openvk\Web\Models\RowModel;
|
|
use openvk\Web\Models\Entities\User;
|
|
use openvk\Web\Models\Entities\Message;
|
|
use openvk\Web\Models\Entities\Correspondence;
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
class Messages
|
|
{
|
|
private $context;
|
|
private $messages;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
$this->messages = $this->context->table("messages");
|
|
}
|
|
|
|
public function get(int $id): ?Message
|
|
{
|
|
$msg = $this->messages->get($id);
|
|
if (!$msg) {
|
|
return null;
|
|
}
|
|
|
|
return new Message($msg);
|
|
}
|
|
|
|
public function getCorrespondencies(RowModel $correspondent, int $page = 1, ?int $perPage = null, ?int $offset = null): \Traversable
|
|
{
|
|
$id = $correspondent->getId();
|
|
$class = get_class($correspondent);
|
|
$limit = $perPage ?? OPENVK_DEFAULT_PER_PAGE;
|
|
$offset ??= ($page - 1) * $limit;
|
|
$query = file_get_contents(__DIR__ . "/../sql/get-correspondencies.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);
|
|
foreach ($coresps as $c) {
|
|
if ($c->class === 'openvk\Web\Models\Entities\User') {
|
|
$anotherCorrespondent = (new Users())->get($c->id);
|
|
} elseif ($c->class === 'openvk\Web\Models\Entities\Club') {
|
|
$anotherCorrespondent = (new Clubs())->get($c->id);
|
|
}
|
|
|
|
yield new Correspondence($correspondent, $anotherCorrespondent);
|
|
}
|
|
}
|
|
|
|
public function getCorrespondenciesCount(RowModel $correspondent): ?int
|
|
{
|
|
$id = $correspondent->getId();
|
|
$class = get_class($correspondent);
|
|
$query = file_get_contents(__DIR__ . "/../sql/get-correspondencies-count.tsql");
|
|
DatabaseConnection::i()->getConnection()->query(file_get_contents(__DIR__ . "/../sql/mysql-msg-fix.tsql"));
|
|
$count = DatabaseConnection::i()->getConnection()->query($query, $id, $class, $id, $class)->fetch()->cnt;
|
|
return $count;
|
|
}
|
|
}
|