Issues (2963)

app/Observers/DeviceObserver.php (1 issue)

1
<?php
2
3
namespace App\Observers;
4
5
use App\Models\Device;
6
7
class DeviceObserver
8
{
9
    /**
10
     * Handle the device "updated" event.
11
     *
12
     * @param  \App\Models\Device  $device
13
     * @return void
14
     */
15
    public function updated(Device $device)
16
    {
17
        // handle device dependency updates
18
        if ($device->isDirty('max_depth')) {
19
            $device->children->each->updateMaxDepth();
20
        }
21
22
        // key attribute changes
23
        foreach (['os', 'sysName', 'version', 'hardware', 'features', 'serial', 'icon'] as $attribute) {
24
            if ($device->isDirty($attribute)) {
25
                \Log::event(self::attributeChangedMessage($attribute, $device->$attribute, $device->getOriginal($attribute)), $device, 'system', 3);
26
            }
27
        }
28
        if ($device->isDirty('location_id')) {
29
            \Log::event(self::attributeChangedMessage('location', (string) $device->location, null), $device, 'system', 3);
30
        }
31
    }
32
33
    /**
34
     * Handle the device "deleting" event.
35
     *
36
     * @param  \App\Models\Device  $device
37
     * @return void
38
     */
39
    public function deleting(Device $device)
40
    {
41
        // delete related data
42
        $device->ports()->delete();
43
        $device->syslogs()->delete();
44
        $device->eventlogs()->delete();
45
        $device->applications()->delete();
46
47
        // handle device dependency updates
48
        $device->children->each->updateMaxDepth($device->device_id);
49
    }
50
51
    /**
52
     * Handle the device "Pivot Attached" event.
53
     *
54
     * @param  \App\Models\Device  $device
55
     * @param  string  $relationName  parents or children
56
     * @param  array  $pivotIds  list of pivot ids
57
     * @param  array  $pivotIdsAttributes  additional pivot attributes
58
     * @return void
59
     */
60
    public function pivotAttached(Device $device, $relationName, $pivotIds, $pivotIdsAttributes)
0 ignored issues
show
The parameter $pivotIdsAttributes is not used and could be removed. ( Ignorable by Annotation )

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

60
    public function pivotAttached(Device $device, $relationName, $pivotIds, /** @scrutinizer ignore-unused */ $pivotIdsAttributes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        if ($relationName == 'parents') {
63
            // a parent attached to this device
64
65
            // update the parent's max depth incase it used to be standalone
66
            Device::whereIn('device_id', $pivotIds)->get()->each->validateStandalone();
67
68
            // make sure this device's max depth is updated
69
            $device->updateMaxDepth();
70
        } elseif ($relationName == 'children') {
71
            // a child device attached to this device
72
73
            // if this device used to be standalone, we need to udpate max depth
74
            $device->validateStandalone();
75
76
            // make sure the child's max depth is updated
77
            Device::whereIn('device_id', $pivotIds)->get()->each->updateMaxDepth();
78
        }
79
    }
80
81
    public static function attributeChangedMessage($attribute, $value, $previous)
82
    {
83
        return trans("device.attributes.$attribute") . ': '
84
            . (($previous && $previous != $value) ? "$previous -> " : '')
85
            . $value;
86
    }
87
}
88