GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 7cfc28...20914a )
by Alexander
33s
created

ManageController::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace app\modules\seo\controllers;
4
5
use app\backend\actions\UpdateEditable;
6
use app\backend\components\BackendController;
7
use app\modules\shop\models\Category;
8
use app\modules\shop\models\OrderTransaction;
9
use app\modules\shop\models\Product;
10
use app\modules\seo\models\Config;
11
use app\modules\seo\models\Counter;
12
use app\modules\seo\models\Meta;
13
use app\modules\seo\models\Redirect;
14
use app\modules\seo\models\Robots;
15
use devgroup\ace\AceHelper;
16
use devgroup\TagDependencyHelper\ActiveRecordHelper;
17
use yii\base\Event;
18
use yii\filters\AccessControl;
19
use yii\helpers\Json;
20
use yii\web\NotFoundHttpException;
21
use yii\helpers\Url;
22
use Yii;
23
24
class ManageController extends BackendController
25
{
26
    public function behaviors()
27
    {
28
        return [
29
            'access' => [
30
                'class' => AccessControl::className(),
31
                'rules' => [
32
                    [
33
                        'allow' => true,
34
                        'actions' => ['get-robots'],
35
                        'roles' => ['?']
36
                    ],
37
                    [
38
                        'allow' => true,
39
                        'actions' => ['flush-meta-cache', 'flush-counter-cache', 'flush-robots-cache'],
40
                        'roles' => ['cache manage'],
41
                    ],
42
                    [
43
                        'allow' => false,
44
                        'actions' => ['flush-meta-cache', 'flush-counter-cache', 'flush-robots-cache'],
45
                    ],
46
                    [
47
                        'allow' => true,
48
                        'roles' => ['seo manage'],
49
                    ],
50
                ],
51
            ],
52
            [
53
              'class' => 'yii\filters\PageCache',
54
              'only' => ['GetRobots'],
55
              'duration' => 24 * 60 * 60,
56
              'dependency' => ActiveRecordHelper::getCommonTag(Config::className()),
57
            ]
58
        ];
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function actions()
65
    {
66
        return [
67
            'update-editable-counter' => [
68
                'class' => UpdateEditable::class,
69
                'modelName' => get_class(Yii::$container->get(Counter::class)),
70
                'allowedAttributes' => [
71
                    'position' => [Counter::class, "updateInfoForEditable"],
72
                ],
73
            ],
74
        ];
75
    }
76
77
    /**
78
     * Index page
79
     * @return string
80
     */
81
    public function actionIndex()
82
    {
83
        return $this->render('index');
84
    }
85
86
    /**
87
     * return robots.txt
88
     */
89
    public function actionGetRobots()
90
    {
91
        $robots = Robots::getRobots();
92
        $response = \Yii::$app->response;
93
        $response->headers->set('Content-Type', 'text/plain');
94
        $response->format = \yii\web\Response::FORMAT_RAW;
95
        $response->data = $robots;
96
        \Yii::$app->end();
97
    }
98
99
    /**
100
     * Show list of Meta tag models
101
     * @return string
102
     */
103 View Code Duplication
    public function actionMeta()
0 ignored issues
show
Coding Style introduced by
actionMeta uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
104
    {
105
        $searchModel = new Meta();
106
        $dataProvider = $searchModel->search($_GET);
107
        return $this->render(
108
            'meta',
109
            [
110
                'dataProvider' => $dataProvider,
111
                'searchModel' => $searchModel,
112
            ]
113
        );
114
    }
115
116
    /**
117
     * Updates an existing Meta model.
118
     * If update is successful, the browser will be redirected to the 'meta' page.
119
     * @param $id
120
     * @return string
121
     * @throws \yii\web\NotFoundHttpException
122
     */
123 View Code Duplication
    public function actionUpdateMeta($id)
0 ignored issues
show
Coding Style introduced by
actionUpdateMeta uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
124
    {
125
        /* @var $model Meta */
126
        $model = Meta::find()->where(
127
            [
128
                'key' => $id,
129
            ]
130
        )->one();
131
        if ($model !== null) {
132
            if ($model->load($_POST) && $model->validate()) {
133
                if ($model->save()) {
134
                    Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved'));
135
                    $returnUrl = Yii::$app->request->get('returnUrl', ['meta']);
136
                    switch (Yii::$app->request->post('action', 'save')) {
137
                        case 'next':
138
                            return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect(a...rnUrl' => $returnUrl)); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...oller::actionUpdateMeta of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
139
                                [
140
                                    'create-meta',
141
                                    'returnUrl' => $returnUrl,
142
                                ]
143
                            );
144
                        case 'back':
145
                            return $this->redirect($returnUrl);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect($returnUrl); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...oller::actionUpdateMeta of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
146
                        default:
147
                            return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect(\...nUrl' => $returnUrl))); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...oller::actionUpdateMeta of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
148
                                Url::toRoute(
149
                                    [
150
                                        'update-meta',
151
                                        'id' => $model->getPrimaryKey(),
152
                                        'returnUrl' => $returnUrl,
153
                                    ]
154
                                )
155
                            );
156
                    }
157
                }
158
159
            } else {
160
                return $this->render(
161
                    'updateMeta',
162
                    [
163
                        'model' => $model,
164
                    ]
165
                );
166
            }
167
        } else {
168
            throw new NotFoundHttpException('Meta tag '.$id.' not found');
169
        }
170
    }
171
172
    /**
173
     * Creates a new Meta model.
174
     * If creation is successful, the browser will be redirected to the 'meta' page.
175
     * @return string|\yii\web\Response
176
     */
177
    public function actionCreateMeta()
0 ignored issues
show
Coding Style introduced by
actionCreateMeta uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
178
    {
179
        $model = new Meta();
180
        if ($model->load($_POST) && $model->save()) {
181
            Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved'));
182
            $returnUrl = Yii::$app->request->get('returnUrl', ['meta']);
183
            switch (Yii::$app->request->post('action', 'save')) {
184
                case 'next':
185
                    return $this->redirect(
186
                        [
187
                            'create-meta',
188
                            'returnUrl' => $returnUrl,
189
                        ]
190
                    );
191
                case 'back':
192
                    return $this->redirect($returnUrl);
193
                default:
194
                    return $this->redirect(
195
                        Url::toRoute(
196
                            [
197
                                'update-meta',
198
                                'id' => $model->key,
199
                                'returnUrl' => $returnUrl,
200
                            ]
201
                        )
202
                    );
203
            }
204
        } else {
205
            return $this->render(
206
                'createMeta',
207
                [
208
                    'model' => $model,
209
                ]
210
            );
211
        }
212
    }
213
214
    /**
215
     * Deletes an existing Meta model.
216
     * If deletion is successful, the browser will be redirected to the 'meta' page.
217
     * @param $id
218
     * @return \yii\web\Response
219
     */
220
    public function actionDeleteMeta($id)
221
    {
222
        Meta::deleteAll('`key` = :id', [':id' => $id]);
223
        return $this->redirect(['meta']);
224
    }
225
226
    /**
227
     * Deletes an existing Meta models.
228
     * If deletion is successful, the browser will be redirected to the 'index' page.
229
     * @return \yii\web\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
230
     */
231
    public function actionDeleteMetas()
0 ignored issues
show
Coding Style introduced by
actionDeleteMetas uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
232
    {
233
        if (isset($_POST['metas'])) {
234
            return Meta::deleteAll(
235
                [
236
                    'in',
237
                    'key',
238
                    $_POST['metas'],
239
                ]
240
            );
241
        }
242
        return false;
243
    }
244
245
    /**
246
     * Deletes meta value from cache
247
     * @return bool if no error happens during deletion
248
     */
249
    public function actionFlushMetaCache()
250
    {
251
        return \Yii::$app->getCache()->delete(\Yii::$app->getModule('seo')->cacheConfig['metaCache']['name']);
252
    }
253
254
    /**
255
     * Show list of Counter models
256
     * @return string
257
     */
258
    public function actionCounter()
0 ignored issues
show
Coding Style introduced by
actionCounter uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
259
    {
260
        $searchModel = new Counter();
261
        $dataProvider = $searchModel->search($_GET);
262
        AceHelper::setAceScript($this);
263
        return $this->render(
264
            'counter',
265
            [
266
                'dataProvider' => $dataProvider,
267
                'searchModel' => $searchModel,
268
            ]
269
        );
270
    }
271
272
    /**
273
     * Updates an existing Counter model.
274
     * If update is successful, the browser will be redirected to the 'meta' page.
275
     * @param $id
276
     * @return string
277
     * @throws \yii\web\NotFoundHttpException
278
     */
279 View Code Duplication
    public function actionUpdateCounter($id)
0 ignored issues
show
Coding Style introduced by
actionUpdateCounter uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
280
    {
281
        /* @var $model Counter */
282
        $model = Counter::find()->where(
283
            [
284
                'id' => $id,
285
            ]
286
        )->one();
287
        AceHelper::setAceScript($this);
288
        if ($model !== null) {
289
            if ($model->load($_POST) && $model->validate()) {
290
                if ($model->save()) {
291
                    Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved'));
292
                    $returnUrl = Yii::$app->request->get('returnUrl', ['counter']);
293
                    switch (Yii::$app->request->post('action', 'save')) {
294
                        case 'next':
295
                            return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect(a...rnUrl' => $returnUrl)); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...er::actionUpdateCounter of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
296
                                [
297
                                    'create-counter',
298
                                    'returnUrl' => $returnUrl,
299
                                ]
300
                            );
301
                        case 'back':
302
                            return $this->redirect($returnUrl);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect($returnUrl); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...er::actionUpdateCounter of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
303
                        default:
304
                            return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect(\...nUrl' => $returnUrl))); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...er::actionUpdateCounter of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
305
                                Url::toRoute(
306
                                    [
307
                                        'update-counter',
308
                                        'id' => $model->id,
309
                                        'returnUrl' => $returnUrl
310
                                    ]
311
                                )
312
                            );
313
                    }
314
                }
315
316
            } else {
317
                return $this->render(
318
                    'updateCounter',
319
                    [
320
                        'model' => $model,
321
                    ]
322
                );
323
            }
324
        } else {
325
            throw new NotFoundHttpException('counter #'.$id.' not found');
326
        }
327
    }
328
329
    /**
330
     * Creates a new Counter model.
331
     * If creation is successful, the browser will be redirected to the 'counter' page.
332
     * @return string|\yii\web\Response
333
     */
334
    public function actionCreateCounter()
0 ignored issues
show
Coding Style introduced by
actionCreateCounter uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
335
    {
336
        $model = new Counter();
337
        AceHelper::setAceScript($this);
338
        if ($model->load($_POST) && $model->save()) {
339
            Yii::$app->session->setFlash('success', Yii::t('app', 'Record has been saved'));
340
            $returnUrl = Yii::$app->request->get('returnUrl', ['counter']);
341
            if (Yii::$app->request->post('action', 'back') == 'next') {
342
                $route = ['create-counter', 'returnUrl' => $returnUrl];
343
                if (!is_null(Yii::$app->request->get('parent_id', null))) {
344
                    $route['parent_id'] = Yii::$app->request->get('parent_id');
345
                }
346
                return $this->redirect($route);
347
            } else {
348
                return $this->redirect($returnUrl);
349
            }
350
351
        } else {
352
            return $this->render(
353
                'createCounter',
354
                [
355
                    'model' => $model,
356
                ]
357
            );
358
        }
359
    }
360
361
    /**
362
     * Deletes an existing Meta model.
363
     * If deletion is successful, the browser will be redirected to the 'counter' page.
364
     * @param $id
365
     * @return \yii\web\Response
366
     */
367
    public function actionDeleteCounter($id)
368
    {
369
        Counter::deleteAll('`id` = :id', [':id' => $id]);
370
        return $this->redirect(['counter']);
371
    }
372
373
    /**
374
     * Deletes an existing Counter models.
375
     * If deletion is successful, the browser will be redirected to the 'counter' page.
376
     * @return \yii\web\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
377
     */
378
    public function actionDeleteCounters()
0 ignored issues
show
Coding Style introduced by
actionDeleteCounters uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
379
    {
380
        if (isset($_POST['counters'])) {
381
            return Counter::deleteAll(
382
                [
383
                    'in',
384
                    'id',
385
                    $_POST['counters'],
386
                ]
387
            );
388
        }
389
        return false;
390
    }
391
392
    /**
393
     * Deletes counter value from cache
394
     * @return bool if no error happens during deletion
395
     */
396
    public function actionFlushCounterCache()
397
    {
398
        return \Yii::$app->getCache()->delete(\Yii::$app->getModule('seo')->cacheConfig['counterCache']['name']);
399
    }
400
401
    /**
402
     * Update robots.txt
403
     * @return string|\yii\web\Response
404
     * @throws \yii\web\NotFoundHttpException
405
     */
406
    public function actionRobots()
407
    {
408
        $model = Robots::getModel();
409
        if ($model === null) {
410
            $model = new Robots();
411
        }
412
413
        if (Yii::$app->request->isPost) {
414
            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
415
                $model->save();
416
                return $this->refresh();
417
            }
418
        }
419
420
        return $this->render('robots', ['model' => $model]);
421
    }
422
423
    /**
424
     * Show list of Redirect models
425
     * @return string
426
     */
427 View Code Duplication
    public function actionRedirect()
0 ignored issues
show
Coding Style introduced by
actionRedirect uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
428
    {
429
        $searchModel = new Redirect(['scenario' => 'search']);
430
        $dataProvider = $searchModel->search($_GET);
431
        return $this->render(
432
            'redirect',
433
            [
434
                'dataProvider' => $dataProvider,
435
                'searchModel' => $searchModel,
436
            ]
437
        );
438
    }
439
440
    /**
441
     * Creates a new Redirect model.
442
     * If creation is successful, the browser will be redirected to the 'redirect' page.
443
     * @return string|\yii\web\Response
444
     */
445
    public function actionCreateRedirect()
0 ignored issues
show
Coding Style introduced by
actionCreateRedirect uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
446
    {
447
        $model = new Redirect(['active' => true]);
448
        if ($model->load($_POST) && $model->save()) {
449
            $action = Yii::$app->request->post('action', 'save');
450
            $returnUrl = Yii::$app->request->get('returnUrl', ['index']);
451
            switch ($action) {
452
                case 'next':
453
                    return $this->redirect(
454
                        [
455
                            'create-redirect',
456
                            'returnUrl' => $returnUrl,
457
                        ]
458
                    );
459
                case 'back':
460
                    return $this->redirect($returnUrl);
461
                default:
462
                    return $this->redirect(
463
                        Url::toRoute(
464
                            [
465
                                'update-redirect',
466
                                'id' => $model->id,
467
                                'returnUrl' => $returnUrl,
468
                            ]
469
                        )
470
                    );
471
            }
472
        } else {
473
            return $this->render(
474
                'createRedirect',
475
                [
476
                    'model' => $model,
477
                ]
478
            );
479
        }
480
    }
481
482
    /**
483
     * Creates a new Redirect models.
484
     * @return string
485
     */
486
    public function actionCreateRedirects()
0 ignored issues
show
Coding Style introduced by
actionCreateRedirects uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
487
    {
488
        if (isset($_POST['redirects'])) {
489
            $all = 0;
490
            $added = 0;
491 View Code Duplication
            if (isset($_POST['redirects']['static']) && !empty($_POST['redirects']['static'])) {
492
                $static = explode("\n", $_POST['redirects']['static']);
493
                foreach ($static as $redirectStr) {
494
                    $all++;
495
                    $r = explode("\t", $redirectStr);
496
                    $redirect = new Redirect(
497
                        [
498
                            'type' => Redirect::TYPE_STATIC,
499
                            'from' => trim($r[0]),
500
                            'to' => trim($r[1]),
501
                            'active' => true,
502
                        ]
503
                    );
504
                    if ($redirect->save()) {
505
                        $added++;
506
                    }
507
                }
508
            }
509 View Code Duplication
            if (isset($_POST['redirects']['regular']) && !empty($_POST['redirects']['regular'])) {
510
                $regular = explode("\n", $_POST['redirects']['regular']);
511
                foreach ($regular as $redirectStr) {
512
                    $all++;
513
                    $r = explode("\t", $redirectStr);
514
                    $redirect = new Redirect(
515
                        [
516
                            'type' => Redirect::TYPE_PREG,
517
                            'from' => trim($r[0]),
518
                            'to' => trim($r[1]),
519
                            'active' => true,
520
                        ]
521
                    );
522
                    if ($redirect->save()) {
523
                        $added++;
524
                    }
525
                }
526
            }
527
528
            Yii::$app->session->setFlash('success', Yii::t('app', 'Records has been saved'));
529
            $returnUrl = Yii::$app->request->get('returnUrl', ['/redirect']);
530
            switch (Yii::$app->request->post('action', 'save')) {
531
                case 'next':
532
                    return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect(a...rnUrl' => $returnUrl)); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...::actionCreateRedirects of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
533
                        [
534
                            'create-redirects',
535
                            'returnUrl' => $returnUrl,
536
                        ]
537
                    );
538
                case 'back':
539
                    return $this->redirect($returnUrl);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect($returnUrl); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...::actionCreateRedirects of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
540
                default:
541
                    return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect(\...nUrl' => $returnUrl))); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...::actionCreateRedirects of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
542
                        Url::toRoute(
543
                            [
544
                                'redirects',
545
                                'returnUrl' => $returnUrl,
546
                            ]
547
                        )
548
                    );
549
            }
550
551
        } else {
552
            return $this->render('createRedirects');
553
        }
554
555
    }
556
557
    /**
558
     * Updates an existing Redirect model.
559
     * If update is successful, the browser will be redirected to the 'redirect' page.
560
     * @param $id
561
     * @return string
562
     * @throws \yii\web\NotFoundHttpException
563
     */
564 View Code Duplication
    public function actionUpdateRedirect($id)
0 ignored issues
show
Coding Style introduced by
actionUpdateRedirect uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
565
    {
566
        /* @var $model Redirect */
567
        $model = Redirect::find()->where(
568
            [
569
                'id' => $id,
570
            ]
571
        )->one();
572
        if ($model !== null) {
573
            if ($model->load($_POST) && $model->validate()) {
574
                if ($model->save()) {
575
                    Yii::$app->session->setFlash('success', Yii::t('app', 'Records has been saved'));
576
                    $returnUrl = Yii::$app->request->get('returnUrl', ['redirect']);
577
                    switch (Yii::$app->request->post('action', 'save')) {
578
                        case 'next':
579
                            return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect(a...rnUrl' => $returnUrl)); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...r::actionUpdateRedirect of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
580
                                [
581
                                    'create-redirect',
582
                                    'returnUrl' => $returnUrl,
583
                                ]
584
                            );
585
                        case 'back':
586
                            return $this->redirect($returnUrl);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect($returnUrl); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...r::actionUpdateRedirect of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
587
                        default:
588
                            return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect(\...nUrl' => $returnUrl))); (yii\web\Response) is incompatible with the return type documented by app\modules\seo\controll...r::actionUpdateRedirect of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
589
                                Url::toRoute(
590
                                    [
591
                                        'update-redirect',
592
                                        'id' => $model->id,
593
                                        'returnUrl' => $returnUrl,
594
                                    ]
595
                                )
596
                            );
597
                    }
598
                }
599
            } else {
600
                return $this->render(
601
                    'updateRedirect',
602
                    [
603
                        'model' => $model,
604
                    ]
605
                );
606
            }
607
        } else {
608
            throw new NotFoundHttpException('redirect #'.$id.' not found');
609
        }
610
    }
611
612
    /**
613
     * Deletes an existing Redirect model.
614
     * If deletion is successful, the browser will be redirected to the 'redirect' page.
615
     * @param $id
616
     * @return \yii\web\Response
617
     */
618
    public function actionDeleteRedirect($id)
619
    {
620
        Redirect::deleteAll('`id` = :id', [':id' => $id]);
621
        return $this->redirect(['redirect']);
622
    }
623
624
    /**
625
     * Deletes an existing Redirect models.
626
     * If deletion is successful, the browser will be redirected to the 'redirect' page.
627
     * @return \yii\web\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
628
     */
629
    public function actionDeleteRedirects()
0 ignored issues
show
Coding Style introduced by
actionDeleteRedirects uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
630
    {
631
        if (isset($_POST['redirects'])) {
632
            return Redirect::deleteAll(
633
                [
634
                    'in',
635
                    'id',
636
                    $_POST['redirects'],
637
                ]
638
            );
639
        }
640
        return false;
641
    }
642
643
    public function actionGenerateRedirectFile()
644
    {
645
        echo Redirect::generateRedirectFile();
646
    }
647
648
    public function actionDeleteRedirectFile()
649
    {
650
        echo (int)Redirect::deleteRedirectFile();
651
    }
652
653
    public function actionEcommerce()
654
    {
655
        $yaCounter = $gaCounter = ['id' => '', 'active' => 0];
656
657
        if (Yii::$app->request->isPost) {
658
            $gaCounter = array_merge($gaCounter, Yii::$app->request->post('GoogleCounter', []));
659
            $yaCounter = array_merge($yaCounter, Yii::$app->request->post('YandexCounter', []));
660
661
            $model = Config::getModelByKey('ecommerceCounters');
662
            if (empty($model)) {
663
                $model = new Config();
664
                $model->key = 'ecommerceCounters';
665
            }
666
            $model->value = Json::encode(['google' => $gaCounter, 'yandex' => $yaCounter]);
667
            $model->save();
668
        }
669
670
        $counters = Config::getModelByKey('ecommerceCounters');
671
        if (!empty($counters)) {
672
            $counters = Json::decode($counters->value);
673
            $gaCounter = !empty($counters['google']) ? $counters['google'] : $gaCounter;
674
            $yaCounter = !empty($counters['yandex']) ? $counters['yandex'] : $yaCounter;
675
        }
676
677
        return $this->render(
678
            'ecommerce-counters',
679
            [
680
                'gaCounter' => $gaCounter,
681
                'yaCounter' => $yaCounter
682
            ]
683
        );
684
    }
685
686
    public static function renderEcommerceCounters(Event $event)
687
    {
688
        /** @var OrderTransaction $orderTransaction */
689
        $orderTransaction = OrderTransaction::findOne($event->data['transactionId']);
690
        $config = Config::getModelByKey('ecommerceCounters');
691
        if (empty($event->data['transactionId']) || empty($config) || !isset($orderTransaction->order)) {
692
            return ;
693
        }
694
695
        if (!empty($orderTransaction->order->items)) {
696
            $products = [];
697
            foreach ($orderTransaction->order->items as $item) {
698
                $product = Yii::$container->get(Product::class);
699
                $productModel = $product::findById($item->product_id, null, null);
700
                if (empty($productModel)) {
701
                    continue;
702
                }
703
                $category = Category::findById($productModel->main_category_id);
704
                $category = empty($category) ? 'Магазин' : str_replace('\'', '', $category->name);
705
706
                $products[] = [
707
                    'id' => $productModel->id,
708
                    'name' => str_replace('\'', '', $productModel->name),
709
                    'price' => number_format($productModel->price, 2, '.', ''),
710
                    'category' => $category,
711
                    'qnt' => $item->quantity
712
                ];
713
            }
714
715
            $order = [
716
                'id' => $orderTransaction->order->id,
717
                'total' => number_format($orderTransaction->order->total_price, 2, '.', ''),
718
            ];
719
720
            echo Yii::$app->view->renderFile(Yii::getAlias('@app/modules/seo/views/manage/_ecommerceCounters.php'), [
721
                'order' => $order,
722
                'products' => $products,
723
                'config' => Json::decode($config->value)
724
            ]);
725
        }
726
    }
727
}
728