Passed
Pull Request — master (#456)
by Alejandro
06:37 queued 01:57
created

ShortUrlRepositoryAdapter::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Paginator\Adapter;
5
6
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
7
use Zend\Paginator\Adapter\AdapterInterface;
8
9
use function strip_tags;
10
use function trim;
11
12
class ShortUrlRepositoryAdapter implements AdapterInterface
13
{
14
    public const ITEMS_PER_PAGE = 10;
15
16
    /** @var ShortUrlRepositoryInterface */
17
    private $repository;
18
    /** @var null|string */
19
    private $searchTerm;
20
    /** @var null|array|string */
21
    private $orderBy;
22
    /** @var array */
23
    private $tags;
24
25 3
    public function __construct(
26
        ShortUrlRepositoryInterface $repository,
27
        $searchTerm = null,
28
        array $tags = [],
29
        $orderBy = null
30
    ) {
31 3
        $this->repository = $repository;
32 3
        $this->searchTerm = $searchTerm !== null ? trim(strip_tags($searchTerm)) : null;
33 3
        $this->orderBy = $orderBy;
34 3
        $this->tags = $tags;
35
    }
36
37
    /**
38
     * Returns a collection of items for a page.
39
     *
40
     * @param  int $offset Page offset
41
     * @param  int $itemCountPerPage Number of items per page
42
     * @return array
43
     */
44 2
    public function getItems($offset, $itemCountPerPage): array
45
    {
46 2
        return $this->repository->findList(
47 2
            $itemCountPerPage,
48
            $offset,
49 2
            $this->searchTerm,
50 2
            $this->tags,
51 2
            $this->orderBy
52
        );
53
    }
54
55
    /**
56
     * Count elements of an object
57
     * @link http://php.net/manual/en/countable.count.php
58
     * @return int The custom count as an integer.
59
     * </p>
60
     * <p>
61
     * The return value is cast to an integer.
62
     * @since 5.1.0
63
     */
64 2
    public function count(): int
65
    {
66 2
        return $this->repository->countList($this->searchTerm, $this->tags);
67
    }
68
}
69