Completed
Push — master ( e11301...78c2e1 )
by Dmitry
04:18
created

OrderController::beforeAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\controllers;
12
13
use hipanel\base\CrudController;
14
use hipanel\filters\EasyAccessControl;
15
use hipanel\modules\server\cart\ServerOrderDedicatedProduct;
16
use hipanel\modules\server\cart\ServerOrderProduct;
17
use hipanel\modules\server\models\Config;
18
use hipanel\modules\server\models\Osimage;
19
use hipanel\modules\server\Module;
20
use hiqdev\yii2\cart\actions\AddToCartAction;
21
use Yii;
22
use yii\web\ForbiddenHttpException;
23
24
class OrderController extends CrudController
25
{
26
    /**
27
     * OrderController constructor.
28
     *
29
     * @param string $id
30
     * @param Module $module
31
     * @param array $config
32
     * @throws ForbiddenHttpException
33
     */
34
    public function __construct(string $id, Module $module, array $config = [])
35
    {
36
        parent::__construct($id, $module, $config);
37
38
        if (!$module->orderIsAllowed) {
39
            throw new ForbiddenHttpException('Server order is not allowed');
40
        }
41
    }
42
43
    public function behaviors()
44
    {
45
        return array_merge(parent::behaviors(), [
46
            [
47
                'class' => EasyAccessControl::class,
48
                'actions' => [
49
                    'add-to-cart' => 'server.pay',
50
                    'add-to-cart-dedicated' => 'server.pay',
51
                    'index' => 'server.pay',
52
                    'xen-ssd' => 'server.pay',
53
                    'open-vz' => 'server.pay',
54
                    'dedicated' => '?',
55
                    '*' => 'server.read',
56
                ],
57
            ],
58
        ]);
59
    }
60
61
    public function beforeAction($action)
62
    {
63
        if ($action->id == 'add-to-cart-dedicated') {
64
            $this->enableCsrfValidation = false;
65
        }
66
67
        return parent::beforeAction($action);
68
    }
69
70
    public function actions()
71
    {
72
        return [
73
            'add-to-cart' => [
74
                'class' => AddToCartAction::class,
75
                'productClass' => ServerOrderProduct::class,
76
                'redirectToCart' => true,
77
            ],
78
            'add-to-cart-dedicated' => [
79
                'class' => AddToCartAction::class,
80
                'productClass' => ServerOrderDedicatedProduct::class,
81
                'redirectToCart' => true,
82
            ],
83
        ];
84
    }
85
86
    public function actionOrder($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

86
    public function actionOrder(/** @scrutinizer ignore-unused */ $id)

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

Loading history...
87
    {
88
        return $this->redirectOutside();
89
        /***
90
        $package = ServerHelper::getAvailablePackages(null, $id);
91
        $osImages = ServerHelper::getOsimages($package->tariff->type);
92
93
        return $this->render('order', [
94
            'package' => $package,
95
            'product' => new ServerOrderProduct(['tariff_id' => $package->tariff->id]),
96
            'groupedOsimages' => ServerHelper::groupOsimages($osImages),
97
            'panels' => ServerHelper::getPanels(),
98
        ]);
99
        ***/
100
    }
101
102
    public function actionIndex()
103
    {
104
        return $this->redirectOutside();
105
        /***
106
        return $this->render('index');
107
         ***/
108
    }
109
110
    public function actionXenSsd()
111
    {
112
        return $this->redirectOutside();
113
        /***
114
        return $this->render('xen_ssd', [
115
            'packages' => ServerHelper::getAvailablePackages(Tariff::TYPE_XEN),
116
            'tariffTypes' => Yii::$app->params['vdsproduct'],
117
        ]);
118
        ***/
119
    }
120
121
    public function actionOpenVz()
122
    {
123
        return $this->redirectOutside();
124
        /***
125
        return $this->render('open_vz', [
126
            'packages' => ServerHelper::getAvailablePackages(Tariff::TYPE_OPENVZ),
127
            'tariffTypes' => Yii::$app->params['vdsproduct'],
128
        ]);
129
        ***/
130
    }
131
132
    public function actionDedicated()
133
    {
134
        $this->layout = '@hipanel/server/order/yii/views/layouts/advancedhosting';
135
        $configs = Config::find()->getAvailable()->withSellerOptions()->withPrices()->addOption('batch', true)->createCommand()->send()->getData();
136
        $osimages = Osimage::find()->where(['type' => 'dedicated'])->all();
137
138
        return $this->render('dedicated', compact('configs', 'osimages'));
139
    }
140
141
    public function actionTariffsDetails()
142
    {
143
        return $this->render('tariffs_details');
144
    }
145
146
    public function actionAdvantages()
147
    {
148
        return $this->render('advantages');
149
    }
150
151
    public function actionWhatIsVds()
152
    {
153
        return $this->render('what_is_vds');
154
    }
155
156
    protected function redirectOutside()
157
    {
158
        $language = Yii::$app->language;
159
        $template = Yii::$app->params['module.server.order.redirect.url'];
160
        $url = preg_replace('/{language}/', $language, $template);
161
        return $this->redirect($url);
162
    }
163
}
164