TransactionController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 28
c 2
b 1
f 0
dl 0
loc 72
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A actionUpload() 0 12 1
A actionExport() 0 3 1
A actionCreateByDescription() 0 8 1
A actionTypes() 0 10 2
A actions() 0 5 1
1
<?php
2
3
namespace app\modules\v1\controllers;
4
5
use app\core\exceptions\InvalidArgumentException;
6
use app\core\models\Transaction;
7
use app\core\requests\TransactionCreateByDescRequest;
8
use app\core\requests\TransactionUploadRequest;
9
use app\core\traits\ServiceTrait;
10
use app\core\types\TransactionType;
11
use Yii;
12
use yii\web\UploadedFile;
13
14
/**
15
 * Transaction controller for the `v1` module
16
 */
17
class TransactionController extends ActiveController
18
{
19
    use ServiceTrait;
20
21
    public $modelClass = Transaction::class;
22
    public $noAuthActions = [];
23
24
    public function actions()
25
    {
26
        $actions = parent::actions();
27
        unset($actions['index'], $actions['delete']);
28
        return $actions;
29
    }
30
31
    /**
32
     * @return Transaction
33
     * @throws \Exception|\Throwable
34
     */
35
    public function actionCreateByDescription()
36
    {
37
        $params = Yii::$app->request->bodyParams;
38
        $model = new TransactionCreateByDescRequest();
39
        /** @var TransactionCreateByDescRequest $model */
40
        $model = $this->validate($model, $params);
41
42
        return $this->transactionService->createByDesc($model->description);
43
    }
44
45
    /**
46
     * @return array
47
     * @throws \Exception
48
     */
49
    public function actionTypes()
50
    {
51
        $items = [];
52
        $texts = TransactionType::texts();
53
        $names = TransactionType::names();
54
        // unset($names[TransactionType::ADJUST]);
55
        foreach ($names as $key => $name) {
56
            $items[] = ['type' => $name, 'name' => data_get($texts, $key)];
57
        }
58
        return $items;
59
    }
60
61
62
    /**
63
     * @return array
64
     * @throws InvalidArgumentException
65
     * @throws \Exception
66
     */
67
    public function actionUpload()
68
    {
69
        $fileParam = 'file';
70
        $uploadedFile = UploadedFile::getInstanceByName($fileParam);
71
        $params = [$fileParam => $uploadedFile];
72
        $model = new TransactionUploadRequest();
73
        $this->validate($model, $params);
74
        $filename = Yii::$app->user->id . 'record.csv';
75
        $this->uploadService->uploadRecord($uploadedFile, $filename);
76
        $data = $this->transactionService->createByCSV($filename);
77
        $this->uploadService->deleteLocalFile($filename);
78
        return $data;
79
    }
80
81
82
    /**
83
     * @return array
84
     * @throws \Exception
85
     */
86
    public function actionExport()
87
    {
88
        return $this->transactionService->exportData();
89
    }
90
}
91