snipe-it/app/Importer/AccessoryImporter.php
Martin Meredith e3e0d57f56 Minor code cleanup bits and bobs (#6805)
* Add IDE Helper files

* Cleanup imports

- Alphabetises imports
- Removes unused imports

* Add Platform requirements

* Move filling asset into block where asset exists

* Remove duplicate array keys
2019-03-13 20:12:03 -07:00

54 lines
1.6 KiB
PHP

<?php
namespace App\Importer;
use App\Models\Accessory;
class AccessoryImporter extends ItemImporter
{
public function __construct($filename)
{
parent::__construct($filename);
}
protected function handle($row)
{
parent::handle($row); // TODO: Change the autogenerated stub
$this->createAccessoryIfNotExists();
}
/**
* Create an accessory if a duplicate does not exist
*
* @author Daniel Melzter
* @since 3.0
*/
public function createAccessoryIfNotExists()
{
$accessory = Accessory::where('name', $this->item['name'])->first();
if ($accessory) {
if (!$this->updating) {
$this->log('A matching Accessory ' . $this->item["name"] . ' already exists. ');
return;
}
$this->log('Updating Accessory');
$accessory->update($this->sanitizeItemForUpdating($accessory));
$accessory->save();
return;
}
$this->log("No Matching Accessory, Creating a new one");
$accessory = new Accessory();
$accessory->fill($this->sanitizeItemForStoring($accessory));
//FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
// $accessory->unsetEventDispatcher();
if ($accessory->save()) {
$accessory->logCreate('Imported using CSV Importer');
$this->log('Accessory ' . $this->item["name"] . ' was created');
return;
}
$this->logError($accessory, 'Accessory');
}
}