ProjectUser::GetAuthorizedAsString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Models;
4
5
use App\Helpers\Macros\Traits\Languages;
6
use App\Models\Traits\BelongsToProject;
7
use App\Models\Traits\BelongsToUser;
8
use Backpack\CRUD\CrudTrait;
9
use Culpa\Traits\Blameable;
10
use Culpa\Traits\CreatedBy;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Database\Eloquent\SoftDeletes;
13
14
/**
15
 * App\Models\ProjectUser
16
 *
17
 * @property int $id
18
 * @property \Carbon\Carbon|null $created_at
19
 * @property \Carbon\Carbon|null $updated_at
20
 * @property \Carbon\Carbon|null $deleted_at
21
 * @property int $user_id
22
 * @property int $agent_id
23
 * @property bool $is_registrar_for
24
 * @property bool $is_admin_for
25
 * @property bool $is_maintainer_for
26
 * @property mixed $languages
27
 * @property string $default_language
28
 * @property string $current_language
29
 * @property int $authorized_as
30
 * @property-read \App\Models\Access\User\User $creator
31
 * @property-read mixed $project_id
32
 * @property-read \App\Models\Project $project
33
 * @property-read \App\Models\Access\User\User $user
34
 * @method static bool|null forceDelete()
35
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ProjectUser onlyTrashed()
36
 * @method static bool|null restore()
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereAgentId($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereAuthorizedAs($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereCreatedAt($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereCurrentLanguage($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereDefaultLanguage($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereDeletedAt($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereId($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereIsAdminFor($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereIsMaintainerFor($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereIsRegistrarFor($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereLanguages($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereUpdatedAt($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProjectUser whereUserId($value)
50
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ProjectUser withTrashed()
51
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ProjectUser withoutTrashed()
52
 * @mixin \Eloquent
53
 */
54
class ProjectUser extends Model
55
{
56
    public const TABLE                    = 'reg_agent_has_user';
57
    public const AUTH_ADMIN               = 0;
58
    public const AUTH_MAINTAINER          = 1;
59
    public const AUTH_LANGUAGE_MAINTAINER = 2;
60
    public const AUTH_VIEWER              = 3;
61
62
    public static $rules = [
63
        'updated_at'       => 'required|',
64
        'user_id'          => 'required|',
65
        'agent_id'         => 'required|',
66
    ];
67
    use CrudTrait, SoftDeletes, Blameable, CreatedBy;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\CrudTrait requires some properties which are not provided by App\Models\ProjectUser: $Type, $fakeColumns
Loading history...
68
    use Languages;
69
    use BelongsToProject, BelongsToUser;
70
    protected $table     = self::TABLE;
71
    protected $blameable = [
72
        'created' => 'user_id',
73
    ];
74
    protected $dates   = ['deleted_at'];
75
    protected $guarded = ['id'];
76
    protected $casts   = [
77
        'id'                => 'integer',
78
        'user_id'           => 'integer',
79
        'agent_id'          => 'integer',
80
        'is_registrar_for'  => 'boolean',
81
        'is_maintainer_for' => 'boolean',
82
        'is_admin_for'      => 'boolean',
83
        'languages'         => 'text',
84
        'default_language'  => 'string',
85
        'current_language'  => 'string',
86
        'authorized_as'     => 'integer',
87
    ];
88
89
    /*
90
    |--------------------------------------------------------------------------
91
    | FUNCTIONS
92
    |--------------------------------------------------------------------------
93
    */
94
95
    /**
96
     * @param string $language
97
     * @param bool   $strict
98
     *
99
     * @return bool
100
     */
101
    public function isMaintainerForLanguage($language, $strict = false): ?bool
102
    {
103
        /** @noinspection UnserializeExploitsInspection */
104
        $languages = unserialize($this->languages);
105
        if ($strict) {
106
            if ($languages) {
107
                return \in_array($language, $languages, false);
108
            }
109
        } else { //will also return true if $languages is empty
110
            return  ! $languages || \in_array($language, $languages, false);
111
        }
112
    }
113
114
    public static function GetAuthorizedAsString($authId)
115
    {
116
        $authArray = [
117
            self::AUTH_VIEWER              => 'Viewer',
118
            self::AUTH_LANGUAGE_MAINTAINER => 'Language Maintainer',
119
            self::AUTH_MAINTAINER          => 'Project Maintainer',
120
            self::AUTH_ADMIN               => 'Project Administrator',
121
        ];
122
123
        return $authArray[$authId];
124
    }
125
126
    /*
127
    |--------------------------------------------------------------------------
128
    | RELATIONS
129
    |--------------------------------------------------------------------------
130
    */
131
132
    /*
133
    |--------------------------------------------------------------------------
134
    | SCOPES
135
    |--------------------------------------------------------------------------
136
    */
137
138
    /*
139
    |--------------------------------------------------------------------------
140
    | ACCESSORS
141
    |--------------------------------------------------------------------------
142
    */
143
144
    /*
145
    |--------------------------------------------------------------------------
146
    | MUTATORS
147
    |--------------------------------------------------------------------------
148
    */
149
150
    public function setAuthorizedAsAttribute($value)
151
    {
152
        if ($value === self::AUTH_ADMIN) { //project admin
153
            $this->attributes['is_admin_for']      = 1;
154
            $this->attributes['is_maintainer_for'] = 1;
155
        }
156
        if ($value === self::AUTH_LANGUAGE_MAINTAINER || $value === self::AUTH_MAINTAINER) { //project maintainer
157
            $this->attributes['is_admin_for']      = 0;
158
            $this->attributes['is_maintainer_for'] = 1;
159
        }
160
        if ($value === self::AUTH_VIEWER) { //project member
161
            $this->attributes['is_admin_for']      = 0;
162
            $this->attributes['is_maintainer_for'] = 0;
163
        }
164
        $this->attributes['authorized_as'] = $value;
165
    }
166
}
167