diff --git a/app/Import/Routine/SpectreRoutine.php b/app/Import/Routine/SpectreRoutine.php index df5410bd60..fd9f0c6e03 100644 --- a/app/Import/Routine/SpectreRoutine.php +++ b/app/Import/Routine/SpectreRoutine.php @@ -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 '
';
@@ -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;
- }
}
diff --git a/app/Services/Spectre/Object/Login.php b/app/Services/Spectre/Object/Login.php
new file mode 100644
index 0000000000..6de8bed874
--- /dev/null
+++ b/app/Services/Spectre/Object/Login.php
@@ -0,0 +1,32 @@
+.
+ */
+
+declare(strict_types=1);
+
+namespace FireflyIII\Services\Spectre\Object;
+
+/**
+ * Class Login
+ */
+class Login extends SpectreObject
+{
+
+}
\ No newline at end of file
diff --git a/app/Services/Spectre/Request/CreateLoginRequest.php b/app/Services/Spectre/Request/CreateLoginRequest.php
new file mode 100644
index 0000000000..1b0e3e6b79
--- /dev/null
+++ b/app/Services/Spectre/Request/CreateLoginRequest.php
@@ -0,0 +1,109 @@
+.
+ */
+
+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 '';
+ 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;
+ }
+
+
+}
\ No newline at end of file