Completed
Push — master ( d3fa24...8301f0 )
by Andrii
12:34
created

PurseController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 63.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 82
ccs 23
cts 36
cp 0.6389
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B actions() 0 35 1
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-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\models\Purse;
20
use hiqdev\hiart\ResponseErrorException;
21
use Yii;
22
23
class PurseController extends \hipanel\base\CrudController
24 1
{
25
    public function actions()
26
    {
27
        return [
28 1
            'index' => [
29 1
                'class' => IndexAction::class,
30
            ],
31 1
            'view' => [
32 1
                'class' => ViewAction::class,
33
            ],
34 1
            'update' => [
35 1
                'class' => SmartUpdateAction::class,
36
            ],
37 1
            'update-contact' => [
38 1
                'class' => SmartUpdateAction::class,
39
            ],
40 1
            'update-requisite' => [
41 1
                'class' => SmartUpdateAction::class,
42
            ],
43 1
            'validate-form' => [
44 1
                'class' => ValidateFormAction::class,
45
            ],
46 1
            'invoice-archive' => [
47 1
                'class' => RedirectAction::class,
48 1
                'error' => Yii::t('hipanel', 'Under construction'),
49
            ],
50 1
            'generate-and-save-monthly-document' => [
51 1
                'class' => SmartPerformAction::class,
52 1
                'success' => Yii::t('hipanel:finance', 'Document updated'),
53
            ],
54 1
            'generate-and-save-document' => [
55 1
                'class' => SmartPerformAction::class,
56 1
                'success' => Yii::t('hipanel:finance', 'Document updated'),
57 1
            ],
58
        ];
59
    }
60
61
    public function actionGenerateMonthlyDocument($id, $type, $month = null)
62
    {
63
        return $this->generateDocument('generate-monthly-document', compact('id', 'type', 'month'));
64
    }
65
66
    public function actionGenerateDocument($id, $type)
67
    {
68
        return $this->generateDocument('generate-document', compact('id', 'type'));
69
    }
70
71
    public function generateDocument($action, $params)
72
    {
73
        try {
74
            $data = Purse::perform($action, $params);
75
        } catch (ResponseErrorException $e) {
76
            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...
77
78
            return $this->redirect(['@client/view', 'id' => $params['id']]);
79
        }
80
        $this->asPdf();
81
82
        return $data;
83
    }
84
85
    protected function asPdf()
86
    {
87
        $response = Yii::$app->getResponse();
88
        $response->format = $response::FORMAT_RAW;
89
        $response->getHeaders()->add('content-type', 'application/pdf');
90
    }
91
92
    public function actionPreGenerateDocument($type)
93
    {
94
        $purse = new Purse();
95
        if ($purse->load(Yii::$app->request->post()) && $purse->validate()) {
96
            return $this->redirect([
97
                '@purse/generate-monthly-document',
98
                '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...
99
                'type' => $type,
100
                '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...
101
            ]);
102
        }
103
    }
104
}
105