Files
firefly-iii/app/Services/Bunq/Request/InstallationTokenRequest.php

133 lines
3.7 KiB
PHP
Raw Normal View History

2017-08-18 17:08:30 +02:00
<?php
/**
* InstallationTokenRequest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-08-18 17:08:30 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Services\Bunq\Request;
use FireflyIII\Services\Bunq\Id\InstallationId;
use FireflyIII\Services\Bunq\Object\ServerPublicKey;
use FireflyIII\Services\Bunq\Token\InstallationToken;
use Log;
2017-08-18 17:08:30 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class InstallationTokenRequest.
2017-08-18 17:08:30 +02:00
*/
class InstallationTokenRequest extends BunqRequest
{
/** @var InstallationId */
private $installationId;
2017-11-15 12:25:49 +01:00
/** @var InstallationToken */
2017-08-18 17:08:30 +02:00
private $installationToken;
/** @var string */
private $publicKey = '';
/**
2018-03-10 07:16:38 +01:00
* @throws \FireflyIII\Exceptions\FireflyException
2017-08-18 17:08:30 +02:00
*/
public function call(): void
{
2018-03-10 07:16:38 +01:00
Log::debug('Now in InstallationTokenRequest::call()');
$uri = 'installation';
2017-11-15 12:25:49 +01:00
$data = ['client_public_key' => $this->publicKey];
2017-08-18 17:08:30 +02:00
$headers = $this->getDefaultHeaders();
$response = $this->sendUnsignedBunqPost($uri, $data, $headers);
2018-03-10 07:16:38 +01:00
//Log::debug('Installation request response', $response);
2017-08-18 17:08:30 +02:00
$this->installationId = $this->extractInstallationId($response);
$this->serverPublicKey = $this->extractServerPublicKey($response);
$this->installationToken = $this->extractInstallationToken($response);
2018-03-10 07:16:38 +01:00
Log::debug('No errors! We have installation ID!');
Log::debug(sprintf('Installation ID: %s', $this->installationId->getId()));
Log::debug(sprintf('Installation token: %s', $this->installationToken->getToken()));
Log::debug('Server public key: (not included)');
2017-08-18 17:08:30 +02:00
}
/**
* @return InstallationId
*/
public function getInstallationId(): InstallationId
{
return $this->installationId;
}
/**
* @return InstallationToken
*/
public function getInstallationToken(): InstallationToken
{
return $this->installationToken;
}
/**
* @return string
*/
public function getPublicKey(): string
{
return $this->publicKey;
}
/**
* @param string $publicKey
*/
public function setPublicKey(string $publicKey)
{
$this->publicKey = $publicKey;
}
/**
* @param array $response
*
* @return InstallationId
*/
private function extractInstallationId(array $response): InstallationId
{
$installationId = new InstallationId;
$data = $this->getKeyFromResponse('Id', $response);
2018-04-02 14:50:17 +02:00
$installationId->setId((int)$data['id']);
2017-08-18 17:08:30 +02:00
return $installationId;
}
/**
* @param array $response
*
* @return InstallationToken
*/
private function extractInstallationToken(array $response): InstallationToken
{
2018-04-02 14:50:17 +02:00
$data = $this->getKeyFromResponse('Token', $response);
2017-08-18 17:08:30 +02:00
2018-04-02 14:50:17 +02:00
return new InstallationToken($data);
2017-08-18 17:08:30 +02:00
}
/**
* @param array $response
*
* @return ServerPublicKey
*/
private function extractServerPublicKey(array $response): ServerPublicKey
{
2018-04-02 14:50:17 +02:00
$data = $this->getKeyFromResponse('ServerPublicKey', $response);
2017-08-18 17:08:30 +02:00
2018-04-02 14:50:17 +02:00
return new ServerPublicKey($data);
2017-08-18 17:08:30 +02:00
}
2017-08-31 06:47:18 +02:00
}