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

158 lines
4.2 KiB
PHP
Raw Normal View History

2017-08-19 09:22:44 +02:00
<?php
/**
* DeviceSessionRequest.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-19 09:22:44 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Services\Bunq\Request;
2018-03-10 20:25:42 +01:00
use FireflyIII\Exceptions\FireflyException;
2017-08-19 09:22:44 +02:00
use FireflyIII\Services\Bunq\Id\DeviceSessionId;
use FireflyIII\Services\Bunq\Object\UserCompany;
use FireflyIII\Services\Bunq\Object\UserPerson;
use FireflyIII\Services\Bunq\Token\InstallationToken;
use FireflyIII\Services\Bunq\Token\SessionToken;
/**
2018-05-23 07:38:27 +02:00
* @deprecated
* @codeCoverageIgnore
2017-11-15 12:25:49 +01:00
* Class DeviceSessionRequest.
2017-08-19 09:22:44 +02:00
*/
class DeviceSessionRequest extends BunqRequest
{
2017-11-15 12:25:49 +01:00
/** @var DeviceSessionId */
2017-08-19 09:22:44 +02:00
private $deviceSessionId;
2017-11-15 12:25:49 +01:00
/** @var InstallationToken */
2017-08-19 09:22:44 +02:00
private $installationToken;
2017-11-15 12:25:49 +01:00
/** @var SessionToken */
2017-08-19 09:22:44 +02:00
private $sessionToken;
2017-11-15 12:25:49 +01:00
/** @var UserCompany */
2017-08-19 09:22:44 +02:00
private $userCompany;
2017-11-15 12:25:49 +01:00
/** @var UserPerson */
2017-08-19 09:22:44 +02:00
private $userPerson;
/**
2018-03-10 20:25:42 +01:00
* @throws FireflyException
2017-08-19 09:22:44 +02:00
*/
public function call(): void
{
2018-03-10 07:16:38 +01:00
$uri = 'session-server';
2017-08-19 09:22:44 +02:00
$data = ['secret' => $this->secret];
$headers = $this->getDefaultHeaders();
$headers['X-Bunq-Client-Authentication'] = $this->installationToken->getToken();
$response = $this->sendSignedBunqPost($uri, $data, $headers);
$this->deviceSessionId = $this->extractDeviceSessionId($response);
$this->sessionToken = $this->extractSessionToken($response);
$this->userPerson = $this->extractUserPerson($response);
$this->userCompany = $this->extractUserCompany($response);
}
/**
* @return DeviceSessionId
*/
public function getDeviceSessionId(): DeviceSessionId
{
return $this->deviceSessionId;
}
/**
* @return SessionToken
*/
public function getSessionToken(): SessionToken
{
return $this->sessionToken;
}
2018-03-10 20:25:42 +01:00
/**
* @return UserCompany
*/
public function getUserCompany(): UserCompany
{
return $this->userCompany;
}
2017-08-19 09:22:44 +02:00
/**
* @return UserPerson
*/
public function getUserPerson(): UserPerson
{
return $this->userPerson;
}
/**
* @param InstallationToken $installationToken
*/
public function setInstallationToken(InstallationToken $installationToken)
{
$this->installationToken = $installationToken;
}
/**
* @param array $response
*
* @return DeviceSessionId
*/
private function extractDeviceSessionId(array $response): DeviceSessionId
{
$data = $this->getKeyFromResponse('Id', $response);
$deviceSessionId = new DeviceSessionId;
2018-04-02 14:50:17 +02:00
$deviceSessionId->setId((int)$data['id']);
2017-08-19 09:22:44 +02:00
return $deviceSessionId;
}
2017-12-17 14:30:53 +01:00
/**
* @param array $response
*
* @return SessionToken
*/
2017-08-19 09:22:44 +02:00
private function extractSessionToken(array $response): SessionToken
{
2018-04-02 14:50:17 +02:00
$data = $this->getKeyFromResponse('Token', $response);
2017-08-19 09:22:44 +02:00
2018-04-02 14:50:17 +02:00
return new SessionToken($data);
2017-08-19 09:22:44 +02:00
}
/**
* @param $response
*
* @return UserCompany
*/
private function extractUserCompany($response): UserCompany
{
2018-04-02 14:50:17 +02:00
$data = $this->getKeyFromResponse('UserCompany', $response);
2017-08-19 09:22:44 +02:00
2018-04-02 14:50:17 +02:00
return new UserCompany($data);
2017-08-19 09:22:44 +02:00
}
/**
* @param $response
*
* @return UserPerson
*/
private function extractUserPerson($response): UserPerson
{
2018-04-02 14:50:17 +02:00
$data = $this->getKeyFromResponse('UserPerson', $response);
2017-08-19 09:22:44 +02:00
2018-04-02 14:50:17 +02:00
return new UserPerson($data);
2017-08-19 09:22:44 +02:00
}
2017-08-31 06:47:18 +02:00
}