TransactionController::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\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