PurseController::asPdf()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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 View Code Duplication
    public function behaviors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        return array_merge(parent::behaviors(), [
31
            [
32
                'class' => EasyAccessControl::class,
33
                'actions' => [
34
                    'update,update-requisite,update-contact' => 'purse.update',
35
                    'pre-generate-document,generate-monthly-document,generate-document' => 'document.generate',
36
                    '*' => 'bill.read',
37
                ],
38
            ],
39
        ]);
40
    }
41 1
42
    public function actions()
43 1
    {
44 1
        return array_merge(parent::actions(), [
45
            'index' => [
46
                'class' => IndexAction::class,
47
            ],
48
            'view' => [
49
                'class' => ViewAction::class,
50
            ],
51
            'update' => [
52
                'class' => SmartUpdateAction::class,
53
            ],
54
            'update-contact' => [
55
                'class' => SmartUpdateAction::class,
56
            ],
57
            'update-requisite' => [
58
                'class' => SmartUpdateAction::class,
59
            ],
60
            'validate-form' => [
61
                'class' => ValidateFormAction::class,
62
            ],
63
            'invoice-archive' => [
64 1
                'class' => RedirectAction::class,
65
                'error' => Yii::t('hipanel', 'Under construction'),
66
            ],
67
            'generate-and-save-monthly-document' => [
68 1
                'class' => SmartPerformAction::class,
69
                'success' => Yii::t('hipanel:finance', 'Document updated'),
70
            ],
71
            'generate-and-save-document' => [
72 1
                'class' => SmartPerformAction::class,
73
                'success' => Yii::t('hipanel:finance', 'Document updated'),
74
            ],
75
        ]);
76
    }
77
78
    public function actionGenerateAll()
79
    {
80
        $request = Yii::$app->request;
81
        $model = new DocumentStatisticModel();
82
        $statisticByTypes = DocumentStatisticModel::batchPerform('get-stats', $model->getAttributes([
83
            'types',
84
            'since',
85
        ]));
86
87
        $type = $request->post('type');
88
        if ($request->isAjax && $type && in_array($type, explode(',', $model->types), true)) {
89
            return StatisticTableGenerator::widget(['type' => $type, 'statistic' => $statisticByTypes[$type]]);
90
        } else {
91
            if ($request->isPost) {
92
                try {
93
                    Purse::batchPerform('generate-and-save-all-monthly-documents', [
94
                        'type' => $type,
95
                        'client_types' => $type === 'acceptance' ? 'employee' : null,
96
                    ]);
97
                } catch (ResponseErrorException $e) {
98
                    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...
99
                }
100
            }
101
102
            return $this->render('generate-all', ['statisticByTypes' => $statisticByTypes]);
103
        }
104
    }
105
106
    public function actionGenerateMonthlyDocument($id, $type, $month = null)
107
    {
108
        return $this->generateDocument('generate-monthly-document', compact('id', 'type', 'month'));
109
    }
110
111
    public function actionGenerateDocument($id, $type)
112
    {
113
        return $this->generateDocument('generate-document', compact('id', 'type'));
114
    }
115
116
    public function generateDocument($action, $params)
117
    {
118
        try {
119
            $data = Purse::perform($action, $params);
120
        } catch (ResponseErrorException $e) {
121
            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...
122
123
            return $this->redirect(['@client/view', 'id' => $params['id']]);
124
        }
125
        $this->asPdf();
126
127
        return $data;
128
    }
129
130
    protected function asPdf()
131
    {
132
        $response = Yii::$app->getResponse();
133
        $response->format = $response::FORMAT_RAW;
134
        $response->getHeaders()->add('content-type', 'application/pdf');
135
    }
136
137
    public function actionPreGenerateDocument($type)
138
    {
139
        $purse = new Purse(['scenario' => 'generate-and-save-monthly-document']);
140
        if ($purse->load(Yii::$app->request->post()) && $purse->validate()) {
141
            return $this->redirect([
142
                '@purse/generate-monthly-document',
143
                'id' => $purse->id,
144
                'type' => $type,
145
                'month' => $purse->month,
146
            ]);
147
        }
148
    }
149
}
150