Passed
Push — master ( 37b02e...ebbbe1 )
by James
08:59
created

app/Models/Category.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Category.php
4
 * Copyright (c) 2017 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace FireflyIII\Models;
24
25
use Crypt;
26
use Illuminate\Database\Eloquent\Model;
27
use Illuminate\Database\Eloquent\Relations\BelongsTo;
28
use Illuminate\Database\Eloquent\SoftDeletes;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30
use Watson\Validating\ValidatingTrait;
31
32
/**
33
 * Class Category.
34
 */
35
class Category extends Model
36
{
37
    use SoftDeletes, ValidatingTrait;
0 ignored issues
show
The trait Watson\Validating\ValidatingTrait requires the property $validationMessages which is not provided by FireflyIII\Models\Category.
Loading history...
38
39
    /**
40
     * The attributes that should be casted to native types.
41
     *
42
     * @var array
43
     */
44
    protected $casts
45
        = [
46
            'created_at' => 'datetime',
47
            'updated_at' => 'datetime',
48
            'deleted_at' => 'datetime',
49
            'encrypted'  => 'boolean',
50
        ];
51
    /** @var array */
52
    protected $fillable = ['user_id', 'name'];
53
    /** @var array */
54
    protected $hidden = ['encrypted'];
55
    /** @var array */
56
    protected $rules = ['name' => 'required|between:1,200'];
57
58
    /**
59
     * @param array $fields
60
     *
61
     * @return Category
62
     */
63
    public static function firstOrCreateEncrypted(array $fields)
64
    {
65
        // everything but the name:
66
        $query  = self::orderBy('id');
67
        $search = $fields;
68
        unset($search['name']);
69
        foreach ($search as $name => $value) {
70
            $query->where($name, $value);
71
        }
72
        $set = $query->get(['categories.*']);
73
        /** @var Category $category */
74
        foreach ($set as $category) {
75
            if ($category->name === $fields['name']) {
76
                return $category;
77
            }
78
        }
79
        // create it!
80
        $category = self::create($fields);
81
82
        return $category;
83
    }
84
85
    /**
86
     * @param string $value
87
     *
88
     * @return Category
89
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
90
     */
91
    public static function routeBinder(string $value): Category
92
    {
93
        if (auth()->check()) {
94
            $categoryId = intval($value);
95
            $category   = auth()->user()->categories()->find($categoryId);
96
            if (!is_null($category)) {
97
                return $category;
98
            }
99
        }
100
        throw new NotFoundHttpException;
101
    }
102
103
    /**
104
     * @codeCoverageIgnore
105
     *
106
     * @param $value
107
     *
108
     * @return string
109
     * @throws \Illuminate\Contracts\Encryption\DecryptException
110
     */
111
    public function getNameAttribute($value)
112
    {
113
        if ($this->encrypted) {
114
            return Crypt::decrypt($value);
115
        }
116
117
        return $value;
118
    }
119
120
    /**
121
     * @codeCoverageIgnore
122
     *
123
     * @param $value
124
     *
125
     * @throws \Illuminate\Contracts\Encryption\EncryptException
126
     */
127
    public function setNameAttribute($value)
128
    {
129
        $encrypt                       = config('firefly.encryption');
130
        $this->attributes['name']      = $encrypt ? Crypt::encrypt($value) : $value;
131
        $this->attributes['encrypted'] = $encrypt;
132
    }
133
134
    /**
135
     * @codeCoverageIgnore
136
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
137
     */
138
    public function transactionJournals()
139
    {
140
        return $this->belongsToMany('FireflyIII\Models\TransactionJournal', 'category_transaction_journal', 'category_id');
141
    }
142
143
    /**
144
     * @codeCoverageIgnore
145
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
146
     */
147
    public function transactions()
148
    {
149
        return $this->belongsToMany('FireflyIII\Models\Transaction', 'category_transaction', 'category_id');
150
    }
151
152
    /**
153
     * @codeCoverageIgnore
154
     * @return BelongsTo
155
     */
156
    public function user(): BelongsTo
157
    {
158
        return $this->belongsTo('FireflyIII\User');
159
    }
160
}
161