fruivita/corporate

View on GitHub
src/Importer/DepartmentImporter.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace FruiVita\Corporate\Importer;

use FruiVita\Corporate\Models\Department;
use Illuminate\Support\Collection;

final class DepartmentImporter extends BaseImporter
{
    /**
     * {@inheritdoc}
     */
    protected $rules = [
        'id' => ['required', 'integer', 'gte:1'],
        'name' => ['required', 'string',  'max:255'],
        'acronym' => ['required', 'string',  'max:50'],
    ];

    /**
     * {@inheritdoc}
     */
    protected $node = 'lotacao';

    /**
     * {@inheritdoc}
     */
    protected $unique = ['id'];

    /**
     * {@inheritdoc}
     */
    protected $fields_to_update = ['name', 'acronym'];

    /**
     * Create new class instance.
     */
    public static function make()
    {
        return new static();
    }

    /**
     * {@inheritdoc}
     */
    protected function extractFieldsFromNode(\XMLReader $node)
    {
        return [
            'id' => $node->getAttribute('id') ?: null,
            'name' => $node->getAttribute('nome') ?: null,
            'acronym' => $node->getAttribute('sigla') ?: null,
        ];
    }

    /**
     * {@inheritdoc}
     */
    protected function save(Collection $validated)
    {
        Department::upsert(
            $validated->toArray(),
            $this->unique,
            $this->fields_to_update
        );

        DepartmentRelationshipImporter::make()->import($this->file_path);
    }
}