Passed
Push — dev6 ( f89a9a...d87223 )
by Ron
19:23
created

TechTipTrait::removeFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\Traits;
4
5
use App\Models\TechTipFile;
6
use App\Models\TechTipEquipment;
7
8
trait TechTipTrait
9
{
10
    use FileTrait;
11
12
    /**
13
     * Add equipment types to a Tech Tip
14
     */
15
    protected function addEquipment($tipId, $equipList)
16
    {
17
        foreach($equipList as $equip)
18
        {
19
            TechTipEquipment::create([
20
                'tip_id'   => $tipId,
21
                'equip_id' => $equip['equip_id'],
22
            ]);
23
        }
24
    }
25
26
    /**
27
     * Find what equipment has been added or removed to the Tech Tip
28
     */
29
    protected function processUpdatedEquipment($tipId, $equipList)
30
    {
31
        $current = TechTipEquipment::where('tip_id', $tipId)->get();
32
        $new     = [];
33
34
        //  Determine if the equipment is new or existing
35
        foreach($equipList as $equip)
36
        {
37
            //  If the laravel_through_key value exists, then it was an existing equipment that has stayed in place
38
            if(isset($equip['laravel_through_key']))
39
            {
40
                //  Remove that piece from the current equipment list so it is not updated later
41
                $current = $current->filter(function($i) use ($equip)
42
                {
43
                    return $i->equip_id != $equip['equip_id'];
44
                });
45
            }
46
            else
47
            {
48
                $new[] = $equip;
49
            }
50
        }
51
52
        $this->processRemovedEquipment($current);
53
        $this->addEquipment($tipId, $new);
54
    }
55
56
    /**
57
     * Remove any equipment that is no longer associated with the Tech Tip
58
     */
59
    protected function processRemovedEquipment($removeList)
60
    {
61
        foreach($removeList as $equip)
62
        {
63
            TechTipEquipment::find($equip->tip_equip_id)->delete();
64
        }
65
    }
66
67
    /**
68
     * Add files to the Tech Tip
69
     */
70
    protected function processNewFiles($tipId, $move = false)
71
    {
72
        $fileData = session()->pull('new-file-upload');
73
        if($fileData)
74
        {
75
            foreach($fileData as $file)
76
            {
77
                if($move)
78
                {
79
                    $this->moveStoredFile($file->file_id, $tipId);
80
                }
81
                TechTipFile::create([
82
                    'tip_id'  => $tipId,
83
                    'file_id' => $file->file_id,
84
                ]);
85
            }
86
        }
87
    }
88
89
    /**
90
     * Remove files from the Tech Tip
91
     */
92
    protected function removeFiles($tipId, $fileList)
93
    {
94
        foreach($fileList as $file)
95
        {
96
            TechTipFile::where('tip_id', $tipId)->where('file_id', $file)->first()->delete();
97
            $this->deleteFile($file);
98
        }
99
    }
100
}
101