GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( 35624a...012a1d )
by Toby
05:53
created

CompletionConditions/RequiredPositionsFilled.php (28 issues)

1
<?php
2
3
namespace BristolSU\Module\AssignRoles\CompletionConditions;
4
5
use BristolSU\ControlDB\Contracts\Models\Group;
6
use BristolSU\ControlDB\Contracts\Models\Role;
7
use BristolSU\ControlDB\Contracts\Repositories\Position;
8
use BristolSU\ControlDB\Contracts\Repositories\Role as RoleRepository;
9
use BristolSU\Module\AssignRoles\Fields\RequiredPositions;
10
use BristolSU\Module\AssignRoles\Support\LogicRoleRepository;
11
use BristolSU\Module\AssignRoles\Support\RequiredSettingRetrieval;
12
use BristolSU\Module\AssignRoles\Support\SettingRetrievalException;
13
use BristolSU\Support\ActivityInstance\ActivityInstance;
14
use BristolSU\Support\Completion\Contracts\CompletionCondition;
15
use BristolSU\Support\Logic\Contracts\Audience\LogicAudience;
16
use BristolSU\Support\Logic\Contracts\LogicRepository;
17
use BristolSU\Support\Logic\Facade\LogicTester;
18
use BristolSU\Support\ModuleInstance\Contracts\ModuleInstance;
19
use FormSchema\Generator\Field;
20
use FormSchema\Schema\Form;
21
use Illuminate\Support\Collection;
22
23
class RequiredPositionsFilled extends CompletionCondition
24
{
25
26
    /**
27
     * @var RoleRepository
28
     */
29
    private $roleRepository;
0 ignored issues
show
Private member variable "roleRepository" must be prefixed with an underscore
Loading history...
30
31
    public function __construct(string $moduleAlias, RoleRepository $roleRepository)
0 ignored issues
show
Missing doc comment for function __construct()
Loading history...
32
    {
33
        parent::__construct($moduleAlias);
34
        $this->roleRepository = $roleRepository;
35
    }
36
37
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $settings should have a doc-comment as per coding-style.
Loading history...
Parameter $activityInstance should have a doc-comment as per coding-style.
Loading history...
Parameter $moduleInstance should have a doc-comment as per coding-style.
Loading history...
38
     * @inheritDoc
39
     */
40
    public function isComplete($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): bool
41
    {
42
        try {
43
            $remainingPositions = $this->positionsStillToFill($settings, $activityInstance, $moduleInstance);
44
        } catch (SettingRetrievalException $e) {
45
            return false;
46
        }
47
       
48
       return count($remainingPositions) === 0;
0 ignored issues
show
Line indented incorrectly; expected at least 8 spaces, found 7
Loading history...
49
    }
50
    
51
    public function percentage($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): int
52
    {
53
        $group = $this->getGroup($activityInstance);
54
55
        try {
56
            $requiredPositions = $this->getRequiredPositions($settings, $group);
57
            $remainingPositions = $this->positionsStillToFill($settings, $activityInstance, $moduleInstance);
58
        } catch (SettingRetrievalException $e) {
59
            return false;
60
        }
61
        
62
        if(count($requiredPositions) === 0) {
63
            return 100;
64
        }
65
        
66
        $filled = count($requiredPositions) - count($remainingPositions);
67
        
68
        $percentage = (int) round(($filled/count($requiredPositions)) * 100, 0);
69
70
        if($percentage > 100) {
71
            return 100;
72
        }
73
        return $percentage;
74
    }
75
76
    protected function positionsStillToFill($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance)
0 ignored issues
show
Missing doc comment for function positionsStillToFill()
Loading history...
77
    {
78
        $group = $this->getGroup($activityInstance);
79
        $roles = $this->rolesThroughGroup($group, $moduleInstance)->filter(function(Role $role) {
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
80
            return $role->users()->count() > 0;
81
        });
82
        
83
        return collect($this->getRequiredPositions($settings, $group))->filter(function(int $positionId) use ($roles) {
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
84
            return $roles->filter(function(Role $role) use ($positionId) {
85
                return $role->positionId() === $positionId;
86
            })->count() === 0;
87
        });
88
    }
89
    
90
    /**
91
     * @inheritDoc
92
     */
93
    public function options(): Form
94
    {
95
        return \FormSchema\Generator\Form::make()->withField(
96
            Field::make(RequiredPositions::class, 'required')
97
                ->label('Required Positions')->featured(true)->required(true)
98
                ->default([])->hint('Define which positions are required')
99
                ->help('Define the positions that are required for any given logic group')
100
                ->logic($this->getLogic())
101
                ->positions($this->getPositions())
102
        )->getSchema();
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function name(): string
109
    {
110
        return 'Required Positions are filled';
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function description(): string
117
    {
118
        return 'Positions that are marked as required for a group have been filled';
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function alias(): string
125
    {
126
        return 'required_positions_are_filled';
127
    }
128
129
    private function getLogic()
130
    {
131
        try {
132
            return collect(app(LogicRepository::class)->all());
133
        } catch (\Exception $e) {
134
            return collect();
135
        }
136
    }
137
138
    private function getPositions()
139
    {
140
        try {
141
            return collect(app(Position::class)->all());
142
        } catch (\Exception $e) {
143
            return collect();
144
        }
145
    }
146
147
    /**
148
     * Get the group from an activity instance
149
     * 
150
     * @param ActivityInstance $activityInstance
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
151
     * @return Group|\BristolSU\ControlDB\Models\Group
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
152
     */
153
    private function getGroup(ActivityInstance $activityInstance)
0 ignored issues
show
Private method name "RequiredPositionsFilled::getGroup" must be prefixed with an underscore
Loading history...
154
    {
155
        if($activityInstance->resource_type === 'group') {
156
            return $activityInstance->participant();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $activityInstance->participant() also could return the type BristolSU\ControlDB\Mode...U\ControlDB\Models\User which is incompatible with the documented return type BristolSU\ControlDB\Cont...\ControlDB\Models\Group.
Loading history...
157
        }
158
        return $activityInstance->participant()->group();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $activityInstance->participant()->group() also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type BristolSU\ControlDB\Cont...\ControlDB\Models\Group.
Loading history...
159
    }
160
161
    private function getRequiredPositions(array $settings, Group $group)
162
    {
163
        return app(RequiredSettingRetrieval::class)->getSettings($group, $settings);
164
    }
165
166
    private function rolesThroughGroup(Group $group, ModuleInstance $moduleInstance)
0 ignored issues
show
Private method name "RequiredPositionsFilled::rolesThroughGroup" must be prefixed with an underscore
Loading history...
Missing doc comment for function rolesThroughGroup()
Loading history...
167
    {
168
        $roles = $group->roles();
169
        if($this->logicGroupId($moduleInstance) !== null) {
0 ignored issues
show
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
170
            $logicGroup = app(LogicRepository::class)->getById($this->logicGroupId($moduleInstance));
0 ignored issues
show
It seems like $this->logicGroupId($moduleInstance) can also be of type null; however, parameter $id of BristolSU\Support\Logic\...icRepository::getById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

170
            $logicGroup = app(LogicRepository::class)->getById(/** @scrutinizer ignore-type */ $this->logicGroupId($moduleInstance));
Loading history...
171
            return $roles->filter(function(\BristolSU\ControlDB\Contracts\Models\Role $role) use ($moduleInstance, $logicGroup) {
0 ignored issues
show
The import $moduleInstance is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
172
                return LogicTester::evaluate($logicGroup, null, $role->group(), $role);
173
            })->values();
0 ignored issues
show
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
174
        }
175
        return $roles;
176
    }
177
178
    private function logicGroupId(ModuleInstance $moduleInstance)
0 ignored issues
show
Private method name "RequiredPositionsFilled::logicGroupId" must be prefixed with an underscore
Loading history...
Missing doc comment for function logicGroupId()
Loading history...
179
    {
180
        $id = $moduleInstance->setting('logic_group', null);
181
        if($id === null) {
182
            return null;
183
        }
184
        return (int) $id;
185
    }
186
    
187
}