From 7b336882d94e268c2552297193eadd8aa0d42463 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Tue, 29 Nov 2022 16:53:59 +0100 Subject: [PATCH] --wip-- [skip ci] --- composer.json | 2 +- composer.lock | 331 +++++++++++------- src/Api.php | 7 +- src/Data/Member.php | 15 + src/Enum/Gender.php | 24 ++ src/Member.php | 173 --------- tests/TestCase.php | 3 +- .../PullTest.php} | 15 +- .../{Member => Membership}/MembershipTest.php | 2 +- .../PushMembershipsTest.php | 2 +- 10 files changed, 254 insertions(+), 320 deletions(-) create mode 100644 src/Data/Member.php create mode 100644 src/Enum/Gender.php delete mode 100644 src/Member.php rename tests/Unit/{PullMemberTest.php => Member/PullTest.php} (96%) rename tests/Unit/{Member => Membership}/MembershipTest.php (95%) rename tests/Unit/{Member => Membership}/PushMembershipsTest.php (95%) diff --git a/composer.json b/composer.json index d656f39..01f4dfc 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "guzzlehttp/guzzle": "^6.3.1|^7.0", "laravel/framework": "^9.0", "laravel/ui": "^3.4", - "spatie/data-transfer-object": "^3.7" + "spatie/laravel-data": "^2.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 51ec49a..717efe2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "456a8c775e0524d9b5be373e4177cbff", + "content-hash": "346a5a2a97f685b2c63ce6fb3d29b2be", "packages": [ { "name": "brick/math", @@ -1866,6 +1866,109 @@ }, "time": "2022-01-24T11:29:14+00:00" }, + { + "name": "phpdocumentor/reflection-common", + "version": "2.2.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionCommon.git", + "reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/1d01c49d4ed62f25aa84a747ad35d5a16924662b", + "reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-2.x": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jaap van Otterdijk", + "email": "opensource@ijaap.nl" + } + ], + "description": "Common reflection classes used by phpdocumentor to reflect the code structure", + "homepage": "http://www.phpdoc.org", + "keywords": [ + "FQSEN", + "phpDocumentor", + "phpdoc", + "reflection", + "static analysis" + ], + "support": { + "issues": "https://github.com/phpDocumentor/ReflectionCommon/issues", + "source": "https://github.com/phpDocumentor/ReflectionCommon/tree/2.x" + }, + "time": "2020-06-27T09:03:43+00:00" + }, + { + "name": "phpdocumentor/type-resolver", + "version": "1.6.0", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/TypeResolver.git", + "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/93ebd0014cab80c4ea9f5e297ea48672f1b87706", + "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "phpdocumentor/reflection-common": "^2.0" + }, + "require-dev": { + "ext-tokenizer": "*", + "psalm/phar": "^4.8" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-1.x": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "phpDocumentor\\Reflection\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "me@mikevanriel.com" + } + ], + "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", + "support": { + "issues": "https://github.com/phpDocumentor/TypeResolver/issues", + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.6.0" + }, + "time": "2022-01-04T19:58:01+00:00" + }, { "name": "phpoption/phpoption", "version": "1.8.1", @@ -2523,32 +2626,51 @@ "time": "2021-09-25T23:10:38+00:00" }, { - "name": "spatie/data-transfer-object", - "version": "3.7.3", + "name": "spatie/laravel-data", + "version": "2.0.2", "source": { "type": "git", - "url": "https://github.com/spatie/data-transfer-object.git", - "reference": "341f72c77e0fce40ea2e0dcb212cb54dc27bbe72" + "url": "https://github.com/spatie/laravel-data.git", + "reference": "17d1ddfc5170e85f69bf92498e4b7fc09cf30792" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/spatie/data-transfer-object/zipball/341f72c77e0fce40ea2e0dcb212cb54dc27bbe72", - "reference": "341f72c77e0fce40ea2e0dcb212cb54dc27bbe72", + "url": "https://api.github.com/repos/spatie/laravel-data/zipball/17d1ddfc5170e85f69bf92498e4b7fc09cf30792", + "reference": "17d1ddfc5170e85f69bf92498e4b7fc09cf30792", "shasum": "" }, "require": { - "php": "^8.0" + "illuminate/contracts": "^8.71|^9.0", + "php": "^8.1", + "phpdocumentor/type-resolver": "^1.5", + "spatie/laravel-package-tools": "^1.9.0" }, "require-dev": { - "illuminate/collections": "^8.36", - "jetbrains/phpstorm-attributes": "^1.0", - "larapack/dd": "^1.1", - "phpunit/phpunit": "^9.0" + "fakerphp/faker": "^1.14", + "friendsofphp/php-cs-fixer": "^3.0", + "inertiajs/inertia-laravel": "^0.6.3", + "nette/php-generator": "^3.5", + "nunomaduro/larastan": "^2.0|^1.0.3", + "orchestra/testbench": "^6.24|^7.5", + "phpstan/extension-installer": "^1.1", + "phpunit/phpunit": "^9.3", + "spatie/invade": "^1.0", + "spatie/laravel-typescript-transformer": "^2.0", + "spatie/phpunit-snapshot-assertions": "^4.2", + "spatie/test-time": "^1.2" }, "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\LaravelData\\LaravelDataServiceProvider" + ] + } + }, "autoload": { "psr-4": { - "Spatie\\DataTransferObject\\": "src" + "Spatie\\LaravelData\\": "src", + "Spatie\\LaravelData\\Database\\Factories\\": "database/factories" } }, "notification-url": "https://packagist.org/downloads/", @@ -2557,33 +2679,87 @@ ], "authors": [ { - "name": "Brent Roose", - "email": "brent@spatie.be", - "homepage": "https://spatie.be", + "name": "Ruben Van Assche", + "email": "ruben@spatie.be", "role": "Developer" } ], - "description": "Data transfer objects with batteries included", - "homepage": "https://github.com/spatie/data-transfer-object", + "description": "Create unified resources and data transfer objects", + "homepage": "https://github.com/spatie/laravel-data", "keywords": [ - "data-transfer-object", + "laravel", + "laravel-data", "spatie" ], "support": { - "issues": "https://github.com/spatie/data-transfer-object/issues", - "source": "https://github.com/spatie/data-transfer-object/tree/3.7.3" + "source": "https://github.com/spatie/laravel-data/tree/2.0.2" }, "funding": [ - { - "url": "https://spatie.be/open-source/support-us", - "type": "custom" - }, { "url": "https://github.com/spatie", "type": "github" } ], - "time": "2021-12-30T20:31:10+00:00" + "time": "2022-07-29T09:32:53+00:00" + }, + { + "name": "spatie/laravel-package-tools", + "version": "1.12.1", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-package-tools.git", + "reference": "09f80fa240d44fafb1c70657c74ee44ffa929357" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/09f80fa240d44fafb1c70657c74ee44ffa929357", + "reference": "09f80fa240d44fafb1c70657c74ee44ffa929357", + "shasum": "" + }, + "require": { + "illuminate/contracts": "^7.0|^8.0|^9.0", + "php": "^7.4|^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.4", + "orchestra/testbench": "^5.0|^6.23|^7.0", + "phpunit/phpunit": "^9.4", + "spatie/test-time": "^1.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\LaravelPackageTools\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "role": "Developer" + } + ], + "description": "Tools for creating Laravel packages", + "homepage": "https://github.com/spatie/laravel-package-tools", + "keywords": [ + "laravel-package-tools", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-package-tools/issues", + "source": "https://github.com/spatie/laravel-package-tools/tree/1.12.1" + }, + "funding": [ + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2022-06-28T14:29:26+00:00" }, { "name": "symfony/console", @@ -6297,59 +6473,6 @@ }, "time": "2022-02-21T01:04:05+00:00" }, - { - "name": "phpdocumentor/reflection-common", - "version": "2.2.0", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/ReflectionCommon.git", - "reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/1d01c49d4ed62f25aa84a747ad35d5a16924662b", - "reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b", - "shasum": "" - }, - "require": { - "php": "^7.2 || ^8.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-2.x": "2.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Jaap van Otterdijk", - "email": "opensource@ijaap.nl" - } - ], - "description": "Common reflection classes used by phpdocumentor to reflect the code structure", - "homepage": "http://www.phpdoc.org", - "keywords": [ - "FQSEN", - "phpDocumentor", - "phpdoc", - "reflection", - "static analysis" - ], - "support": { - "issues": "https://github.com/phpDocumentor/ReflectionCommon/issues", - "source": "https://github.com/phpDocumentor/ReflectionCommon/tree/2.x" - }, - "time": "2020-06-27T09:03:43+00:00" - }, { "name": "phpdocumentor/reflection-docblock", "version": "5.3.0", @@ -6407,56 +6530,6 @@ }, "time": "2021-10-19T17:43:47+00:00" }, - { - "name": "phpdocumentor/type-resolver", - "version": "1.6.0", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/93ebd0014cab80c4ea9f5e297ea48672f1b87706", - "reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706", - "shasum": "" - }, - "require": { - "php": "^7.2 || ^8.0", - "phpdocumentor/reflection-common": "^2.0" - }, - "require-dev": { - "ext-tokenizer": "*", - "psalm/phar": "^4.8" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-1.x": "1.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\Reflection\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Mike van Riel", - "email": "me@mikevanriel.com" - } - ], - "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", - "support": { - "issues": "https://github.com/phpDocumentor/TypeResolver/issues", - "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.6.0" - }, - "time": "2022-01-04T19:58:01+00:00" - }, { "name": "phpspec/prophecy", "version": "v1.15.0", diff --git a/src/Api.php b/src/Api.php index 419afa7..6067691 100644 --- a/src/Api.php +++ b/src/Api.php @@ -10,6 +10,7 @@ use Illuminate\Support\Str; use Zoomyboy\LaravelNami\Authentication\Authenticator; use Zoomyboy\LaravelNami\Data\Baustein; use Zoomyboy\LaravelNami\Data\Course; +use Zoomyboy\LaravelNami\Data\Member; use Zoomyboy\LaravelNami\Data\Membership; use Zoomyboy\LaravelNami\Data\MembershipEntry; use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException; @@ -289,14 +290,12 @@ class Api } } - public function member(int $groupId, int $memberId): array + public function member(int $groupId, int $memberId): Member { $this->assertLoggedIn(); $url = $this->url.'/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/'.$groupId.'/'.$memberId; $response = $this->http()->get($url); - Logger::http($url, $response, 'Show member '.$memberId, ['memberId' => $memberId]); - if (false === $response->json()['success'] && Str::startsWith($response['message'], 'Access denied')) { return $this->singleMemberFallback($groupId, $memberId); } @@ -309,7 +308,7 @@ class Api $this->exception('Fetching member failed', $url, $response->json()); } - return $response->json()['data']; + return Member::from($response->json()['data']); } public function hasGroup(int $groupId): bool diff --git a/src/Data/Member.php b/src/Data/Member.php new file mode 100644 index 0000000..22bbde9 --- /dev/null +++ b/src/Data/Member.php @@ -0,0 +1,15 @@ +name) { + 'Männlich' => 'Herr', + 'Weiblich' => 'Frau', + default => '' + }; + } +} diff --git a/src/Member.php b/src/Member.php deleted file mode 100644 index d9b38ae..0000000 --- a/src/Member.php +++ /dev/null @@ -1,173 +0,0 @@ - 19, - 'weiblich' => 20, - 'keine Angabe' => 23, - ]; - - protected static $overviewAttributes = [ - 'vorname' => 'firstname', - 'nachname' => 'lastname', - 'spitzname' => 'nickname', - 'staatsangehoerigkeitText' => 'other_country', - 'staatangehoerigkeitText' => 'other_country', - 'strasse' => 'address', - 'nameZusatz' => 'further_address', - 'plz' => 'zip', - 'ort' => 'location', - 'id' => 'id', - 'telefon1' => 'main_phone', - 'telefon2' => 'mobile_phone', - 'telefon3' => 'work_phone', - 'telefax' => 'fax', - 'email' => 'email', - 'geschlecht' => 'geschlecht_text', - 'geschlechtId' => 'gender_id', - 'emailVertretungsberechtigter' => 'email_parents', - 'staatsangehoerigkeitId' => 'nationality_id', - 'konfessionId' => 'confession_id', - 'geburtsDatum' => 'birthday', - 'eintrittsdatum' => 'joined_at', - 'gruppierungId' => 'group_id', - 'lastUpdated' => 'updated_at', - 'mitgliedsNummer' => 'mitgliedsnr', - 'zeitschriftenversand' => 'send_newspaper', - 'regionId' => 'region_id', - 'landId' => 'country_id', - 'beitragsartId' => 'fee_id', - 'version' => 'version', - 'gruppierung' => 'group_name', - ]; - - protected $casts = []; - - protected $nullable = ['further_address', 'other_country', 'nickname', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents']; - - protected $guarded = []; - - public static function fromNami($item): self - { - $item = collect($item) - ->only(array_keys(static::$overviewAttributes)) - ->mapWithKeys(function ($item, $key) { - return [data_get(static::$overviewAttributes, $key, $key) => $item]; - }) - ->toArray(); - - return new self($item); - } - - public static function fromAttributes(array $attributes): self - { - return new self($attributes); - } - - public function toNami(): array - { - return [ - 'vorname' => $this->firstname, - 'nachname' => $this->lastname, - 'spitzname' => $this->nickname ?: '', - 'strasse' => $this->address, - 'plz' => $this->zip, - 'ort' => $this->location, - 'eintrittsdatum' => $this->joined_at.'T00:00:00', - 'version' => $this->version, - 'beitragsartId' => $this->fee_id, - 'regionId' => $this->region_id ?: Region::getNullValue(), - 'landId' => $this->country_id, - 'staatsangehoerigkeitId' => $this->nationality_id, - 'geburtsDatum' => $this->birthday, - 'geschlechtId' => $this->gender_id ?: Gender::getNullValue(), - 'gruppierungId' => $this->group_id, - 'id' => $this->id, - 'zeitschriftenversand' => $this->send_newspaper, - 'telefon1' => $this->main_phone ?: '', - 'telefon2' => $this->mobile_phone ?: '', - 'telefon3' => $this->work_phone ?: '', - 'telefax' => $this->fax ?: '', - 'email' => $this->email ?: '', - 'emailVertretungsberechtigter' => $this->email_parents ?: '', - 'geburtsDatum' => $this->birthday.' 00:00:00', - 'nameZusatz' => $this->further_address ?: '', - 'konfessionId' => $this->confession_id, - 'staatsangehoerigkeitText' => $this->other_country ?: '', - 'ersteTaetigkeitId' => $this->first_activity_id ?: null, - 'ersteUntergliederungId' => $this->first_subactivity_id ?: null, - ]; - } - - public function getBirthdayAttribute(): ?string - { - return Carbon::parse($this->attributes['birthday'])->format('Y-m-d'); - } - - public function getJoinedAtAttribute(): ?string - { - $date = $this->attributes['joined_at']; - - return empty($date) - ? null - : Carbon::parse($date)->format('Y-m-d'); - } - - public function getGenderIdAttribute(): ?int - { - return $this->attributes['gender_id'] == Gender::getNullValue() ? null : $this->attributes['gender_id']; - } - - public function setGeschlechtTextAttribute($v) - { - $this->attributes['gender_id'] = data_get($this->geschlechtMaps, $v, null); - } - - public function setAttribute($key, $value) - { - if (in_array($key, $this->nullable) && '' === $value) { - return parent::setAttribute($key, null); - } - - return parent::setAttribute($key, $value); - } - - /** - * @return Collection - */ - public function memberships(): Collection - { - return Nami::membershipsOf($this->id); - } - - public function putMembership(Membership $membership): int - { - return Nami::putMembership($this->id, $membership); - } - - public function deleteMembership(int $id): void - { - Nami::deleteMembership($this->id, $this->membership($id)); - } - - public function membership(int $id): ?Membership - { - try { - return Nami::membership($this->id, $id); - } catch (RightException $e) { - return null; - } - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index ae81957..417193a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ namespace Zoomyboy\LaravelNami\Tests; +use Spatie\LaravelData\LaravelDataServiceProvider; use Zoomyboy\LaravelNami\Api; use Zoomyboy\LaravelNami\Authentication\Auth; use Zoomyboy\LaravelNami\Authentication\Authenticator; @@ -19,7 +20,7 @@ class TestCase extends \Orchestra\Testbench\TestCase protected function getPackageProviders($app) { - return [NamiServiceProvider::class]; + return [LaravelDataServiceProvider::class, NamiServiceProvider::class]; } public function getAnnotations(): array diff --git a/tests/Unit/PullMemberTest.php b/tests/Unit/Member/PullTest.php similarity index 96% rename from tests/Unit/PullMemberTest.php rename to tests/Unit/Member/PullTest.php index 03d9d88..65950a7 100644 --- a/tests/Unit/PullMemberTest.php +++ b/tests/Unit/Member/PullTest.php @@ -1,13 +1,13 @@ Http::response($this->fakeJson('member-16.json', ['data' => $input]), 200), ]); - $group = $this->login()->group(103); + $member = $this->login()->member(103, 16); - foreach ($check as $key => $value) { - $this->assertSame($value, $group->member(16)->toArray()[$key]); - } + $this->assertEquals('Max', $member->firstname); - Http::assertSentCount(2); + Http::assertSentCount(1); } /** diff --git a/tests/Unit/Member/MembershipTest.php b/tests/Unit/Membership/MembershipTest.php similarity index 95% rename from tests/Unit/Member/MembershipTest.php rename to tests/Unit/Membership/MembershipTest.php index 12ab310..bff7eb0 100644 --- a/tests/Unit/Member/MembershipTest.php +++ b/tests/Unit/Membership/MembershipTest.php @@ -1,6 +1,6 @@