RecordController::actions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace app\modules\v1\controllers;
4
5
use app\core\exceptions\InternalException;
6
use app\core\exceptions\InvalidArgumentException;
7
use app\core\models\Record;
8
use app\core\traits\ServiceTrait;
9
use app\core\types\RecordSource;
10
use app\core\types\TransactionType;
11
use Exception;
12
use Yii;
13
use yii\base\InvalidConfigException;
14
use yii\data\ActiveDataProvider;
15
16
/**
17
 * Record controller for the `v1` module
18
 */
19
class RecordController extends ActiveController
20
{
21
    use ServiceTrait;
22
23
    public $modelClass = Record::class;
24
    public $noAuthActions = [];
25
    public $partialMatchAttributes = ['name'];
26
    public $defaultOrder = ['date' => SORT_DESC, 'id' => SORT_DESC];
27
    public $stringToIntAttributes = ['transaction_type' => TransactionType::class];
28
    public $relations = ['transaction' => []];
29
30
    public function actions()
31
    {
32
        $actions = parent::actions();
33
        unset($actions['create'], $actions['update']);
34
        return $actions;
35
    }
36
37
38
    /**
39
     * @return ActiveDataProvider
40
     * @throws InvalidArgumentException
41
     * @throws InvalidConfigException
42
     * @throws InternalException
43
     */
44
    public function prepareDataProvider()
45
    {
46
        $dataProvider = parent::prepareDataProvider();
47
        if ($searchKeywords = trim(request('keyword'))) {
48
            $dataProvider->query->andWhere(
0 ignored issues
show
Bug introduced by
The method andWhere() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            $dataProvider->query->/** @scrutinizer ignore-call */ 
49
                                  andWhere(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
                "MATCH(`description`, `tags`, `remark`) AGAINST ('*$searchKeywords*' IN BOOLEAN MODE)"
0 ignored issues
show
Bug introduced by
'MATCH(`description`, `t...s.'*' IN BOOLEAN MODE)' of type string is incompatible with the type array expected by parameter $condition of yii\db\QueryInterface::andWhere(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
                /** @scrutinizer ignore-type */ "MATCH(`description`, `tags`, `remark`) AGAINST ('*$searchKeywords*' IN BOOLEAN MODE)"
Loading history...
50
            );
51
        }
52
        $dataProvider->setModels($this->transactionService->formatRecords($dataProvider->getModels()));
53
        return $dataProvider;
54
    }
55
56
    /**
57
     * @param array $params
58
     * @return array
59
     * @throws Exception
60
     */
61
    protected function formatParams(array $params)
62
    {
63
        if (($date = explode('~', data_get($params, 'date'))) && count($date) == 2) {
0 ignored issues
show
Bug introduced by
It seems like data_get($params, 'date') can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        if (($date = explode('~', /** @scrutinizer ignore-type */ data_get($params, 'date'))) && count($date) == 2) {
Loading history...
64
            $start = $date[0] . ' 00:00:00';
65
            $end = $date[1] . ' 23:59:59';
66
            $params['date'] = "{$start}~{$end}";
67
        }
68
        return $params;
69
    }
70
71
    /**
72
     * @return array
73
     * @throws Exception
74
     */
75
    public function actionOverview()
76
    {
77
        return array_values($this->analysisService->recordOverview);
78
    }
79
80
81
    /**
82
     * @return array
83
     * @throws InvalidArgumentException
84
     * @throws Exception
85
     */
86
    public function actionAnalysis()
87
    {
88
        $transactionType = request('transaction_type', TransactionType::getName(TransactionType::EXPENSE));
89
        $date = request('date', Yii::$app->formatter->asDatetime('now'));
90
91
        return $this->analysisService->getRecordStatisticalData(
92
            $date,
93
            TransactionType::toEnumValue($transactionType)
94
        );
95
    }
96
97
    /**
98
     * @return array
99
     * @throws Exception
100
     */
101
    public function actionSources()
102
    {
103
        $items = [];
104
        $names = RecordSource::names();
105
        foreach ($names as $key => $name) {
106
            $items[] = ['type' => $key, 'name' => $name];
107
        }
108
        return $items;
109
    }
110
}
111