Completed
Push — master ( f5d363...109ed8 )
by Dmitry
12:12
created

CartController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/*
4
 * Finance module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-finance
7
 * @package   hipanel-module-finance
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\finance\controllers;
13
14
use hipanel\modules\client\models\Client;
15
use hipanel\modules\finance\cart\CartFinisher;
16
use hipanel\modules\finance\Module;
17
use Yii;
18
use yii\filters\AccessControl;
19
20
class CartController extends \yii\web\Controller
21
{
22
    /**
23
     * @var Module
24
     */
25
    public $module;
26
27
    public function behaviors()
28
    {
29
        return [
30
            'access' => [
31
                'class' => AccessControl::class,
32
                'only' => ['deposit', 'select', 'partial', 'full', 'finish'],
33
                'rules' => [
34
                    [
35
                        'allow' => true,
36
                        'roles' => ['@']
37
                    ]
38
                ]
39
            ]
40
        ];
41
    }
42
43
    public function renderDeposit($sum)
44
    {
45
        return $this->module->getMerchant()->renderDeposit([
46
            'sum' => $sum,
47
            'finishUrl' => '/finance/cart/finish',
48
        ]);
49
    }
50
51
    public function actionSelect()
52
    {
53
        $client = Client::findOne(['id' => Yii::$app->user->identity->id]);
54
        $cart = $this->module->getCart();
55
        $total = $cart->getTotal();
56
        $rest = $client->balance + $client->credit;
57
58
        if ($rest <= 0 && $total > 0) {
59
            return $this->renderDeposit($total);
60
        }
61
62
        return $this->render('select', [
63
            'client' => $client,
64
            'cart' => $cart,
65
            'rest' => $rest,
66
        ]);
67
    }
68
69
    public function actionFull()
70
    {
71
        return $this->renderDeposit($this->module->getCart()->getTotal());
72
    }
73
74
    public function actionPartial()
75
    {
76
        $client = Client::findOne(['id' => Yii::$app->user->identity->id]);
77
        $cart = $this->module->getCart();
78
79
        return $this->renderDeposit($cart->total - $client->balance - $client->credit);
80
    }
81
82
    public function actionFinish()
83
    {
84
        $cart = $this->module->getCart();
85
86
        $finisher = new CartFinisher(['cart' => $cart]);
87
        $finisher->run();
88
89
        $client = Client::findOne(['id' => Yii::$app->user->identity->id]);
90
91
        return $this->render('finish', [
92
            'balance' => $client->balance,
93
            'success' => $finisher->getSuccess(),
94
            'error' => $finisher->getError(),
95
            'pending' => $finisher->getPending(),
96
            'remarks' => (array)Yii::$app->getView()->params['remarks'],
97
        ]);
98
    }
99
100
    public function actionTest()
101
    {
102
        $cart = $this->module->getCart();
0 ignored issues
show
Unused Code introduced by
$cart is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
        return $this->render('finish', [
104
            'remarks' => [
105
                'tr' => Yii::$app->view->render('@hipanel/modules/domain/views/domain/_transferAttention'),
106
            ],
107
        ]);
108
    }
109
}
110