CategoryService::getCurrentMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace app\core\services;
4
5
use app\core\models\Account;
6
use app\core\models\Category;
7
use app\core\types\TransactionType;
8
use Yii;
9
use yii\db\ActiveRecord;
10
use yii\helpers\ArrayHelper;
11
use yii\web\NotFoundHttpException;
12
13
class CategoryService
14
{
15
    public static function getDefaultCategory(int $userId = 0)
16
    {
17
        $userId = $userId ?: Yii::$app->user->id;
18
        return Category::find()
19
            ->where(['user_id' => $userId, 'default' => Category::DEFAULT])
20
            ->orderBy(['id' => SORT_ASC])
21
            ->asArray()
22
            ->one();
23
    }
24
25
    public static function getAdjustCategoryId(int $userId = 0)
26
    {
27
        $userId = $userId ?: Yii::$app->user->id;
28
        return Category::find()
29
            ->where(['user_id' => $userId, 'transaction_type' => TransactionType::ADJUST])
30
            ->orderBy(['id' => SORT_ASC])
31
            ->scalar();
32
    }
33
34
    /**
35
     * @param int $id
36
     * @return Account|ActiveRecord|null
37
     * @throws NotFoundHttpException
38
     */
39
    public static function findCurrentOne(int $id)
40
    {
41
        if (!$model = Category::find()->where(['id' => $id, 'user_id' => \Yii::$app->user->id])->one()) {
42
            throw new NotFoundHttpException('No data found');
43
        }
44
        return $model;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $model also could return the type array which is incompatible with the documented return type app\core\models\Account|null|yii\db\ActiveRecord.
Loading history...
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public static function getCurrentMap()
51
    {
52
        $categories = Category::find()->where(['user_id' => Yii::$app->user->id])->asArray()->all();
53
        return ArrayHelper::map($categories, 'id', 'name');
54
    }
55
}
56