98 lines
No EOL
3.5 KiB
PHP
98 lines
No EOL
3.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace GamesShop\Routing\Api\DataTables;
|
|
|
|
use Doctrine\Common\Collections\Criteria;
|
|
use Doctrine\Common\Collections\Order;
|
|
use Doctrine\ORM\EntityManager;
|
|
use GamesShop\Entities\Account\User;
|
|
use GamesShop\Entities\Games\Game;
|
|
use GamesShop\Entities\Games\Key;
|
|
use GamesShop\Login\LoginHandler;
|
|
use GamesShop\Login\UserPermission;
|
|
use Laminas\Diactoros\Response\JsonResponse;
|
|
use League\Route\Http\Exception\ForbiddenException;
|
|
use League\Route\Http\Exception\UnauthorizedException;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
final class ProviderKeysEndpoint
|
|
{
|
|
public function __construct(
|
|
private readonly LoginHandler $loginHandler,
|
|
private readonly EntityManager $entityManager,
|
|
)
|
|
{
|
|
}
|
|
|
|
public function __invoke(ServerRequestInterface $request): ResponseInterface
|
|
{
|
|
if (!$this->loginHandler->isLoggedIn()) {
|
|
throw new UnauthorizedException();
|
|
}
|
|
|
|
$user = $this->loginHandler->getCurrentUser();
|
|
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
|
|
throw new ForbiddenException();
|
|
}
|
|
|
|
|
|
$params = $request->getQueryParams();
|
|
$draw = $params['draw'];
|
|
$start = $params['start'];
|
|
$length = $params['length'];
|
|
|
|
$searchValue = $params['search']['value'];
|
|
|
|
$repo = $this->entityManager->getRepository(Game::class);
|
|
$total = $repo->count();
|
|
|
|
$criteria = Criteria::create();
|
|
$criteria->where(Criteria::expr()->contains('name', $searchValue));
|
|
$criteria->setFirstResult((int)$start);
|
|
$criteria->setMaxResults((int)$length);
|
|
$criteria->orderBy([ 'name' => Order::Ascending ]);
|
|
|
|
$values = $repo->matching($criteria);
|
|
$filteredCount = $values->count();
|
|
|
|
$entityManager = $this->entityManager;
|
|
|
|
return new JsonResponse([
|
|
'draw' => $draw,
|
|
'recordsTotal' => $total,
|
|
'recordsFiltered' => $filteredCount,
|
|
'data' =>
|
|
array_filter($values->map(function (Game $game) use ($entityManager, $user) {
|
|
$criteria = Criteria::create();
|
|
$criteria->where(Criteria::expr()->eq('game', $game));
|
|
$criteria->andWhere(Criteria::expr()->eq('contributedUser', $user));
|
|
$criteria->orderBy(['state' => Order::Ascending]);
|
|
|
|
$keys = $entityManager->getRepository(Key::class)->matching($criteria);
|
|
|
|
$keysAmount = $keys->count();
|
|
if ($keysAmount < 1) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'gamePicture' => '',
|
|
'name' => $game->getName(),
|
|
'keysAmount' => $keysAmount,
|
|
'igdbState' => 'yet to be implermented',
|
|
'keys' => $keys->map(function (Key $key) use ($entityManager) {
|
|
return [
|
|
'keyState' => $key->getState()->value,
|
|
'key' => $key->getKey(),
|
|
'store' => $key->getStore()->value,
|
|
'store_link' => $key->getStoreLink(),
|
|
'from' => $key->getFromWhere(),
|
|
];
|
|
})->toArray()
|
|
];
|
|
})->toArray())
|
|
]);
|
|
}
|
|
} |