From e400315cf64cd89e707a74ebb1fcad6e28470242 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Wed, 23 Feb 2022 22:18:11 +0100 Subject: [PATCH] Add Course DTO --- composer.json | 4 ++- composer.lock | 71 +++++++++++++++++++++++++++++++++++++-- src/Api.php | 28 +++++++-------- src/Data/Course.php | 24 +++++++++++++ tests/Unit/CourseTest.php | 6 ++-- 5 files changed, 112 insertions(+), 21 deletions(-) create mode 100644 src/Data/Course.php diff --git a/composer.json b/composer.json index cef8445..df4206c 100644 --- a/composer.json +++ b/composer.json @@ -9,9 +9,11 @@ } ], "require": { + "php": "8.1.*", "guzzlehttp/guzzle": "^6.3.1|^7.0", "laravel/framework": "^8.0", - "laravel/ui": "^3.4" + "laravel/ui": "^3.4", + "spatie/data-transfer-object": "^3.7" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index d13ae69..7fa6d08 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": "c08ae38275555b5d4c8569acc8f033a1", + "content-hash": "91a283d3422069c7fa3a1b13a0850bbf", "packages": [ { "name": "brick/math", @@ -2509,6 +2509,69 @@ ], "time": "2021-09-25T23:10:38+00:00" }, + { + "name": "spatie/data-transfer-object", + "version": "3.7.3", + "source": { + "type": "git", + "url": "https://github.com/spatie/data-transfer-object.git", + "reference": "341f72c77e0fce40ea2e0dcb212cb54dc27bbe72" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/data-transfer-object/zipball/341f72c77e0fce40ea2e0dcb212cb54dc27bbe72", + "reference": "341f72c77e0fce40ea2e0dcb212cb54dc27bbe72", + "shasum": "" + }, + "require": { + "php": "^8.0" + }, + "require-dev": { + "illuminate/collections": "^8.36", + "jetbrains/phpstorm-attributes": "^1.0", + "larapack/dd": "^1.1", + "phpunit/phpunit": "^9.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\DataTransferObject\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Brent Roose", + "email": "brent@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Data transfer objects with batteries included", + "homepage": "https://github.com/spatie/data-transfer-object", + "keywords": [ + "data-transfer-object", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/data-transfer-object/issues", + "source": "https://github.com/spatie/data-transfer-object/tree/3.7.3" + }, + "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" + }, { "name": "swiftmailer/swiftmailer", "version": "v6.3.0", @@ -9046,7 +9109,9 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "php": "8.1.*" + }, "platform-dev": [], - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.2.0" } diff --git a/src/Api.php b/src/Api.php index 5a78d62..ff30a3e 100644 --- a/src/Api.php +++ b/src/Api.php @@ -14,6 +14,7 @@ use Illuminate\Support\Str; use Log; use Zoomyboy\LaravelNami\Authentication\Authenticator; use Zoomyboy\LaravelNami\Concerns\IsNamiMember; +use Zoomyboy\LaravelNami\Data\Course; use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException; use Zoomyboy\LaravelNami\Exceptions\RightException; use Zoomyboy\LaravelNami\NamiException; @@ -198,27 +199,26 @@ class Api { $this->assertLoggedIn(); return $this->fetchCollection('/ica/rest/module/baustein', 'Fetch courses failed') - ->map(fn ($course) => (object) ['name' => $course['descriptor'], 'id' => $course['id']]); + ->map(fn ($course) => (object) ['id' => $course['id']]); } + /** + * @return Collection + */ public function coursesFor(int $memberId): Collection { $this->assertLoggedIn(); - return $this->fetchCollection( - "/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/flist", - 'Courses fetch failed' - )->map(function ($course) use ($memberId) { - $single = $this->fetchData("/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/{$course['id']}", "Error fetching single course"); + return $this->fetchCollection("/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/flist", 'Courses fetch failed') + ->map(fn ($course) => $this->course($memberId, $course['id'])) + ->filter(fn ($course) => $course !== null); + } - return $single ? (object) [ - 'id' => $single['id'], - 'organizer' => $single['veranstalter'], - 'course_id' => $single['bausteinId'], - 'event_name' => $single['vstgName'], - 'completed_at' => $single['vstgTag'], - ] : null; - })->filter(fn ($course) => $course !== null); + public function course(int $memberId, int $courseId): ?Course + { + $single = $this->fetchData("/ica/rest/nami/mitglied-ausbildung/filtered-for-navigation/mitglied/mitglied/{$memberId}/{$courseId}", "Error fetching single course"); + + return $single ? new Course($single) : null; } /** diff --git a/src/Data/Course.php b/src/Data/Course.php new file mode 100644 index 0000000..2e6621d --- /dev/null +++ b/src/Data/Course.php @@ -0,0 +1,24 @@ +assertEquals(788, $course->id); $this->assertEquals('KJA', $course->organizer); - $this->assertEquals(506, $course->course_id); - $this->assertEquals('eventname', $course->event_name); - $this->assertEquals('2021-11-12 00:00:00', $course->completed_at); + $this->assertEquals(506, $course->courseId); + $this->assertEquals('eventname', $course->eventName); + $this->assertEquals('2021-11-12 00:00:00', $course->completedAt); app(CourseFake::class)->assertFetched(11111); app(CourseFake::class)->assertFetchedSingle(11111, 788);