Test Setup Failed
Push — v2 ( 74186a...c94b75 )
by Alexander
06:56
created

PaginatorFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder\Pagination;
4
5
use Flugg\Responder\Contracts\Pagination\PaginatorFactory as PaginatorFactoryContract;
6
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
7
use League\Fractal\Pagination\Cursor;
8
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
9
use League\Fractal\Pagination\PaginatorInterface;
10
11
/**
12
 * A factory class for making Fractal paginator adapters from a Laravel paginator.
13
 *
14
 * @package flugger/laravel-responder
15
 * @author  Alexander Tømmerås <[email protected]>
16
 * @license The MIT License
17
 */
18
class PaginatorFactory implements PaginatorFactoryContract
19
{
20
    /**
21
     * A list of query string values appended to the paginator links.
22
     *
23
     * @var array
24
     */
25
    protected $parameters;
26
27
    /**
28
     * Construct the factory class.
29
     *
30
     * @param array $parameters
31
     */
32
    public function __construct(array $parameters)
33
    {
34
        $this->parameters = $parameters;
35
    }
36
37
    /**
38
     * Make a Fractal paginator adapter from a Laravel paginator.
39
     *
40
     * @param  \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
41
     * @return \League\Fractal\Pagination\PaginatorInterface
42
     */
43
    public function make(LengthAwarePaginator $paginator): PaginatorInterface
44
    {
45
        $paginator->appends($this->parameters);
46
47
        return new IlluminatePaginatorAdapter($paginator);
48
    }
49
50
    /**
51
     * Make a Fractal paginator adapter from a Laravel paginator.
52
     *
53
     * @param  \Flugg\Responder\Pagination\CursorPaginator $paginator
54
     * @return \League\Fractal\Pagination\Cursor
55
     */
56
    public function makeCursor(CursorPaginator $paginator): Cursor
57
    {
58
        $paginator->appends($this->parameters);
0 ignored issues
show
Bug introduced by
The method appends() does not seem to exist on object<Flugg\Responder\P...nation\CursorPaginator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
60
        return new Cursor($paginator->cursor(), $paginator->previous(), $paginator->next(), $paginator->get()->count());
0 ignored issues
show
Bug introduced by
It seems like $paginator->previous() targeting Flugg\Responder\Paginati...orPaginator::previous() can also be of type integer or string; however, League\Fractal\Pagination\Cursor::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
    }
62
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
63