Passed
Push — master ( 580dba...7607dd )
by Aleksandr
04:23 queued 02:35
created

ArticleController::actionDeleteUnusedImages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
4
namespace carono\exchange1c\controllers;
5
6
7
use carono\exchange1c\models\Article;
8
use yii\helpers\FileHelper;
9
use yii\helpers\Html;
10
use Yii;
11
12
/**
13
 * Class ArticleController
14
 *
15
 * @package carono\exchange1c\controllers
16
 */
17
class ArticleController extends Controller
18
{
19
    public function actionCreate($parent = null)
20
    {
21
        $article = new Article();
22
        $article->pos = Article::find()->andWhere(['[[parent_id]]' => $parent])->max('pos') + 10;
23
        $article->parent_id = $parent;
24
        if ($article->load(\Yii::$app->request->post())) {
25
            if ($article->save()) {
26
                return $this->redirect(['article/index']);
27
            }
28
29
            \Yii::$app->session->setFlash('error', Html::errorSummary($article));
0 ignored issues
show
Bug introduced by
The method setFlash() 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

29
            \Yii::$app->session->/** @scrutinizer ignore-call */ 
30
                                 setFlash('error', Html::errorSummary($article));

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...
30
        }
31
        return $this->render('update', ['article' => $article]);
32
    }
33
34
    public function actionUpdate($id)
35
    {
36
        $article = Article::findOne($id, true);
37
        if ($article->load(\Yii::$app->request->post())) {
38
            if ($article->save()) {
39
                return $this->redirect(['article/view', 'id' => $article->id]);
40
            }
41
42
            \Yii::$app->session->setFlash('error', Html::errorSummary($article));
43
        }
44
        return $this->render('update', ['article' => $article]);
45
    }
46
47
    public function actionView($id)
48
    {
49
        Yii::$app->getView()->registerJs("$('pre').each(function(i, block) { hljs.highlightBlock(block); });");
50
        $article = Article::findOne($id, true);
51
        return $this->render('view', ['article' => $article]);
52
    }
53
54
    public function actionDelete($id)
55
    {
56
        Article::findOne($id, true)->delete();
57
        return $this->redirect(['article/index']);
58
    }
59
60
    public function actionIndex()
61
    {
62
        $dataProvider = Article::find()->orderBy(['{{%article}}.[[pos]]' => SORT_ASC])->search();
63
        return $this->render('index', ['dataProvider' => $dataProvider]);
64
    }
65
66
    /**
67
     * Удаляем все реальные файлы, которые не используются в статьях
68
     */
69
    public function actionDeleteUnusedImages()
70
    {
71
        $content = Article::find()->select(['content' => 'group_concat([[content]])'])->scalar();
72
        $dir = Yii::getAlias(Yii::$app->controller->module->redactor->uploadDir);
73
        $files = Article::extractFilesFromString($content);
74
        $realFiles = FileHelper::findFiles($dir);
75
        array_walk($realFiles, function (&$item) use ($dir) {
76
            $item = str_replace('\\', '/', substr($item, strlen($dir)));
77
        });
78
        foreach (array_diff($realFiles, $files) as $file) {
79
            unlink($dir . '/' . $file);
80
        };
81
        return $this->redirect(['article/index']);
82
    }
83
84
    public function actionCreateReadme()
85
    {
86
        $badges = [];
87
        $lines = [];
88
        $titles =  Article::formTitleItems();
89
90
        $badges[] = '[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/carono/yii2-1c-exchange/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/carono/yii2-1c-exchange/?branch=master)';
91
        $badges[] = '[![Latest Stable Version](https://poser.pugx.org/carono/yii2-1c-exchange/v/stable)](https://packagist.org/packages/carono/yii2-1c-exchange)';
92
        $badges[] = '[![Total Downloads](https://poser.pugx.org/carono/yii2-1c-exchange/downloads)](https://packagist.org/packages/carono/yii2-1c-exchange)';
93
        $badges[] = '[![License](https://poser.pugx.org/carono/yii2-1c-exchange/license)](https://packagist.org/packages/carono/yii2-1c-exchange)';
94
        $badges[] = "\n";
95
        foreach (Article::find()->andWhere(['not', ['content' => '']])->all() as $article) {
96
            $lines[] = Html::a($article->name, false, ['name' => $article->id]) . "\n=\n";
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type null|string|array expected by parameter $url of yii\helpers\BaseHtml::a(). ( Ignorable by Annotation )

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

96
            $lines[] = Html::a($article->name, /** @scrutinizer ignore-type */ false, ['name' => $article->id]) . "\n=\n";
Loading history...
97
            $lines[] = str_replace('../file/article?file=', 'https://raw.github.com/carono/yii2-1c-exchange/HEAD/files/articles', $article->content);
98
            $lines[] = "\n";
99
        }
100
        $titles[] = "\n";
101
        $content = implode("\n", array_merge($badges, $titles, $lines));
102
        file_put_contents('README.md', $content);
103
    }
104
}