Completed
Push — master ( 0984d0...c9941e )
by Shawn
02:33
created

DutyList   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 77
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A scheduledUpdate() 0 7 1
A processSwapRequest() 0 15 4
A processedList() 0 8 2
A createDutySwap() 0 9 1
A emailOutput() 0 6 1
A htmlOutput() 0 4 1
1
<?php
2
3
namespace SET\Handlers\Duty;
4
5
use SET\Duty;
6
use SET\DutySwap;
7
8
class DutyList
9
{
10
    private $duty;
11
12
    public function __construct($duty)
13
    {
14
        if (is_int($duty)) {
15
            $duty = Duty::findOrFail($duty);
16
        }
17
        $this->duty = $duty;
18
    }
19
20
    public function htmlOutput()
21
    {
22
        return $this->processedList()->htmlOutput();
23
    }
24
25
    public function scheduledUpdate()
26
    {
27
        //build the list and write into the database the next record.
28
        $this->processedList()->iterateList();
29
        //build the list again (with the new record on top) and generate our email output.
30
        return $this->processedList()->emailOutput();
31
    }
32
33
    public function emailOutput()
34
    {
35
        $userGroup = $this->processedList();
36
37
        return $userGroup->emailOutput();
38
    }
39
40
    /**
41
     * @param array  $dateArray
42
     * @param array  $IDArray
43
     * @param string $type
44
     */
45
    public function processSwapRequest(array $dateArray, array $IDArray, $type)
46
    {
47
        for ($i = 0; $i < 2; $i++) {
48
            $futureSwap = DutySwap::where('date', $dateArray[$i])
49
                ->where('duty_id', $this->duty->id)->first();
50
            $flippedI = ($i == 0 ? 1 : 0);
51
52
            if (is_null($futureSwap)) {
53
                $this->createDutySwap($dateArray[$i], $IDArray[$flippedI], $type);
54
            } else {
55
                $futureSwap->imageable_id = $IDArray[$flippedI];
56
                $futureSwap->save();
57
            }
58
        }
59
    }
60
61
    private function processedList()
62
    {
63
        if ($this->duty->has_groups) {
64
            return new DutyGroups($this->duty);
65
        } else {
66
            return new DutyUsers($this->duty);
67
        }
68
    }
69
70
    /**
71
     * @param $date
72
     * @param $ID
73
     * @param $type
74
     */
75
    private function createDutySwap($date, $ID, $type)
76
    {
77
        DutySwap::create([
78
            'imageable_id'   => $ID,
79
            'imageable_type' => $type,
80
            'duty_id'        => $this->duty->id,
81
            'date'           => $date,
82
        ]);
83
    }
84
}
85