Completed
Pull Request — master (#38)
by
unknown
03:00
created

DataProviderReader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace pastuhov\ymlcatalog\readers;
4
5
use pastuhov\ymlcatalog\ReaderInterface;
6
use yii\base\Exception;
7
use yii\data\ActiveDataProvider;
8
9
/**
10
 * Класс выборки данных с помощью DataProvider.
11
 *
12
 * @package pastuhov\ymlcatalog\readers
13
 */
14
class DataProviderReader implements ReaderInterface
15
{
16
    /**
17
     * @var ActiveDataProvider|null
18
     */
19
    protected $dataProvider = null;
20
21
    /**
22
     * @var \yii\data\Pagination|null
23
     */
24
    protected $pagination = null;
25
26
    /**
27
     * @inheritdoc
28
     *
29
     * @param ActiveDataProvider   $dataProvider
30
     */
31
    public function __construct(ActiveDataProvider $dataProvider = null)
32
    {
33
        if (is_null($dataProvider)) {
34
            throw new Exception('DataProvider can`t be empty.');
35
        }
36
        $this->dataProvider = $dataProvider;
37
        $this->pagination = $this->dataProvider->getPagination();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->dataProvider->getPagination() can also be of type boolean. However, the property $pagination is declared as type object<yii\data\Pagination>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function rewind()
44
    {
45
        if ($this->pagination->getPage() != 0) {
46
            $this->pagination->setPage(0);
47
            $this->dataProvider->prepare(true);
48
        }
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function next()
55
    {
56
        $this->pagination->setPage($this->pagination->getPage() + 1);
57
        $this->dataProvider->prepare(true);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function valid()
64
    {
65
        return !empty($this->dataProvider->getModels());
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function current()
72
    {
73
        return $this->dataProvider->getModels();
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function key()
80
    {
81
        return $this->pagination->getPage();
82
    }
83
}
84