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

app/Models/Budget.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Budget.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 Budget.
34
 */
35
class Budget 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\Budget.
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
            'active'     => 'boolean',
50
            'encrypted'  => 'boolean',
51
        ];
52
    /** @var array */
53
    protected $fillable = ['user_id', 'name', 'active'];
54
    /** @var array */
55
    protected $hidden = ['encrypted'];
56
    /** @var array */
57
    protected $rules = ['name' => 'required|between:1,200'];
58
59
    /**
60
     * @param array $fields
61
     *
62
     * @return Budget
63
     */
64
    public static function firstOrCreateEncrypted(array $fields)
65
    {
66
        // everything but the name:
67
        $query  = self::orderBy('id');
68
        $search = $fields;
69
        unset($search['name']);
70
        foreach ($search as $name => $value) {
71
            $query->where($name, $value);
72
        }
73
        $set = $query->get(['budgets.*']);
74
        /** @var Budget $budget */
75
        foreach ($set as $budget) {
76
            if ($budget->name === $fields['name']) {
77
                return $budget;
78
            }
79
        }
80
        // create it!
81
        $budget = self::create($fields);
82
83
        return $budget;
84
    }
85
86
    /**
87
     * @param string $value
88
     *
89
     * @return Budget
90
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
91
     */
92
    public static function routeBinder(string $value): Budget
93
    {
94
        if (auth()->check()) {
95
            $budgetId = intval($value);
96
            $budget   = auth()->user()->budgets()->find($budgetId);
97
            if (!is_null($budget)) {
98
                return $budget;
99
            }
100
        }
101
        throw new NotFoundHttpException;
102
    }
103
104
    /**
105
     * @codeCoverageIgnore
106
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
107
     */
108
    public function budgetlimits()
109
    {
110
        return $this->hasMany('FireflyIII\Models\BudgetLimit');
111
    }
112
113
    /**
114
     * @codeCoverageIgnore
115
     *
116
     * @param $value
117
     *
118
     * @return string
119
     * @throws \Illuminate\Contracts\Encryption\DecryptException
120
     */
121
    public function getNameAttribute($value)
122
    {
123
        if ($this->encrypted) {
124
            return Crypt::decrypt($value);
125
        }
126
127
        return $value;
128
    }
129
130
    /**
131
     * @codeCoverageIgnore
132
     *
133
     * @param $value
134
     *
135
     * @throws \Illuminate\Contracts\Encryption\EncryptException
136
     */
137
    public function setNameAttribute($value)
138
    {
139
        $encrypt                       = config('firefly.encryption');
140
        $this->attributes['name']      = $encrypt ? Crypt::encrypt($value) : $value;
141
        $this->attributes['encrypted'] = $encrypt;
142
    }
143
144
    /**
145
     * @codeCoverageIgnore
146
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
147
     */
148
    public function transactionJournals()
149
    {
150
        return $this->belongsToMany('FireflyIII\Models\TransactionJournal', 'budget_transaction_journal', 'budget_id');
151
    }
152
153
    /**
154
     * @codeCoverageIgnore
155
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
156
     */
157
    public function transactions()
158
    {
159
        return $this->belongsToMany('FireflyIII\Models\Transaction', 'budget_transaction', 'budget_id');
160
    }
161
162
    /**
163
     * @codeCoverageIgnore
164
     * @return BelongsTo
165
     */
166
    public function user(): BelongsTo
167
    {
168
        return $this->belongsTo('FireflyIII\User');
169
    }
170
}
171