HasMembers::registrar()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
/** Created by PhpStorm,  User: jonphipps,  Date: 2017-05-22,  Time: 11:44 AM */
4
5
namespace App\Models\Traits;
6
7
use App\Models\Access\User\User;
8
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
10
trait HasMembers
11
{
12
    /**
13
     * @return BelongsToMany
14
     */
15
    public function members(): ?BelongsToMany
16
    {
17
        //determine the parent class
18
        $class         = static::class;
19
        $classUser     = $class . 'User';
20
        $className     = $this->get_class_name($class);
21
        $foreignKey    = ['Vocabulary'=> 'vocabulary_id', 'Elementset' => 'schema_id', 'Project' => 'agent_id'];
22
23
        return $this->belongsToMany(User::class,
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        return $this->/** @scrutinizer ignore-call */ belongsToMany(User::class,
Loading history...
24
            $classUser::TABLE,
25
            $foreignKey[$className],
26
            'user_id')->withTimestamps()->withPivot('is_maintainer_for',
27
            'is_registrar_for',
28
            'is_admin_for',
29
            'languages',
30
            'default_language',
31
            'current_language');
32
    }
33
34
    /**
35
     * @return mixed
36
     */
37
    public function registrar()
38
    {
39
        return $this->members()->where('is_registrar_for', true);
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45
    public function administrators()
46
    {
47
        return $this->members()->where('is_admin_for', true);
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53
    public function languageMaintainers()
54
    {
55
        return $this->members()->where('is_maintainer_for', true)->whereNotNull('languages');
56
    }
57
58
    /**
59
     * @return mixed
60
     */
61
    public function maintainersForLanguage($language)
62
    {
63
        return $this->members()
64
            ->where('is_maintainer_for', true)
65
            ->whereNotNull('languages')
66
            ->whereIn('languages', $language);
67
    }
68
69
    /**
70
     * @return mixed
71
     */
72
    public function maintainers()
73
    {
74
        return $this->members()->where('is_maintainer_for', true)->whereNull('languages');
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    public function viewers()
81
    {
82
        return $this->members()->whereNull('is_admin_for', true)->whereNull('is_maintainer_for');
83
    }
84
85
    private function get_class_name($classname)
86
    {
87
        if ($pos = strrpos($classname, '\\')) {
88
            return substr($classname, $pos + 1);
89
        }
90
91
        return $pos;
92
    }
93
}
94