/** * Handle the event. * * @param AccessSwitchSlotWasModified $event * @return void */ public function handle(SlotModuleWasChanged $event) { $original_slot_module = $event->access_switch_slot; $new_slot_module = AccessSwitchSlot::findOrFail($original_slot_module->id); $originalPortCount = $original_slot_module->module_type->number_of_ports; $originalModuleType = $original_slot_module->module_type->name; $newPortCount = $new_slot_module->module_type->number_of_ports; $newModuleType = $new_slot_module->module_type->name; $switchName = $new_slot_module->access_switch->name; $slotNumber = $new_slot_module->slot_number; if ($original_slot_module->module_type->empty_slot) { // If the original state was empty, we only need to add ports for ($port_number = 1; $port_number <= $newPortCount; $port_number++) { $new_slot_module->ports()->create(['port_number' => $port_number]); } logThis($newModuleType . ' added to ' . $switchName . ' slot ' . $slotNumber); } else { // First remove any ports associated with the slot foreach ($original_slot_module->ports as $port) { $port->delete(); } // Now add the new ports if ($new_slot_module->module_type->empty_slot) { logThis($originalModuleType . ' removed from ' . $switchName . ' slot ' . $slotNumber); } else { for ($port_number = 1; $port_number <= $newPortCount; $port_number++) { $new_slot_module->ports()->create(['port_number' => $port_number]); } } logThis($originalModuleType . ' removed from ' . $switchName . ' slot ' . $slotNumber . ' and replaced with ' . $newModuleType); } }
/** * Assign a module type to a switch slot */ public function apiChangeSlotModule(AccessSwitchSlot $access_switch_slot, Request $request) { $access_switch_slot->update(['module_type_id' => $request->module_type_id]); event(new SlotModuleWasChanged($access_switch_slot)); // fyi, $access_switch_slot is the ORIGINAL version of the model, not the updated version return AccessSwitchSlot::findOrFail($access_switch_slot->id); }