wallabag/src/Helper/PreparePagerForEntries.php

41 lines
1 KiB
PHP
Raw Normal View History

2016-04-14 13:03:22 +00:00
<?php
2024-02-19 00:30:12 +00:00
namespace Wallabag\Helper;
2016-04-14 13:03:22 +00:00
use Pagerfanta\Adapter\AdapterInterface;
2023-08-08 01:27:21 +00:00
use Pagerfanta\Adapter\NullAdapter;
2016-04-14 13:03:22 +00:00
use Pagerfanta\Pagerfanta;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2024-02-19 00:30:12 +00:00
use Wallabag\Entity\User;
2016-04-14 13:03:22 +00:00
class PreparePagerForEntries
{
private $tokenStorage;
2016-04-14 13:03:22 +00:00
2022-11-14 22:11:46 +00:00
public function __construct(TokenStorageInterface $tokenStorage)
2016-04-14 13:03:22 +00:00
{
$this->tokenStorage = $tokenStorage;
2016-04-14 13:03:22 +00:00
}
/**
* @param User $user If user isn't logged in, we can force it (like for feed)
2016-04-14 13:03:22 +00:00
*
2023-08-08 01:27:21 +00:00
* @return Pagerfanta
2016-04-14 13:03:22 +00:00
*/
2024-02-19 08:31:30 +00:00
public function prepare(AdapterInterface $adapter, ?User $user = null)
2016-04-14 13:03:22 +00:00
{
2017-06-10 11:11:08 +00:00
if (null === $user) {
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
}
2022-11-23 14:51:33 +00:00
if (!$user instanceof User) {
2023-08-08 01:27:21 +00:00
return new Pagerfanta(new NullAdapter());
}
2016-04-14 13:03:22 +00:00
$entries = new Pagerfanta($adapter);
$entries->setMaxPerPage($user->getConfig()->getItemsPerPage());
2016-04-14 13:03:22 +00:00
return $entries;
}
}