Create logins.

This commit is contained in:
James Cole 2017-12-27 18:34:03 +01:00
parent 5ba03bd222
commit 97cde38d8f
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 216 additions and 22 deletions

View File

@ -23,7 +23,9 @@ declare(strict_types=1);
namespace FireflyIII\Import\Routine;
use FireflyIII\Models\ImportJob;
use FireflyIII\Models\SpectreProvider;
use FireflyIII\Services\Spectre\Object\Customer;
use FireflyIII\Services\Spectre\Request\CreateLoginRequest;
use FireflyIII\Services\Spectre\Request\ListLoginsRequest;
use FireflyIII\Services\Spectre\Request\NewCustomerRequest;
use Illuminate\Support\Collection;
@ -79,6 +81,7 @@ class SpectreRoutine implements RoutineInterface
/**
*
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function run(): bool
{
@ -103,7 +106,8 @@ class SpectreRoutine implements RoutineInterface
// create new login if list is empty or no login exists.
if (is_null($login)) {
$login = $this->createLogin($customer);
die('new login');
var_dump($login);
exit;
}
echo '<pre>';
@ -113,13 +117,6 @@ class SpectreRoutine implements RoutineInterface
return true;
}
/**
* @param Customer $customer
*/
protected function createLogin(Customer $customer) {
}
/**
* @param ImportJob $job
*/
@ -143,6 +140,44 @@ class SpectreRoutine implements RoutineInterface
}
/**
* @param Customer $customer
*/
protected function createLogin(Customer $customer)
{
$providerId = intval($this->job->configuration['provider']);
$provider = $this->findProvider($providerId);
$createLoginRequest = new CreateLoginRequest($this->job->user);
$createLoginRequest->setCustomer($customer);
$createLoginRequest->setProvider($provider);
$createLoginRequest->setMandatoryFields($this->decrypt($this->job->configuration['mandatory-fields']));
$createLoginRequest->call();
echo '123';
// country code, provider code (find by spectre ID)
// credentials
// daily_refresh=true
// fetch_type=recent
// include_fake_providers=true
// store_credentials=true
var_dump($this->job->configuration);
exit;
}
/**
* @param int $providerId
*
* @return SpectreProvider|null
*/
protected function findProvider(int $providerId): ?SpectreProvider
{
return SpectreProvider::where('spectre_id', $providerId)->first();
}
/**
* @return Customer
* @throws \FireflyIII\Exceptions\FireflyException
@ -157,6 +192,38 @@ class SpectreRoutine implements RoutineInterface
exit;
}
/**
* @param Customer $customer
*
* @return array
* @throws \FireflyIII\Exceptions\FireflyException
*/
protected function listLogins(Customer $customer): array
{
$listLoginRequest = new ListLoginsRequest($this->job->user);
$listLoginRequest->setCustomer($customer);
$listLoginRequest->call();
$logins = $listLoginRequest->getLogins();
return $logins;
}
/**
* @param array $configuration
*
* @return array
*/
private function decrypt(array $configuration): array
{
$new = [];
foreach ($configuration as $key => $value) {
$new[$key] = app('steam')->tryDecrypt($value);
}
return $new;
}
/**
* Return login belonging to country and provider
* TODO must return Login object, not array
@ -178,18 +245,4 @@ class SpectreRoutine implements RoutineInterface
return null;
}
/**
* @return array
*/
private function listLogins(Customer $customer): array
{
$listLoginRequest = new ListLoginsRequest($this->job->user);
$listLoginRequest->setCustomer($customer);
$listLoginRequest->call();
$logins = $listLoginRequest->getLogins();
return $logins;
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* Login.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Spectre\Object;
/**
* Class Login
*/
class Login extends SpectreObject
{
}

View File

@ -0,0 +1,109 @@
<?php
/**
* CreateLoginRequest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Spectre\Request;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\SpectreProvider;
use FireflyIII\Services\Spectre\Object\Customer;
/**
* Class CreateLoginRequest
*/
class CreateLoginRequest extends SpectreRequest
{
/** @var Customer */
private $customer;
/** @var array */
private $mandatoryFields = [];
/** @var SpectreProvider */
private $provider;
/**
*
* @throws FireflyException
*/
public function call(): void
{
// add mandatory fields to login object
$data = [
'customer_id' => $this->customer->getId(),
'country_code' => $this->provider->country_code,
'provider_code' => $this->provider->code,
'credentials' => $this->buildCredentials(),
'daily_refresh' => true,
'fetch_type' => 'recent',
'include_fake_providers' => true,
];
$uri = '/api/v3/logins';
$response = $this->sendSignedSpectrePost($uri, $data);
echo '<pre>';
print_r($response);
exit;
}
/**
* @param Customer $customer
*/
public function setCustomer(Customer $customer): void
{
$this->customer = $customer;
}
/**
* @param array $mandatoryFields
*/
public function setMandatoryFields(array $mandatoryFields): void
{
$this->mandatoryFields = $mandatoryFields;
}
/**
* @param SpectreProvider $provider
*/
public function setProvider(SpectreProvider $provider): void
{
$this->provider = $provider;
}
/**
* @return array
* @throws FireflyException
*/
private function buildCredentials(): array
{
$return = [];
/** @var array $requiredField */
foreach ($this->provider->data['required_fields'] as $requiredField) {
$fieldName = $requiredField['name'];
if (!isset($this->mandatoryFields[$fieldName])) {
throw new FireflyException(sprintf('Mandatory field "%s" is missing from job.', $fieldName));
}
$return[$fieldName] = $this->mandatoryFields[$fieldName];
}
return $return;
}
}