Passed
Pull Request — master (#53)
by
unknown
05:24
created

Category   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 54.24%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 58
c 3
b 1
f 0
dl 0
loc 129
ccs 32
cts 59
cp 0.5424
rs 10
wmc 27

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAgeMinAndMax() 0 8 2
A hasGradeMinAndMax() 0 8 2
A buildName() 0 17 3
B getGradeString() 0 17 8
A getCategorieslabelByRule() 0 13 3
B getAgeString() 0 25 8
A scopeIsTeam() 0 3 1
1
<?php
2
3
namespace App;
4
5
use Illuminate\Support\Facades\App;
6
use Illuminate\Support\Facades\Auth;
7
8
class Category extends \Xoco70\LaravelTournaments\Models\Category
9
{
10
    const AGE_CUSTOM = 5;
11
12
    /**
13
     * Get All Presets for Categories
14
     * @param null $ruleId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $ruleId is correct as it would always require null to be passed?
Loading history...
15
     * @return array
16
     */
17 3
    public function getCategorieslabelByRule($ruleId = null)
18
    {
19 3
        $result = [];
20
21 3
        if ($ruleId == null) {
0 ignored issues
show
introduced by
The condition $ruleId == null is always true.
Loading history...
22 3
            $rules = ['ikf', 'ekf', 'lakc'];
23 3
            foreach ($rules as $rule) {
24 3
                $result[] = static::whereIn('id', array_keys(config('options.' . $rule . '_settings')))
25 3
                    ->select('name')->get()
26 3
                    ->map->name->toArray();
27
            }
28
        }
29 3
        return $result;
30
    }
31
32
    public function scopeIsTeam($query)
33
    {
34
        return $query->where('isTeam', 1);
35
    }
36
37 2
    public function buildName()
38
    {
39
40 2
        if (Auth::check()) {
41 2
            App::setLocale(Auth::user()->locale);
42
        }
43
        $genders = [
44 2
            'M' => trans('categories.male'),
45 2
            'F' => trans('categories.female'),
46 2
            'X' => trans('categories.mixt')
47
        ];
48
49 2
        $teamText = $this->isTeam() ? trans_choice('core.team', 1) : trans('categories.single');
50 2
        $ageCategoryText = $this->getAgeString();
51 2
        $gradeText = $this->getGradeString();
52
53 2
        return $teamText . ' ' . $genders[$this->gender] . ' ' . $ageCategoryText . ' ' . $gradeText;
54
    }
55
56
    /**
57
     * Build Age String
58
     * @return string
59
     */
60 2
    public function getAgeString()
61
    {
62
        $ageCategories = [
63 2
            0 => trans('categories.no_age_restriction'),
64 2
            1 => trans('categories.children'),
65 2
            2 => trans('categories.students'),
66 2
            3 => trans('categories.adults'),
67 2
            4 => trans('categories.masters'),
68 2
            5 => trans('categories.custom')
69
        ];
70
71 2
        if ($this->ageCategory == self::AGE_CUSTOM) {
72
            $ageCategoryText = ' - ' . trans('categories.age') . ' : ';
73
            if ($this->ageMin != 0 && $this->ageMax != 0) {
74
                return $this->hasAgeMinAndMax($ageCategoryText);
75
            }
76
            if ($this->ageMin == 0 && $this->ageMax != 0) {
77
                return $ageCategoryText .= ' < ' . $this->ageMax . ' ' . trans('categories.years');
78
            }
79
            if ($this->ageMin != 0 && $this->ageMax == 0) {
80
                return $ageCategoryText .= ' > ' . $this->ageMin . ' ' . trans('categories.years');
81
            }
82
            return '';
83
        }
84 2
        return $ageCategories[$this->ageCategory];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ageCategories[$this->ageCategory] also could return the type Illuminate\Contracts\Translation\Translator|array which is incompatible with the documented return type string.
Loading history...
85
    }
86
87
    /**
88
     * @param $ageCategoryText
89
     * @return string
90
     */
91
    protected function hasAgeMinAndMax($ageCategoryText): string
92
    {
93
        if ($this->ageMin == $this->ageMax) {
94
            $ageCategoryText .= $this->ageMax . ' ' . trans('categories.years');
95
        } else {
96
            $ageCategoryText .= $this->ageMin . ' - ' . $this->ageMax . ' ' . trans('categories.years');
97
        }
98
        return $ageCategoryText;
99
    }
100
101
    /**
102
     * Build Grade String
103
     * @return string
104
     */
105 2
    public function getGradeString()
106
    {
107
108 2
        $grades = Grade::getAll();
109 2
        if ($this->gradeCategory == 3) {  // Custom
0 ignored issues
show
Bug Best Practice introduced by
The property gradeCategory does not exist on App\Category. Since you implemented __get, consider adding a @property annotation.
Loading history...
110
            $gradeText = ' - ' . trans('core.grade') . ' : ';
111
            if ($this->gradeMin != 0 && $this->gradeMax != 0) {
112
                return $this->hasGradeMinAndMax($grades, $gradeText);
113
            }
114
            if ($this->gradeMin == 0 && $this->gradeMax != 0) {
115
                return $gradeText . ' < ' . $grades[$this->gradeMax - 1]->name;
116
            }
117
            if ($this->gradeMin != 0 && $this->gradeMax == 0) {
118
                return $gradeText . ' > ' . $grades[$this->gradeMin - 1]->name;
119
            }
120
        }
121 2
        return '';
122
    }
123
124
    /**
125
     * @param $grades
126
     * @param $gradeText
127
     * @return string
128
     */
129
    protected function hasGradeMinAndMax($grades, $gradeText): string
130
    {
131
        if ($this->gradeMin == $this->gradeMax) {
132
            $gradeText .= $grades[$this->gradeMin - 1]->name;
133
        } else {
134
            $gradeText .= $grades[$this->gradeMin - 1]->name . ' - ' . $grades[$this->gradeMax - 1]->name;
135
        }
136
        return $gradeText;
137
    }
138
}
139