Completed
Push — master ( 6ebb19...d1d9d0 )
by Andrii
04:24
created

PurseController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 12.24%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 123
ccs 6
cts 49
cp 0.1224
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A actions() 0 35 1
A behaviors() 0 12 1
B actionGenerateAll() 0 27 7
A actionGenerateMonthlyDocument() 0 4 1
A actionGenerateDocument() 0 4 1
A generateDocument() 0 13 2
A asPdf() 0 6 1
A actionPreGenerateDocument() 0 12 3
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\controllers;
12
13
use hipanel\actions\IndexAction;
14
use hipanel\actions\RedirectAction;
15
use hipanel\actions\SmartPerformAction;
16
use hipanel\actions\SmartUpdateAction;
17
use hipanel\actions\ValidateFormAction;
18
use hipanel\actions\ViewAction;
19
use hipanel\filters\EasyAccessControl;
20
use hipanel\modules\document\models\Statistic as DocumentStatisticModel;
21
use hipanel\modules\finance\models\Purse;
22
use hipanel\modules\finance\widgets\StatisticTableGenerator;
23
use hiqdev\hiart\ResponseErrorException;
24
use Yii;
25
26
class PurseController extends \hipanel\base\CrudController
27
{
28
    public function behaviors()
29
    {
30
        return array_merge(parent::behaviors(), [
31
            [
32
                'class' => EasyAccessControl::class,
33
                'actions' => [
34
                    'update' => 'bill.update',
35
                    '*' => 'bill.read',
36
                ],
37
            ],
38
        ]);
39
    }
40
41 1
    public function actions()
42
    {
43 1
        return array_merge(parent::actions(), [
44 1
            'index' => [
45
                'class' => IndexAction::class,
46
            ],
47
            'view' => [
48
                'class' => ViewAction::class,
49
            ],
50
            'update' => [
51
                'class' => SmartUpdateAction::class,
52
            ],
53
            'update-contact' => [
54
                'class' => SmartUpdateAction::class,
55
            ],
56
            'update-requisite' => [
57
                'class' => SmartUpdateAction::class,
58
            ],
59
            'validate-form' => [
60
                'class' => ValidateFormAction::class,
61
            ],
62
            'invoice-archive' => [
63
                'class' => RedirectAction::class,
64 1
                'error' => Yii::t('hipanel', 'Under construction'),
65
            ],
66
            'generate-and-save-monthly-document' => [
67
                'class' => SmartPerformAction::class,
68 1
                'success' => Yii::t('hipanel:finance', 'Document updated'),
69
            ],
70
            'generate-and-save-document' => [
71
                'class' => SmartPerformAction::class,
72 1
                'success' => Yii::t('hipanel:finance', 'Document updated'),
73
            ],
74
        ]);
75
    }
76
77
    public function actionGenerateAll()
78
    {
79
        $request = Yii::$app->request;
80
        $model = new DocumentStatisticModel();
81
        $statisticByTypes = DocumentStatisticModel::batchPerform('get-stats', $model->getAttributes([
82
            'types',
83
            'since',
84
        ]));
85
86
        $type = $request->post('type');
87
        if ($request->isAjax && $type && in_array($type, explode(',', $model->types), true)) {
88
            return StatisticTableGenerator::widget(['type' => $type, 'statistic' => $statisticByTypes[$type]]);
89
        } else {
90
            if ($request->isPost) {
91
                try {
92
                    Purse::batchPerform('generate-and-save-all-monthly-documents', [
93
                        'type' => $type,
94
                        'client_types' => $type === 'acceptance' ? 'employee' : null,
95
                    ]);
96
                } catch (ResponseErrorException $e) {
97
                    Yii::$app->getSession()->setFlash('error', Yii::t('hipanel:finance', 'Failed to generate document! Check requisites!'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
98
                }
99
            }
100
101
            return $this->render('generate-all', ['statisticByTypes' => $statisticByTypes]);
102
        }
103
    }
104
105
    public function actionGenerateMonthlyDocument($id, $type, $month = null)
106
    {
107
        return $this->generateDocument('generate-monthly-document', compact('id', 'type', 'month'));
108
    }
109
110
    public function actionGenerateDocument($id, $type)
111
    {
112
        return $this->generateDocument('generate-document', compact('id', 'type'));
113
    }
114
115
    public function generateDocument($action, $params)
116
    {
117
        try {
118
            $data = Purse::perform($action, $params);
119
        } catch (ResponseErrorException $e) {
120
            Yii::$app->getSession()->setFlash('error', Yii::t('hipanel:finance', 'Failed to generate document! Check requisites!'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
121
122
            return $this->redirect(['@client/view', 'id' => $params['id']]);
123
        }
124
        $this->asPdf();
125
126
        return $data;
127
    }
128
129
    protected function asPdf()
130
    {
131
        $response = Yii::$app->getResponse();
132
        $response->format = $response::FORMAT_RAW;
133
        $response->getHeaders()->add('content-type', 'application/pdf');
134
    }
135
136
    public function actionPreGenerateDocument($type)
137
    {
138
        $purse = new Purse(['scenario' => 'generate-and-save-monthly-document']);
139
        if ($purse->load(Yii::$app->request->post()) && $purse->validate()) {
140
            return $this->redirect([
141
                '@purse/generate-monthly-document',
142
                'id' => $purse->id,
143
                'type' => $type,
144
                'month' => $purse->month,
145
            ]);
146
        }
147
    }
148
}
149