adrema/app/Initialize/InitializeGroups.php

41 lines
1.0 KiB
PHP
Raw Normal View History

2022-03-11 20:19:17 +01:00
<?php
namespace App\Initialize;
use App\Group;
2022-09-02 00:45:39 +02:00
use DB;
use Zoomyboy\LaravelNami\Api;
2023-02-17 14:54:35 +01:00
use Zoomyboy\LaravelNami\Data\Group as NamiGroup;
2022-03-11 20:19:17 +01:00
class InitializeGroups
{
private Api $api;
2022-03-11 20:19:17 +01:00
public function __construct(Api $api)
{
$this->api = $api;
}
public function handle(): void
{
2023-02-17 14:54:35 +01:00
$this->api->groups(null)->each(function ($group) {
$parent = Group::updateOrCreate(['nami_id' => $group->id], ['nami_id' => $group->id, 'name' => $group->name]);
2023-02-17 14:54:35 +01:00
$this->syncChildren($group, $parent);
2022-03-15 10:24:04 +01:00
});
}
2023-02-17 14:54:35 +01:00
private function syncChildren(NamiGroup $namiParent, Group $parent): void
2022-03-15 10:24:04 +01:00
{
2023-02-17 14:54:35 +01:00
$this->api->groups($namiParent)->each(function ($subgroup) use ($parent) {
2022-03-15 10:24:04 +01:00
$newParent = Group::updateOrCreate(['nami_id' => $subgroup->id], ['nami_id' => $subgroup->id, 'name' => $subgroup->name, 'parent_id' => $parent->id]);
2023-02-17 14:54:35 +01:00
$this->syncChildren($subgroup, $newParent);
});
}
2022-09-02 00:45:39 +02:00
public function restore(): void
{
DB::table('groups')->delete();
}
}