From 95d7280adbc1e1cc45dac40c4af2186e5a3039e8 Mon Sep 17 00:00:00 2001 From: Tiago Araujo Date: Thu, 5 Sep 2019 10:43:53 +0200 Subject: [PATCH] Added notify client --- README.md | 2 +- src/Clients/NotifyClient.php | 80 ++++++++++++++ src/Models/SeenUpdate.php | 102 ++++++++++++++++++ src/Models/VersionControlUpdate.php | 86 +++++++++++++++ src/Models/VersionUpdate.php | 65 +++++++++++ tests/NotifyTest.php | 36 +++++++ .../notify-version-control-seen-update.json | 9 ++ .../mocks/notify-version-control-update.json | 40 +++++++ 8 files changed, 419 insertions(+), 1 deletion(-) create mode 100644 src/Clients/NotifyClient.php create mode 100644 src/Models/SeenUpdate.php create mode 100644 src/Models/VersionControlUpdate.php create mode 100644 src/Models/VersionUpdate.php create mode 100644 tests/NotifyTest.php create mode 100644 tests/mocks/notify-version-control-seen-update.json create mode 100644 tests/mocks/notify-version-control-update.json diff --git a/README.md b/README.md index 5763ab6..3de776b 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ $nstack = new \NStack\NStack($config); [x] Content Localize proposals [x] Content Files [x] Content Collections - [ ] Notify updates + [x] Notify updates [ ] UGC pushlogs [x] Validators diff --git a/src/Clients/NotifyClient.php b/src/Clients/NotifyClient.php new file mode 100644 index 0000000..6a8f7cd --- /dev/null +++ b/src/Clients/NotifyClient.php @@ -0,0 +1,80 @@ + + */ +class NotifyClient extends NStackClient +{ + /** @var string */ + protected $path = 'notify/updates'; + + /** + * versionControlIndex + * + * @param String $platform + * @param String|null $currentVersion + * @param String|null $lastVersion + * @param String|null $test + * @return VersionControlUpdate + * @throws FailedToParseException + * @author Tiago Araujo + */ + public function versionControlIndex( + String $platform, + String $currentVersion = null, + String $lastVersion = null, + String $test = null + ): VersionControlUpdate + { + $path = $this->buildPath($this->path) . "?platform=" . $platform; + if ($currentVersion) { + $path = $path . '¤t_version=' . $currentVersion; + } + if ($lastVersion) { + $path = $path . '&last_version=' . $lastVersion; + } + if ($test) { + $path = $path . '&test=' . $test; + } + + $response = $this->client->get($path); + $contents = $response->getBody()->getContents(); + $data = json_decode($contents, true); + return new VersionControlUpdate($data); + } + + /** + * markUpdateAsSeen + * + * @param String $guid + * @param int $updateId + * @param String $answer + * @param String $type + * @return SeenUpdate + * @throws FailedToParseException + */ + public function markUpdateAsSeen(String $guid, int $updateId, String $answer, String $type) + { + $response = $this->client->post($this->buildPath($this->path), [ + 'form_params' => [ + 'guid' => $guid, + 'update_id' => $updateId, + 'answer' => $answer, + 'type' => $type, + ] + ]); + $contents = $response->getBody()->getContents(); + $data = json_decode($contents, true); + return new SeenUpdate($data['data']); + } + +} \ No newline at end of file diff --git a/src/Models/SeenUpdate.php b/src/Models/SeenUpdate.php new file mode 100644 index 0000000..99b3844 --- /dev/null +++ b/src/Models/SeenUpdate.php @@ -0,0 +1,102 @@ + + */ +class SeenUpdate extends Model +{ + /** @var int */ + protected $id; + + /** @var String */ + protected $guid; + + /** @var string */ + protected $answer; + + /** @var string */ + protected $type; + + /** @var DateTime */ + protected $created; + + /** + * parse + * + * @param array $data + * @throws Exception + */ + public function parse(array $data) + { + $this->id = (int)$data['id']; + $this->guid = (int)$data['guid']; + $this->answer = (string)$data['answer']; + $this->type = (string)$data['type']; + $this->created = new DateTime($data['created']); + } + + /** + * toArray + * + * @return array + */ + public function toArray(): array + { + return [ + 'id' => $this->id, + 'guid' => $this->guid, + 'answer' => $this->answer, + 'type' => $this->type, + 'created' => $this->created, + ]; + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return String + */ + public function getGuid(): String + { + return $this->guid; + } + + /** + * @return string + */ + public function getAnswer(): string + { + return $this->answer; + } + + /** + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * @return DateTime + */ + public function getCreated(): DateTime + { + return $this->created; + } + +} \ No newline at end of file diff --git a/src/Models/VersionControlUpdate.php b/src/Models/VersionControlUpdate.php new file mode 100644 index 0000000..91df120 --- /dev/null +++ b/src/Models/VersionControlUpdate.php @@ -0,0 +1,86 @@ + + */ +class VersionControlUpdate extends Model +{ + /** @var String */ + protected $update; + + /** @var array */ + protected $updateVersions; + + /** @var bool */ + protected $newInVersion; + + /** @var array */ + protected $newInVersions; + + /** + * parse + * + * @param array $data + */ + public function parse(array $data) + { + $this->update = (String)$data['update']; + $this->updateVersions = (array)$data['update_versions']; + $this->newInVersion = (string)$data['new_in_version']; + $this->newInVersions = (array)$data['new_in_versions']; + } + + /** + * toArray + * + * @return array + * @author Casper Rasmussen + */ + public function toArray(): array + { + return [ + 'update' => $this->update, + 'update_versions' => $this->updateVersions, + 'new_in_version' => $this->newInVersion, + 'new_in_versions' => $this->newInVersions, + ]; + } + + /** + * @return String + */ + public function getUpdate(): String + { + return $this->update; + } + + /** + * @return array + */ + public function getUpdateVersions(): array + { + return $this->updateVersions; + } + + /** + * @return bool + */ + public function isNewInVersion(): bool + { + return $this->newInVersion; + } + + /** + * @return array + */ + public function getNewInVersions(): array + { + return $this->newInVersions; + } + +} \ No newline at end of file diff --git a/src/Models/VersionUpdate.php b/src/Models/VersionUpdate.php new file mode 100644 index 0000000..d243320 --- /dev/null +++ b/src/Models/VersionUpdate.php @@ -0,0 +1,65 @@ + + */ +class VersionUpdate extends Model +{ + /** @var int */ + protected $id; + + /** @var String */ + protected $version; + + /** @var String */ + protected $update; + + /** @var bool */ + protected $newInVersion; + + /** @var String */ + protected $changeLog; + + /** @var String */ + protected $fileUrl; + + /** + * parse + * + * @param array $data + */ + public function parse(array $data) + { + $this->id = (String)$data['id']; + $this->version = (array)$data['version']; + $this->update = (string)$data['update']; + $this->newInVersion = (array)$data['new_in_version']; + $this->changeLog = (array)$data['change_log']; + $this->fileUrl = (array)$data['file_url']; + } + + /** + * toArray + * + * @return array + * @author Casper Rasmussen + */ + public function toArray(): array + { + return [ + 'id' => $this->id, + 'version' => $this->version, + 'update' => $this->update, + 'new_in_version' => $this->newInVersion, + 'change_log' => $this->changeLog, + 'file_url' => $this->fileUrl, + ]; + } + + +} \ No newline at end of file diff --git a/tests/NotifyTest.php b/tests/NotifyTest.php new file mode 100644 index 0000000..76e8703 --- /dev/null +++ b/tests/NotifyTest.php @@ -0,0 +1,36 @@ +getClientWithMockedGet('notify-version-control-update.json'); + + $client = new NotifyClient($this->getConfig(), $client); + $entry = $client->versionControlIndex("mobile"); + + $this->assertInstanceOf(VersionControlUpdate::class, $entry); + } + + public function testVersionControlSeemUpdate() + { + $client = $this->getClientWithMockedPost('notify-version-control-seen-update.json'); + + $client = new NotifyClient($this->getConfig(), $client); + $entry = $client->markUpdateAsSeen('test', 689, 'no', 'newer_version'); + + $this->assertInstanceOf(SeenUpdate::class, $entry); + } +} diff --git a/tests/mocks/notify-version-control-seen-update.json b/tests/mocks/notify-version-control-seen-update.json new file mode 100644 index 0000000..e61df29 --- /dev/null +++ b/tests/mocks/notify-version-control-seen-update.json @@ -0,0 +1,9 @@ +{ + "data": { + "id": 171992, + "guid": "test", + "answer": "no", + "type": "newer_version", + "created": "2019-09-05T07:57:13+00:00" + } +} \ No newline at end of file diff --git a/tests/mocks/notify-version-control-update.json b/tests/mocks/notify-version-control-update.json new file mode 100644 index 0000000..9ff8469 --- /dev/null +++ b/tests/mocks/notify-version-control-update.json @@ -0,0 +1,40 @@ +{ + "update": "yes", + "update_versions": [ + { + "id": 689, + "version": "40", + "update": "on", + "new_in_version": false, + "change_log": null, + "file_url": null + }, + { + "id": 688, + "version": "35", + "update": "on", + "new_in_version": false, + "change_log": null, + "file_url": null + } + ], + "new_in_version": false, + "new_in_versions": [ + { + "id": 689, + "version": "40", + "update": "on", + "new_in_version": false, + "change_log": null, + "file_url": null + }, + { + "id": 688, + "version": "35", + "update": "on", + "new_in_version": false, + "change_log": null, + "file_url": null + } + ] +} \ No newline at end of file