adrema/app/Initialize/ActivityCreator.php

80 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Initialize;
2022-02-12 16:16:56 +01:00
use Zoomyboy\LaravelNami\Api;
2023-02-17 14:54:35 +01:00
use Zoomyboy\LaravelNami\Data\Group;
2022-02-12 16:16:56 +01:00
2022-03-11 20:19:17 +01:00
class ActivityCreator
{
/** @var array<int, string> */
private array $tries = [
'Schnuppermitgliedschaft',
];
/** @var array<int, string> */
private array $members = [
'€ Mitglied',
'Schnuppermitgliedschaft',
];
/** @var array<int, string> */
private array $filterableActivities = [
'€ Mitglied',
'€ passive Mitgliedschaft',
'€ KassiererIn',
'€ LeiterIn',
'Schnuppermitgliedschaft',
];
/** @var array<int, string> */
private array $filterableSubactivities = [
'Biber',
'Wölfling',
'Jungpfadfinder',
'Pfadfinder',
'Vorstand',
'Rover',
];
/** @var array<int, string> */
private array $ageGroups = [
'Biber',
'Wölfling',
'Jungpfadfinder',
'Pfadfinder',
'Rover',
];
/** @var array<int, string> */
private array $efz = [
'€ LeiterIn',
];
2023-02-17 14:54:35 +01:00
public function createFor(Api $api, Group $groupId): void
2022-02-12 16:16:56 +01:00
{
2023-02-17 14:54:35 +01:00
$api->activities($groupId)->each(function ($activity) {
$localActivity = \App\Activity::updateOrCreate(['nami_id' => $activity->id], [
'nami_id' => $activity->id,
'name' => $activity->name,
'is_try' => in_array($activity->name, $this->tries),
'is_member' => in_array($activity->name, $this->members),
'is_filterable' => in_array($activity->name, $this->filterableActivities),
'has_efz' => in_array($activity->name, $this->efz),
]);
$groups = [];
2023-02-17 14:54:35 +01:00
$activity->subactivities()->each(function ($group) use (&$groups) {
$group = \App\Subactivity::updateOrCreate(['nami_id' => $group->id], [
'nami_id' => $group->id,
'name' => $group->name,
'is_filterable' => in_array($group->name, $this->filterableSubactivities),
'is_age_group' => in_array($group->name, $this->ageGroups),
]);
$groups[] = $group->id;
});
2023-02-17 14:54:35 +01:00
$localActivity->subactivities()->sync($groups);
});
}
}