Completed
Push — master ( 524f68...5fad21 )
by Dmitry
02:35
created

SiteController::actionIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Asset Packagist
5
 *
6
 * @link      https://github.com/hiqdev/asset-packagist
7
 * @package   asset-packagist
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\assetpackagist\controllers;
13
14
use hiqdev\assetpackagist\models\AssetPackage;
15
use Yii;
16
use yii\filters\VerbFilter;
17
18
class SiteController extends \yii\web\Controller
19
{
20
    public function behaviors()
21
    {
22
        return [
23
            'access' => [
24
                'class' => VerbFilter::class,
25
                'actions' => [
26
                    'update' => ['post']
27
                ]
28
            ]
29
        ];
30
    }
31
32
    public function actionIndex()
33
    {
34
        return $this->render('index');
35
    }
36
37
    public function actionAbout()
38
    {
39
        return $this->render('about');
40
    }
41
42
    public function actionContact()
43
    {
44
        return $this->render('contact');
45
    }
46
47
    public function actionSearch($query)
48
    {
49
        $package = $this->getAssetPackage($query);
50
        $params = ['package' => $package, 'query' => $query, 'forceUpdate' => false];
51
52
        if ($package->canAutoUpdate()) {
53
            $params['forceUpdate'] = true;
54
        }
55
56
        return $this->render('search', $params);
57
    }
58
59
    /**
60
     * @param $query
61
     * @return AssetPackage
62
     */
63
    private static function getAssetPackage($query)
64
    {
65
        list($type, $name) = AssetPackage::splitFullName($query);
66
        $package = new AssetPackage($type, $name);
67
        $package->load();
68
        return $package;
69
    }
70
71
    public function actionUpdate()
72
    {
73
        $query = Yii::$app->request->post('query');
74
75
        $package = $this->getAssetPackage($query);
76
        if ($package->canBeUpdated()) {
77
            $package->update();
78
        } else {
79
            Yii::$app->session->addFlash('update-impossible', true);
80
        }
81
        $package->load();
82
83
        return $this->renderPartial('package-details', ['package' => $package]);
84
    }
85
}
86