Add types

This commit is contained in:
Philipp Lang 2023-02-17 14:54:35 +01:00
parent 531f44bc94
commit b5dfa80a1c
8 changed files with 60 additions and 139 deletions

View File

@ -79,7 +79,7 @@ class InsertMembershipsAction
private function singleStrategy(Member $member, Group $group, NamiMembershipEntry $membershipEntry): ?Membership
{
$membership = $this->api()->membership($member->nami_id, $membershipEntry->id);
app(ActivityCreator::class)->createFor($this->api(), $membership->groupId);
app(ActivityCreator::class)->createFor($this->api(), $membership->group());
$activity = Activity::nami($membership->activityId);
if (!$activity) {

View File

@ -10,10 +10,16 @@ use Illuminate\Validation\ValidationException;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
/**
* @template Payload of array{name: string, subactivities: array<int, int>}
*/
class ActivityUpdateAction
{
use AsAction;
/**
* @param Payload $payload
*/
public function handle(Activity $activity, array $payload): void
{
DB::transaction(function() use ($activity, $payload) {
@ -22,6 +28,9 @@ class ActivityUpdateAction
});
}
/**
* @return array<string, string>
*/
public function rules(): array
{
return [
@ -43,20 +52,21 @@ class ActivityUpdateAction
/**
* @todo handle this with a model event on the pivot model
* @param Payload $payload
*/
private function validateNami(Activity $activity, array $payload): void
{
if ($activity->name !== data_get($payload, 'name', '')) {
if ($activity->name !== $payload['name']) {
throw ValidationException::withMessages(['nami_id' => 'Aktivität ist in NaMi. Update des Namens nicht möglich.']);
}
$removingSubactivities = $activity->subactivities()->whereNotIn('id', data_get($payload, 'subactivities'))->pluck('id');
$removingSubactivities = $activity->subactivities()->whereNotIn('id', $payload['subactivities'])->pluck('id');
if ($removingSubactivities->first(fn ($subactivity) => Subactivity::find($subactivity)->hasNami)) {
throw ValidationException::withMessages(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']);
}
$addingSubactivities = collect(data_get($payload, 'subactivities'))->filter(fn ($subactivityId) => $activity->subactivities->doesntContain($subactivityId));
$addingSubactivities = collect($payload['subactivities'])->filter(fn ($subactivityId) => $activity->subactivities->doesntContain($subactivityId));
if ($addingSubactivities->first(fn ($subactivity) => Subactivity::find($subactivity)->hasNami)) {
throw ValidationException::withMessages(['nami_id' => 'Untertätigkeit kann nicht hinzugefügt werden.']);

View File

@ -3,6 +3,7 @@
namespace App\Initialize;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\Group;
class ActivityCreator
{
@ -50,10 +51,10 @@ class ActivityCreator
'€ LeiterIn',
];
public function createFor(Api $api, int $groupId): void
public function createFor(Api $api, Group $groupId): void
{
$api->activities($groupId)->each(function ($activity) use ($api) {
$activity = \App\Activity::updateOrCreate(['nami_id' => $activity->id], [
$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),
@ -63,7 +64,7 @@ class ActivityCreator
]);
$groups = [];
$api->subactivitiesOf($activity->nami_id)->each(function ($group) use (&$groups) {
$activity->subactivities()->each(function ($group) use (&$groups) {
$group = \App\Subactivity::updateOrCreate(['nami_id' => $group->id], [
'nami_id' => $group->id,
'name' => $group->name,
@ -72,7 +73,7 @@ class ActivityCreator
]);
$groups[] = $group->id;
});
$activity->subactivities()->sync($groups);
$localActivity->subactivities()->sync($groups);
});
}
}

View File

@ -2,7 +2,7 @@
namespace App\Initialize;
use DB;
use Illuminate\Support\Facades\DB;
use Zoomyboy\LaravelNami\Api;
class InitializeActivities
@ -16,8 +16,7 @@ class InitializeActivities
public function handle(): void
{
$groupId = $this->api->groups()->first()->id;
app(ActivityCreator::class)->createFor($this->api, $groupId);
app(ActivityCreator::class)->createFor($this->api, $this->api->groups()->first());
}
public function restore(): void

View File

@ -5,6 +5,7 @@ namespace App\Initialize;
use App\Group;
use DB;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\Group as NamiGroup;
class InitializeGroups
{
@ -17,18 +18,18 @@ class InitializeGroups
public function handle(): void
{
$this->api->groups()->each(function ($group) {
$this->api->groups(null)->each(function ($group) {
$parent = Group::updateOrCreate(['nami_id' => $group->id], ['nami_id' => $group->id, 'name' => $group->name]);
$this->syncChildren($group->id, $parent);
$this->syncChildren($group, $parent);
});
}
private function syncChildren(int $groupId, Group $parent): void
private function syncChildren(NamiGroup $namiParent, Group $parent): void
{
$this->api->subgroupsOf($groupId)->each(function ($subgroup) use ($parent) {
$this->api->groups($namiParent)->each(function ($subgroup) use ($parent) {
$newParent = Group::updateOrCreate(['nami_id' => $subgroup->id], ['nami_id' => $subgroup->id, 'name' => $subgroup->name, 'parent_id' => $parent->id]);
$this->syncChildren($subgroup->id, $newParent);
$this->syncChildren($subgroup, $newParent);
});
}

@ -1 +1 @@
Subproject commit 07d1e67698ef09fe274a35f3d822ef3dd526ff22
Subproject commit 3c3d0df50e56ce09a09f8624e9491fdd43369dfa

View File

@ -294,31 +294,6 @@ parameters:
count: 1
path: tests/Unit/Mailman/ServiceTest.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Activity\\:\\:fromNami\\(\\) has no return type specified\\.$#"
count: 1
path: packages/laravel-nami/src/Activity.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Activity\\:\\:fromNami\\(\\) has parameter \\$item with no type specified\\.$#"
count: 1
path: packages/laravel-nami/src/Activity.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Activity\\:\\:subactivities\\(\\) has no return type specified\\.$#"
count: 1
path: packages/laravel-nami/src/Activity.php
-
message: "#^Unable to resolve the template type TKey in call to function collect$#"
count: 1
path: packages/laravel-nami/src/Activity.php
-
message: "#^Unable to resolve the template type TValue in call to function collect$#"
count: 1
path: packages/laravel-nami/src/Activity.php
-
message: "#^Property Zoomyboy\\\\LaravelNami\\\\Authentication\\\\FakeCookie\\:\\:\\$authenticated type has no value type specified in iterable type array\\.$#"
count: 1
@ -459,21 +434,6 @@ parameters:
count: 1
path: packages/laravel-nami/src/Gender.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Group\\:\\:fees\\(\\) has no return type specified\\.$#"
count: 1
path: packages/laravel-nami/src/Group.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Group\\:\\:fromResponse\\(\\) has parameter \\$response with no value type specified in iterable type array\\.$#"
count: 1
path: packages/laravel-nami/src/Group.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Group\\:\\:subgroups\\(\\) has no return type specified\\.$#"
count: 1
path: packages/laravel-nami/src/Group.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Logger\\:\\:__construct\\(\\) has parameter \\$options with no type specified\\.$#"
count: 1
@ -674,41 +634,6 @@ parameters:
count: 1
path: packages/laravel-nami/src/Region.php
-
message: "#^Call to an undefined static method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:getNullValue\\(\\)\\.$#"
count: 1
path: packages/laravel-nami/src/Subactivity.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:fromNami\\(\\) has no return type specified\\.$#"
count: 1
path: packages/laravel-nami/src/Subactivity.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:fromNami\\(\\) has parameter \\$item with no type specified\\.$#"
count: 1
path: packages/laravel-nami/src/Subactivity.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:getIsNullAttribute\\(\\) has no return type specified\\.$#"
count: 1
path: packages/laravel-nami/src/Subactivity.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:getNameAttribute\\(\\) has no return type specified\\.$#"
count: 1
path: packages/laravel-nami/src/Subactivity.php
-
message: "#^Unable to resolve the template type TKey in call to function collect$#"
count: 1
path: packages/laravel-nami/src/Subactivity.php
-
message: "#^Unable to resolve the template type TValue in call to function collect$#"
count: 1
path: packages/laravel-nami/src/Subactivity.php
-
message: "#^Method Zoomyboy\\\\LaravelNami\\\\Tests\\\\Stub\\\\Member\\:\\:findByNamiId\\(\\) has no return type specified\\.$#"
count: 1

View File

@ -8,7 +8,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use PHPUnit\Framework\MockObject\Stub;
use Tests\TestCase;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Group;
use Zoomyboy\LaravelNami\Data\Group;
class InitializeGroupsTest extends TestCase
{
@ -34,13 +34,12 @@ class InitializeGroupsTest extends TestCase
public function testItSynchsAGroupWithASingleNodeAndNoChildren(): void
{
$parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]);
$this->api->method('groups')->will($this->returnValueMap([
[
null,
collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')]),
],
[null, collect([$parentGroup])],
[$parentGroup, collect([])],
]));
$this->api->method('subgroupsOf')->willReturn(collect([]));
$this->api->method('groups')->willReturn(collect([]));
(new InitializeGroups($this->api))->handle();
@ -54,13 +53,11 @@ class InitializeGroupsTest extends TestCase
public function testItDoesntCreateAGroupTwiceWithTheSameNamiId(): void
{
GroupModel::factory()->create(['nami_id' => 150]);
$parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]);
$this->api->method('groups')->will($this->returnValueMap([
[
null,
collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')]),
],
[null, collect([$parentGroup])],
[$parentGroup, collect([])],
]));
$this->api->method('subgroupsOf')->willReturn(collect([]));
(new InitializeGroups($this->api))->handle();
@ -69,17 +66,13 @@ class InitializeGroupsTest extends TestCase
public function testItSynchsSubgroups(): void
{
GroupModel::factory()->create(['nami_id' => 150]);
$this->api->method('groups')->willReturn(
collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')])
);
$this->api->method('subgroupsOf')->willReturnCallback(function ($groupId) {
if (150 === $groupId) {
return collect([(new Group())->setParentId(150)->setId(200)->setName('subgroup')]);
}
return collect([]);
});
$parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]);
$subgroup = Group::from(['id' => 200, 'name' => 'subgroup', 'parentId' => 150]);
$this->api->method('groups')->will($this->returnValueMap([
[null, collect([$parentGroup])],
[$parentGroup, collect([$subgroup])],
[$subgroup, collect([])],
]));
(new InitializeGroups($this->api))->handle();
@ -91,20 +84,15 @@ class InitializeGroupsTest extends TestCase
public function testItSynchsSubgroupsOfSubgroups(): void
{
GroupModel::factory()->create(['nami_id' => 150]);
$this->api->method('groups')->willReturn(
collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')])
);
$this->api->method('subgroupsOf')->willReturnCallback(function ($groupId) {
if (150 === $groupId) {
return collect([(new Group())->setParentId(150)->setId(200)->setName('subgroup')]);
}
if (200 === $groupId) {
return collect([(new Group())->setParentId(200)->setId(201)->setName('subsubgroup')]);
}
return collect([]);
});
$parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]);
$subgroup = Group::from(['id' => 200, 'name' => 'subgroup', 'parentId' => 150]);
$subsubgroup = Group::from(['id' => 250, 'name' => 'subsubgroup', 'parentId' => 200]);
$this->api->method('groups')->will($this->returnValueMap([
[null, collect([$parentGroup])],
[$parentGroup, collect([$subgroup])],
[$subgroup, collect([$subsubgroup])],
[$subsubgroup, collect([])],
]));
(new InitializeGroups($this->api))->handle();
@ -114,16 +102,13 @@ class InitializeGroupsTest extends TestCase
public function testItAssignsIdAndParentToAnExistingSubgroup(): void
{
GroupModel::factory()->create(['nami_id' => 200]);
$this->api->method('groups')->willReturn(
collect([(new Group())->setParentId(null)->setId(150)->setName('root')])
);
$this->api->method('subgroupsOf')->willReturnCallback(function ($groupId) {
if (150 === $groupId) {
return collect([(new Group())->setParentId(150)->setId(200)->setName('child')]);
}
return collect([]);
});
$parentGroup = Group::from(['id' => 150, 'name' => 'root', 'parentId' => null]);
$subgroup = Group::from(['id' => 200, 'name' => 'child', 'parentId' => 150]);
$this->api->method('groups')->will($this->returnValueMap([
[null, collect([$parentGroup])],
[$parentGroup, collect([$subgroup])],
[$subgroup, collect([])],
]));
(new InitializeGroups($this->api))->handle();