. */ 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;
    }


}