ProviderAbstract   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 0
loc 36
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getPageSize() 0 22 7
1
<?php
2
3
namespace yiicod\pagesizepager\providers;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
8
/**
9
 * Class ProviderAbstract
10
 *
11
 * @author Orlov Aleksey <[email protected]>
12
 * @package yiicod\pagesizepager\providers
13
 */
14
abstract class ProviderAbstract implements ProviderInterface
15
{
16
    const SESSION_PAGER_KEY = 'session_pager';
17
18
    /**
19
     * Get page size
20
     *
21
     * @param string $gridIdentifier
22
     * @param string $sizeVar
23
     * @param int $default
24
     *
25
     * @return int
26
     */
27
    public function getPageSize(string $gridIdentifier, string $sizeVar = 'per-page', int $default = 10): int
28
    {
29
        $data = $this->getStorageState($gridIdentifier);
30
        if (false === (Yii::$app instanceof \yii\console\Application)) {
31
            if ('' === $sizeVar) {
32
                $sizeVar = Yii::$app->session->get(self::SESSION_PAGER_KEY . $gridIdentifier);
33
            }
34
35
            if (null !== Yii::$app->request->getQueryParam($sizeVar, null) && (
36
                    false === isset($data[$sizeVar]) ||
37
                    $data[$sizeVar] != Yii::$app->request->getQueryParam($sizeVar)
38
                )
39
            ) {
40
                $data = ArrayHelper::merge($data, [
41
                    $sizeVar => Yii::$app->request->getQueryParam($sizeVar),
42
                ]);
43
                $this->setStorageState($gridIdentifier, $data);
44
            }
45
        }
46
47
        return isset($data[$sizeVar]) ? (int)$data[$sizeVar] : $default;
48
    }
49
}
50