PageDatabagTrait::setCurrentPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Jaxon\App;
4
5
use Jaxon\Plugin\Response\Pagination\Paginator;
6
7
trait PageDatabagTrait
8
{
9
    /**
10
     * The current page number.
11
     *
12
     * @var int
13
     */
14
    private int $currentPage = 1;
15
16
    /**
17
     * Get the pagination databag name.
18
     *
19
     * @return string
20
     */
21
    abstract protected function bagName(): string;
22
23
    /**
24
     * Get the pagination databag attribute.
25
     *
26
     * @return string
27
     */
28
    abstract protected function bagAttr(): string;
29
30
    /**
31
     * Get the page number.
32
     *
33
     * @param int $pageNumber
34
     *
35
     * @return int
36
     */
37
    private function getPageNumber(int $pageNumber): int
38
    {
39
        // If no page number is provided, then get the value from the databag.
40
        return $pageNumber > 0 ? $pageNumber :
41
            (int)$this->bag($this->bagName())->get($this->bagAttr(), 1);
0 ignored issues
show
Bug introduced by
It seems like bag() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            (int)$this->/** @scrutinizer ignore-call */ bag($this->bagName())->get($this->bagAttr(), 1);
Loading history...
42
    }
43
44
    /**
45
     * Set the page number.
46
     *
47
     * @param int $currentPage
48
     *
49
     * @return void
50
     */
51
    private function setCurrentPage(int $currentPage): void
52
    {
53
        // Save the current page in the databag.
54
        $this->bag($this->bagName())->set($this->bagAttr(), $currentPage);
55
        $this->currentPage = $currentPage;
56
    }
57
58
    /**
59
     * Get the paginator for the component.
60
     *
61
     * @param int $pageNumber
62
     *
63
     * @return Paginator
64
     */
65
    protected function paginator(int $pageNumber): Paginator
66
    {
67
        return $this->cl(Pagination::class)
0 ignored issues
show
Bug introduced by
It seems like cl() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        return $this->/** @scrutinizer ignore-call */ cl(Pagination::class)
Loading history...
68
            // Use the js class name as component item identifier.
69
            ->item($this->rq()->_class())
0 ignored issues
show
Bug introduced by
It seems like rq() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            ->item($this->/** @scrutinizer ignore-call */ rq()->_class())
Loading history...
70
            ->paginator($this->getPageNumber($pageNumber), $this->limit(), $this->count())
0 ignored issues
show
Bug introduced by
It seems like limit() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            ->paginator($this->getPageNumber($pageNumber), $this->/** @scrutinizer ignore-call */ limit(), $this->count())
Loading history...
Bug introduced by
It seems like count() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            ->paginator($this->getPageNumber($pageNumber), $this->limit(), $this->/** @scrutinizer ignore-call */ count())
Loading history...
71
            // This callback will receive the final value of the current page number.
72
            ->page(function(int $currentPage) {
73
                $this->setCurrentPage($currentPage);
74
            });
75
    }
76
77
    /**
78
     * Get the current page number
79
     *
80
     * @return int
81
     */
82
    protected function currentPage(): int
83
    {
84
        return $this->currentPage;
85
    }
86
}
87