Completed
Push — master ( d558ad...b35973 )
by Dmitry
11s
created

PurseController::asPdf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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-2017, 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\modules\finance\widgets\StatisticTableGenerator;
20
use hipanel\modules\finance\models\Purse;
21
use hipanel\filters\EasyAccessControl;
22
use hiqdev\hiart\ResponseErrorException;
23
use hipanel\modules\document\models\Statistic as DocumentStatisticModel;
24
use Yii;
25 1
26
class PurseController extends \hipanel\base\CrudController
27
{
28
29 1 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...
30 1
    {
31
        return array_merge(parent::behaviors(), [
32 1
            [
33 1
                'class' => EasyAccessControl::class,
34
                'actions' => [
35 1
                    'update' => 'bill.update',
36 1
                    '*' => 'bill.read',
37
                ],
38 1
            ],
39 1
        ]);
40
    }
41 1
42 1
43
    public function actions()
44 1
    {
45 1
        return array_merge(parent::actions(), [
46
            'index' => [
47 1
                'class' => IndexAction::class,
48 1
            ],
49 1
            'view' => [
50
                'class' => ViewAction::class,
51 1
            ],
52 1
            'update' => [
53 1
                'class' => SmartUpdateAction::class,
54
            ],
55 1
            'update-contact' => [
56 1
                'class' => SmartUpdateAction::class,
57 1
            ],
58 1
            'update-requisite' => [
59
                'class' => SmartUpdateAction::class,
60
            ],
61
            'validate-form' => [
62
                'class' => ValidateFormAction::class,
63
            ],
64
            'invoice-archive' => [
65
                'class' => RedirectAction::class,
66
                'error' => Yii::t('hipanel', 'Under construction'),
67
            ],
68
            'generate-and-save-monthly-document' => [
69
                'class' => SmartPerformAction::class,
70
                'success' => Yii::t('hipanel:finance', 'Document updated'),
71
            ],
72
            'generate-and-save-document' => [
73
                'class' => SmartPerformAction::class,
74
                'success' => Yii::t('hipanel:finance', 'Document updated'),
75
            ],
76
        ]);
77
    }
78
79
    public function actionGenerateAll()
80
    {
81
        $request = Yii::$app->request;
82
        $model = new DocumentStatisticModel();
83
        $statisticByTypes = DocumentStatisticModel::batchPerform('get-stats', $model->getAttributes([
84
            'types',
85
            'since',
86
        ]));
87
88
        $type = $request->post('type');
89
        if ($request->isAjax && $type && in_array($type, explode(',', $model->types))) {
90
            return StatisticTableGenerator::widget(['type' => $type, 'statistic' => $statisticByTypes[$type]]);
91
        } else {
92
            if ($request->isPost) {
93
                Purse::batchPerform('generate-and-save-all-monthly-documents', [
94
                    'type' => $type,
95
                ]);
96
            }
97
            return $this->render('generate-all', ['statisticByTypes' => $statisticByTypes]);
98
        }
99
    }
100
101
    public function actionGenerateMonthlyDocument($id, $type, $month = null)
102
    {
103
        return $this->generateDocument('generate-monthly-document', compact('id', 'type', 'month'));
104
    }
105
106
    public function actionGenerateDocument($id, $type)
107
    {
108
        return $this->generateDocument('generate-document', compact('id', 'type'));
109
    }
110
111
    public function generateDocument($action, $params)
112
    {
113
        try {
114
            $data = Purse::perform($action, $params);
115
        } catch (ResponseErrorException $e) {
116
            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...
117
118
            return $this->redirect(['@client/view', 'id' => $params['id']]);
119
        }
120
        $this->asPdf();
121
122
        return $data;
123
    }
124
125
    protected function asPdf()
126
    {
127
        $response = Yii::$app->getResponse();
128
        $response->format = $response::FORMAT_RAW;
129
        $response->getHeaders()->add('content-type', 'application/pdf');
130
    }
131
132
    public function actionPreGenerateDocument($type)
133
    {
134
        $purse = new Purse(['scenario' => 'generate-and-save-monthly-document']);
135
        if ($purse->load(Yii::$app->request->post()) && $purse->validate()) {
136
            return $this->redirect([
137
                '@purse/generate-monthly-document',
138
                'id' => $purse->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\finance\models\Purse>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
                'type' => $type,
140
                'month' => $purse->month,
0 ignored issues
show
Documentation introduced by
The property month does not exist on object<hipanel\modules\finance\models\Purse>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
141
            ]);
142
        }
143
    }
144
}
145