DefController::actionInstallterm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace nkostadinov\taxonomy\controllers;
4
5
use nkostadinov\taxonomy\models\TaxonomyTerms;
6
use Yii;
7
use nkostadinov\taxonomy\models\TaxonomyDef;
8
use nkostadinov\taxonomy\models\TaxonomyDefSearch;
9
use yii\base\InvalidConfigException;
10
use yii\data\ActiveDataProvider;
11
use yii\helpers\Url;
12
use yii\web\Controller;
13
use yii\web\NotFoundHttpException;
14
use yii\filters\VerbFilter;
15
16
// fcgi doesn't have STDIN and STDOUT defined by default
17
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
18
defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w'));
19
20
class Migrator extends \yii\console\controllers\MigrateController
21
{
22
    public function __construct($id, $module, $config = [])
23
    {
24
        parent::__construct($id, $module, $config); // TODO: Change the autogenerated stub
25
    }
26
27
    public function runTaxonomy($class, $migrationPath)
0 ignored issues
show
Unused Code introduced by
The parameter $migrationPath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        ob_start();
30
        //$this->migrationPath = Yii::getAlias($migrationPath);
31
        if($this->beforeAction('up'))
0 ignored issues
show
Documentation introduced by
'up' is of type string, but the function expects a object<yii\base\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
            $this->migrateUp($class);
33
        $result = ob_get_contents();
34
        return $result;
35
    }
36
37
    public function removeTaxonomy($class, $migrationPath)
0 ignored issues
show
Unused Code introduced by
The parameter $migrationPath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        ob_start();
40
        //$this->migrationPath = Yii::getAlias($migrationPath);
41
        if($this->beforeAction('down'))
0 ignored issues
show
Documentation introduced by
'down' is of type string, but the function expects a object<yii\base\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
            $this->migrateDown($class);
43
        $result = ob_get_contents();
44
        return $result;
45
    }
46
}
47
48
/**
49
 * DefController implements the CRUD actions for TaxonomyDef model.
50
 */
51
class DefController extends Controller
52
{
53
    public function behaviors()
54
    {
55
        return [
56
            'verbs' => [
57
                'class' => VerbFilter::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
                'actions' => [
59
                    'delete' => ['post'],
60
                ],
61
            ],
62
        ];
63
    }
64
65
    /**
66
     * Lists all TaxonomyDef models.
67
     * @return mixed
68
     * @throws InvalidConfigException
69
     */
70
    public function actionIndex()
71
    {
72
        if(!$this->getComponent()->isInstalled())
73
            throw new InvalidConfigException("Please run the migration first!");
74
75
        $searchModel = new TaxonomyDefSearch();
76
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
77
78
        return $this->render('index', [
79
            'searchModel' => $searchModel,
80
            'dataProvider' => $dataProvider,
81
        ]);
82
    }
83
84
    /**
85
     * Displays a single TaxonomyDef model.
86
     * @param integer $id
87
     * @return mixed
88
     */
89
    public function actionView($id)
90
    {
91
        $termProvider = new ActiveDataProvider([
92
            'query' => TaxonomyTerms::find()->andFilterWhere(['taxonomy_id' => $id]),
93
            'sort' => [
94
                // Set the default sort by name ASC and created_at DESC.
95
                'defaultOrder' => [
96
                    'total_count' => SORT_DESC,
97
                ]
98
            ],
99
        ]);
100
101
        return $this->render('view', [
102
            'model' => $this->findModel($id),
103
            'termProvider' => $termProvider
104
        ]);
105
    }
106
107
    /**
108
     * Creates a new TaxonomyDef model.
109
     * If creation is successful, the browser will be redirected to the 'view' page.
110
     * @return mixed
111
     */
112
    public function actionCreate()
113
    {
114
        $model = new TaxonomyDef();
115
        $definitions = $this->getComponent()->getDefinitions();
116
117
        if ($model->load(Yii::$app->request->post())) {
118
            //install the term
119
            $term = Yii::createObject($model->attributes);
120
                //$this->getComponent()->getTerm($model->name);
121
            $term->install();
122
            $migration = new Migrator('migrate', Yii::$app);
123
            $messsage = $migration->runTaxonomy($term->migration, $term->migrationPath);
124
            Yii::$app->session->setFlash('info', $messsage, true);
125
126
            return $this->redirect(['index', 'id' => $model->id]);
127
        } else {
128
            return $this->render('create', [
129
                'model' => $model,
130
                'definitions' => $definitions,
131
            ]);
132
        }
133
    }
134
135
    /**
136
     * Updates an existing TaxonomyDef model.
137
     * If update is successful, the browser will be redirected to the 'view' page.
138
     * @param integer $id
139
     * @return mixed
140
     */
141
    public function actionUpdate($id)
142
    {
143
        $model = $this->findModel($id);
144
        $definitions = $this->getComponent()->getDefinitions();
145
146
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
147
            return $this->redirect(['view', 'id' => $model->id]);
148
        } else {
149
            return $this->render('update', [
150
                'model' => $model,
151
                'definitions' => $definitions,
152
            ]);
153
        }
154
    }
155
156
    /**
157
     * Deletes an existing TaxonomyDef model.
158
     * If deletion is successful, the browser will be redirected to the 'index' page.
159
     * @param integer $id
160
     * @return mixed
161
     */
162
    public function actionDelete($id)
163
    {
164
        $model = $this->findModel($id);
165
        //UNinstall the term
166
        $term = $this->getComponent()->getTerm($model->name);
167
        $migration = new Migrator('migrate', Yii::$app);
168
        $messsage = $migration->removeTaxonomy($term->migration, $term->migrationPath);
169
        Yii::$app->session->setFlash('info', $messsage, true);
170
171
        $term->uninstall();
172
173
174
        return $this->redirect(['index']);
175
    }
176
177
    /**
178
     * Finds the TaxonomyDef model based on its primary key value.
179
     * If the model is not found, a 404 HTTP exception will be thrown.
180
     * @param integer $id
181
     * @return TaxonomyDef the loaded model
182
     * @throws NotFoundHttpException if the model cannot be found
183
     */
184
    protected function findModel($id)
185
    {
186
        if (($model = TaxonomyDef::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \nkostadinov\taxonomy\mo...onomyDef::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 187 which is incompatible with the return type documented by nkostadinov\taxonomy\con...efController::findModel of type nkostadinov\taxonomy\models\TaxonomyDef.
Loading history...
187
            return $model;
188
        } else {
189
            throw new NotFoundHttpException('The requested page does not exist.');
190
        }
191
    }
192
193
    public function getComponent()
194
    {
195
        if(\Yii::$app->has($this->module->component))
0 ignored issues
show
Bug introduced by
The property component does not seem to exist. Did you mean _components?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
196
            return \Yii::$app->{$this->module->component};
0 ignored issues
show
Bug introduced by
The property component does not seem to exist. Did you mean _components?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
197
        else
198
            throw new InvalidConfigException("Cannot find taxonomy component({$this->module->component})");
0 ignored issues
show
Bug introduced by
The property component does not seem to exist. Did you mean _components?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
199
    }
200
201
    public function actionInstall()
202
    {
203
        if(!$this->getComponent()->isInstalled() and \Yii::$app->request->isPost) {
204
            //start installation
205
            if($this->getComponent()) {
206
                $this->getComponent()->install();
207
208
                $this->redirect(['/'.$this->module->id . '/' . $this->id . '/index']);
209
            } else
210
                throw new InvalidConfigException("Cannot find taxonomy component({$this->module->component})");
0 ignored issues
show
Bug introduced by
The property component does not seem to exist. Did you mean _components?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
211
        }
212
        return $this->render('install');
213
    }
214
215
    public function actionInstallterm($term)
216
    {
217
        $term = $this->getComponent()->getTerm($term);
218
        $term->install();
219
    }
220
}
221