Passed
Push — master ( 0f45c2...370b21 )
by Matthew
03:56
created

EUpdateAllAction::getUpdatedModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * Date: 2017-12-07
5
 * Time: 03:12
6
 */
7
8
namespace MP\ExtendedApi;
9
10
use Yii;
11
use yii\data\ActiveDataProvider;
12
use yii\db\ActiveQuery;
13
use yii\db\ActiveRecord;
14
use yii\rest\IndexAction;
15
use yii\web\BadRequestHttpException;
16
17
/**
18
 * Class    EUpdateAllAction
19
 * @package MP\ExtendedApi
20
 * @author  Yarmaliuk Mikhail
21
 * @version 1.0
22
 */
23
class EUpdateAllAction extends IndexAction
24
{
25
    /**
26
     * @var string
27
     */
28
    public $filterAttribute = 'filter';
29
30
    /**
31
     * @var string
32
     */
33
    public $updatedAttribute = 'updatedAttributes';
34
35
    /**
36
     * Add custom query condition
37
     *
38
     * @var null|\Closure
39
     */
40
    public $addQuery = null;
41
42
    /**
43
     * Column name
44
     *
45
     * @var null|string
46
     */
47
    public $filterUser = null;
48
49
    /**
50
     * @var array
51
     */
52
    private $_updatedModels = [];
53
54
    /**
55
     * Get deleted models
56
     *
57
     * @return array
58
     */
59
    public function getUpdatedModels(): array
60
    {
61
        return $this->_updatedModels;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    protected function prepareDataProvider()
68
    {
69
        $filter            = Yii::$app->request->get($this->filterAttribute);
70
        $queryParams       = Yii::$app->request->getQueryParams();
71
        $updatedAttributes = [];
72
73
        if (!empty($filter)) {
74
            $queryParams[$this->filterAttribute] = json_decode($filter, true);
0 ignored issues
show
Bug introduced by
It seems like $filter can also be of type array; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

74
            $queryParams[$this->filterAttribute] = json_decode(/** @scrutinizer ignore-type */ $filter, true);
Loading history...
75
        }
76
77
        if (!empty($queryParams[$this->updatedAttribute])) {
78
            $updatedAttributes = json_decode($queryParams[$this->updatedAttribute], true);
79
        }
80
81
        if (empty($updatedAttributes)) {
82
            throw new BadRequestHttpException("Param '{$this->updatedAttribute}' cannot be empty");
83
        }
84
85
        Yii::$app->request->setQueryParams($queryParams);
86
87
        $this->prepareDataProvider = function (EIndexAction $action, $filter) use ($extraFilter) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $extraFilter seems to be never defined.
Loading history...
88
            /** @var ActiveDataProvider $dataProvider */
89
            $dataProvider = call_user_func([$action->dataFilter->searchModel, 'getDataProvider']);
90
            $dataProvider->query->andWhere($filter);
91
92
            if ($this->addQuery) {
93
                call_user_func($this->addQuery, $dataProvider->query, $extraFilter, $action->dataFilter);
94
95
                if ($action->dataFilter->hasErrors()) {
0 ignored issues
show
Bug introduced by
The method hasErrors() does not exist on null. ( Ignorable by Annotation )

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

95
                if ($action->dataFilter->/** @scrutinizer ignore-call */ hasErrors()) {

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...
96
                    return $action->dataFilter;
97
                }
98
            }
99
100
            if ($this->filterUser) {
101
                $filterUserColumn = is_callable($this->filterUser) ? call_user_func($this->filterUser) : $this->filterUser;
102
103
                if ($filterUserColumn !== null) {
104
                    $dataProvider->query->andWhere([$filterUserColumn => Yii::$app->user->getId()]);
0 ignored issues
show
Bug introduced by
The method getId() does not exist on null. ( Ignorable by Annotation )

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

104
                    $dataProvider->query->andWhere([$filterUserColumn => Yii::$app->user->/** @scrutinizer ignore-call */ getId()]);

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...
105
                }
106
            }
107
108
            return $dataProvider;
109
        };
110
111
        $dataProvider = parent::prepareDataProvider();
112
        /** @var ActiveQuery $query */
113
        $query = $dataProvider->query;
114
        $query
115
            ->limit(-1)
116
            ->offset(-1)
117
            ->orderBy([]);
118
119
        $countUpdated = 0;
120
121
        foreach ($query->each() as $model) {
122
            /** @var $model ActiveRecord */
123
            $model->setAttributes($updatedAttributes);
124
125
            if ($model->save()) {
126
                $this->_updatedModels[] = $model;
127
                $countUpdated++;
128
            }
129
        }
130
131
        Yii::$app->response->headers->set('X-Total-Updated', $countUpdated);
132
133
        return;
134
    }
135
}
136