CallbackAdapter::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the Pagerfanta package.
5
 *
6
 * (c) Pablo Díez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Pagerfanta\Adapter;
13
14
use Pagerfanta\Exception\InvalidArgumentException;
15
16
/**
17
 * @author Adrien Brault <[email protected]>
18
 */
19
class CallbackAdapter implements AdapterInterface
20
{
21
    private $getNbResultsCallback;
22
    private $getSliceCallback;
23
24
    /**
25
     * @param callable $getNbResultsCallback
26
     * @param callable $getSliceCallback
27
     */
28 8
    public function __construct($getNbResultsCallback, $getSliceCallback)
29
    {
30 8
        if (!is_callable($getNbResultsCallback)) {
31 2
            throw new InvalidArgumentException('$getNbResultsCallback should be a callable');
32
        }
33 6
        if (!is_callable($getSliceCallback)) {
34 2
            throw new InvalidArgumentException('$getSliceCallback should be a callable');
35
        }
36
37 4
        $this->getNbResultsCallback = $getNbResultsCallback;
38 4
        $this->getSliceCallback = $getSliceCallback;
39 4
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    public function getNbResults()
45
    {
46 2
        return call_user_func($this->getNbResultsCallback);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 3
    public function getSlice($offset, $length)
53
    {
54 3
        return call_user_func($this->getSliceCallback, $offset, $length);
55
    }
56
}
57