From 30e49846e0448d5609c7c5b0c99808b12813eb15 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 10 Mar 2018 07:33:49 +0100 Subject: [PATCH] First list existing devices, then try to get a new one. --- .../Prerequisites/BunqPrerequisites.php | 27 ++++++++++++------- .../Bunq/Request/ListDeviceServerRequest.php | 12 +++++++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app/Import/Prerequisites/BunqPrerequisites.php b/app/Import/Prerequisites/BunqPrerequisites.php index 5c7dc15e59..fae123198e 100644 --- a/app/Import/Prerequisites/BunqPrerequisites.php +++ b/app/Import/Prerequisites/BunqPrerequisites.php @@ -187,7 +187,7 @@ class BunqPrerequisites implements PrerequisitesInterface * * @throws FireflyException */ - private function getExistingDevice(): DeviceServerId + private function getExistingDevice(): ?DeviceServerId { Log::debug('Now in getExistingDevice()'); $installationToken = $this->getInstallationToken(); @@ -205,7 +205,8 @@ class BunqPrerequisites implements PrerequisitesInterface return $device->getId(); } } - throw new FireflyException('Cannot find existing Server Device that can be used by this instance of Firefly III.'); + + return null; } /** @@ -342,13 +343,21 @@ class BunqPrerequisites implements PrerequisitesInterface return $deviceServerId->data; } - Log::debug('Device server ID is null, we have to register.'); + Log::debug('Device server ID is null, we have to find an existing one or register a new one.'); $installationToken = $this->getInstallationToken(); $serverPublicKey = $this->getServerPublicKey(); $apiKey = Preferences::getForUser($this->user, 'bunq_api_key', ''); - Log::debug('Going to create new DeviceServerRequest()'); - $request = new DeviceServerRequest; + // try get the current from a list: + $deviceServerId = $this->getExistingDevice(); + if (null !== $deviceServerId) { + Log::debug('Found device server ID in existing devices list.'); + + return $deviceServerId; + } + + Log::debug('Going to create new DeviceServerRequest() because nothing found in existing list.'); + $request = new DeviceServerRequest; $request->setPrivateKey($this->getPrivateKey()); $request->setDescription('Firefly III v' . config('firefly.version') . ' for ' . $this->user->email); $request->setSecret($apiKey->data); @@ -363,12 +372,12 @@ class BunqPrerequisites implements PrerequisitesInterface } catch (FireflyException $e) { Log::error($e->getMessage()); // we really have to quit at this point :( - throw new FireflyException($e->getMessage()); + //throw new FireflyException($e->getMessage()); } - if (null === $deviceServerId) { - // try get the current from a list: - $deviceServerId = $this->getExistingDevice(); + if(is_null($deviceServerId)) { + throw new FireflyException('Was not able to register server with bunq. Please see the log files.'); } + Preferences::setForUser($this->user, 'bunq_device_server_id', $deviceServerId); Log::debug(sprintf('Server ID: %s', serialize($deviceServerId))); diff --git a/app/Services/Bunq/Request/ListDeviceServerRequest.php b/app/Services/Bunq/Request/ListDeviceServerRequest.php index 72deda39bd..1c7664379b 100644 --- a/app/Services/Bunq/Request/ListDeviceServerRequest.php +++ b/app/Services/Bunq/Request/ListDeviceServerRequest.php @@ -25,6 +25,7 @@ namespace FireflyIII\Services\Bunq\Request; use FireflyIII\Services\Bunq\Object\DeviceServer; use FireflyIII\Services\Bunq\Token\InstallationToken; use Illuminate\Support\Collection; +use Log; /** * Class ListDeviceServerRequest. @@ -40,23 +41,30 @@ class ListDeviceServerRequest extends BunqRequest { parent::__construct(); $this->devices = new Collection; + Log::debug('Constructed ListDeviceServerRequest'); } /** + * @throws \FireflyIII\Exceptions\FireflyException */ public function call(): void { + Log::debug('Now in ListDeviceServerRequest::call()'); $uri = 'device-server'; $data = []; $headers = $this->getDefaultHeaders(); $headers['X-Bunq-Client-Authentication'] = $this->installationToken->getToken(); $response = $this->sendSignedBunqGet($uri, $data, $headers); - + Log::debug('Returned from sending device-server list request!'); // create device server objects: $raw = $this->getArrayFromResponse('DeviceServer', $response); + Log::debug(sprintf('Count %d entries in response array.', count($raw))); + Log::debug('Full response', $response); /** @var array $entry */ foreach ($raw as $entry) { - $this->devices->push(new DeviceServer($entry)); + $server = new DeviceServer($entry); + Log::debug(sprintf('Created server "%s" with IP "%s"', $server->getId()->getId(), $server->getIp())); + $this->devices->push($server); } return;