Completed
Push — master ( 96f318...d1bd20 )
by Timo
10:06
created

DataComponent::forget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models;
4
5
use hamburgscleanest\DataTables\Facades\SessionHelper;
6
use Illuminate\Database\Eloquent\Builder;
7
8
/**
9
 * Class DataComponent
10
 * @package hamburgscleanest\DataTables\Models
11
 */
12
abstract class DataComponent {
13
14
    /** @var Builder */
15
    protected $_queryBuilder;
16
17
    /** @var bool */
18
    protected $_rememberState = false;
19
20
    /** @var string */
21
    protected $_rememberKey = 'global';
22
23
    /** @var array */
24
    protected $_columns = [];
25
26
    /**
27
     * You cannot count the data when ordering.
28
     * Disables ordering temporary.
29
     *
30
     * @return int
31
     */
32 8
    public function getQueryCount() : int
33
    {
34
        /** @var \Illuminate\Database\Query\Builder $query */
35 8
        $query = $this->_queryBuilder->getQuery();
36
37 8
        [$oldOrders, $oldLimit, $oldOffset] = [$query->orders, $query->limit, $query->offset];
38 8
        $query->orders = $query->limit = $query->offset = null;
39
40 8
        $dataCount = $query->count();
41
42 8
        $query->orders = $oldOrders;
43 8
        $query->limit = $oldLimit;
44 8
        $query->offset = $oldOffset;
45
46 8
        return $dataCount;
47
    }
48
49
    /**
50
     * @param Builder $queryBuilder
51
     * @param array $columns
52
     */
53 27
    public function init(Builder $queryBuilder, array $columns) : void
54
    {
55 27
        $this->_queryBuilder = $queryBuilder;
56 27
        $this->_columns = $columns;
57
58 27
        if ($this->_rememberState)
59
        {
60 3
            $this->_readFromSession();
61
        }
62 27
        $this->_afterInit();
63 27
    }
64
65
    /**
66
     * Everything that needs to be read when the state is remembered.
67
     * Is called before _afterInit(), so that session values can be overriden.
68
     */
69 2
    protected function _readFromSession() : void
70
    {
71
72 2
    }
73
74
    /**
75
     * Initalize fields after the query builder instance is set.
76
     */
77 3
    protected function _afterInit() : void
78
    {
79
80 3
    }
81
82
    /**
83
     * Remember the state of the data component.
84
     *
85
     * @return DataComponent
86
     */
87 3
    public function remember() : DataComponent
88
    {
89 3
        $this->_rememberState = true;
90
91 3
        return $this;
92
    }
93
94
    /**
95
     * Forget the state of the data component.
96
     *
97
     * @return DataComponent
98
     */
99 1
    public function forget() : DataComponent
100
    {
101 1
        $this->_rememberState = false;
102 1
        SessionHelper::removeState($this->_rememberKey);
103
104 1
        return $this;
105
    }
106
107 14
    public function transformData() : void
108
    {
109 14
        if ($this->_rememberState)
110
        {
111 2
            $this->_storeInSession();
112
        }
113 14
        $this->_shapeData();
114 14
    }
115
116
    /**
117
     * Use this function to save your state in the session.
118
     * This is called just before rendering, so all dynamically added stuff etc. is considered.
119
     */
120 1
    protected function _storeInSession() : void
121
    {
122
123 1
    }
124
125
    /**
126
     * @return Builder
127
     */
128
    abstract protected function _shapeData() : Builder;
129
130
    /**
131
     * @return string
132
     */
133
    abstract public function render() : string;
134
}