Passed
Push — feature/job-status-transitions ( 688dc7...931d64 )
by Tristan
11:00 queued 06:03
created

JobStatusTransitions::stateMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\Services;
4
5
use App\Models\User;
6
7
class JobStatusTransitions
8
{
9
    protected $transition_graph = [
10
        'states' => [
11
            [
12
                'name' => 'draft',
13
                'metadata' => ['owner' => 'manager']
14
            ],
15
            [
16
                'name' => 'review_manager',
17
                'metadata' => ['owner' => 'manager']
18
            ],
19
            [
20
                'name' => 'review_hr',
21
                'metadata' => ['owner' => 'hr']
22
            ],
23
            [
24
                'name' => 'translation',
25
                'metadata' => ['owner' => 'admin']
26
            ],
27
            [
28
                'name' => 'final_review_manager',
29
                'metadata' => ['owner' => 'manager']
30
            ],
31
            [
32
                'name' => 'final_review_hr',
33
                'metadata' => ['owner' => 'hr']
34
            ],
35
            [
36
                'name' => 'pending_approval',
37
                'metadata' => ['owner' => 'hr']
38
            ],
39
            [
40
                'name' => 'approved',
41
                'metadata' => ['owner' => 'admin']
42
            ],
43
            [
44
                'name' => 'published',
45
                'metadata' => ['owner' => 'admin']
46
            ],
47
            [
48
                'name' => 'completed',
49
                'metadata' => ['owner' => 'admin']
50
            ]
51
        ],
52
        'transitions' => [
53
            'send_to_hr' => [
54
                'from' => ['draft', 'review_manager'],
55
                'to' => 'review_hr'
56
            ],
57
            'send_to_manager' => [
58
                'from' => ['review_hr'],
59
                'to' => 'review_manager'
60
            ],
61
            'send_to_translation' => [
62
                'from' => ['review_hr', 'final_review_hr', 'pending_approval'],
63
                'to' => 'translation'
64
            ],
65
            'send_to_manager_final' => [
66
                'from' => ['translation', 'final_review_hr', 'pending_approval'],
67
                'to' => 'final_review_manager'
68
            ],
69
            'send_to_hr_final' => [
70
                'from' => ['final_review_manager'],
71
                'to' => 'final_review_hr',
72
            ],
73
            'submit_for_approval' => [
74
                'from' => ['final_review_manager'],
75
                'to' => 'pending_approval'
76
            ],
77
            'approve' => [
78
                'from' => ['pending_approval'],
79
                'to' => 'approved'
80
            ],
81
            'publish' => [
82
                'from' => ['approved'],
83
                'to' => 'published'
84
            ],
85
            'complete' => [
86
                'from' => ['published'],
87
                'to' => 'completed'
88
            ]
89
        ]
90
    ];
91
92
    public function states()
93
    {
94
        return collect($this->transition_graph['states'])->pluck('name')->all();
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

94
        return /** @scrutinizer ignore-call */ collect($this->transition_graph['states'])->pluck('name')->all();
Loading history...
95
    }
96
97
    public function stateMetadata(string $state)
98
    {
99
        $graph = $this->transition_graph;
100
        $stateObj = collect($graph['states'])->firstWhere('name', $state);
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

100
        $stateObj = /** @scrutinizer ignore-call */ collect($graph['states'])->firstWhere('name', $state);
Loading history...
101
        return $stateObj !== null
102
            ? $stateObj['metadata']
103
            : [];
104
    }
105
106
    public function isLegalTransition(string $from, string $to): bool
107
    {
108
        $graph = $this->transition_graph;
109
        $transitions = collect($graph['transitions']);
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

109
        $transitions = /** @scrutinizer ignore-call */ collect($graph['transitions']);
Loading history...
110
        return $transitions->some(function ($transition) use ($from, $to) {
111
            return $transition['to'] === $to && collect($transition['from'])->contains($from);
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

111
            return $transition['to'] === $to && /** @scrutinizer ignore-call */ collect($transition['from'])->contains($from);
Loading history...
112
        });
113
    }
114
115
    public function legalDestinations(string $from)
116
    {
117
        return collect($this->transition_graph['transitions'])
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

117
        return /** @scrutinizer ignore-call */ collect($this->transition_graph['transitions'])
Loading history...
118
            ->filter(function ($transition) use ($from) {
119
                return in_array($from, $transition['from']);
120
            })
121
            ->pluck('to')
122
            ->unique()
123
            ->all();
124
    }
125
126
    public function userOwnsState(User $user, string $state): bool
127
    {
128
        $owner = $this->stateMetadata($state)['owner'];
129
        return $user->isAdmin()
130
            || $owner === 'manager' && $user->isManager()
131
            || $owner === 'hr' && $user->isHrAdvisor()
132
            || $owner === 'admin' && $user->isAdmin();
133
    }
134
135
    public function canTransition(User $user, string $from, string $to): bool
136
    {
137
        return $this->userOwnsState($user, $from) && $this->isLegalTransition($from, $to);
138
    }
139
}
140