Files
alfa-bank-sdk/src/Core/AlfaConfig.php

59 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
/*
// MODULE_CONTRACT: Defines a strongly-typed, immutable Data Transfer Object (DTO) for all connector configurations.
// RESPONSIBILITY: To hold and validate critical configuration parameters like certificate paths and API credentials.
// INPUTS: Configuration primitives (strings for paths, URLs, credentials).
// OUTPUTS: A validated, read-only configuration object.
// NOTES:
// - Stores both Transport keys (mTLS) and optional Signing keys (JWS/CMS).
// - Validates file existence upon instantiation.
// LINKS: AlfaBankConnector.php, SbpConnector.php
*/
namespace Apoloz\AlfaBank\Core;
// <SECTION name="class_definition" intent="Define an immutable DTO for connector configuration.">
// <BELIEF_STATE "Using a `readonly` class for configuration is a best practice. It prevents accidental modification of the configuration after instantiation, ensuring that the connector's behavior remains consistent throughout its lifecycle. This pattern is superior to passing around raw arrays.">
readonly class AlfaConfig
{
// <SECTION name="constructor" intent="Inject and validate all configuration properties upon creation.">
// <BELIEF_STATE "The constructor acts as a validation gate. By checking for the existence of certificate and key files here, we enforce a fail-fast policy. It's better to throw an exception immediately upon configuration creation than to fail unexpectedly during an API call later.">
public function __construct(
// <BELIEF_STATE "The base URL for the API endpoint.">
public string $baseUrl,
// <BELIEF_STATE "The file system path to the client's mTLS certificate.">
public string $certificatePath,
// <BELIEF_STATE "The file system path to the client's mTLS private key.">
public string $privateKeyPath,
// <BELIEF_STATE "Optional: The path to a custom Certificate Authority bundle, required for some environments (e.g., Russian Trusted CA).">
public ?string $caBundlePath = null,
// <BELIEF_STATE "Optional: The OAuth client ID, required for token-based authentication.">
public ?string $clientId = null,
// <BELIEF_STATE "Optional: The OAuth client secret, required for token-based authentication.">
public ?string $clientSecret = null,
// <BELIEF_STATE "Optional: The OAuth redirect URI, required for authorization code flow.">
public ?string $redirectUri = null,
// <BELIEF_STATE "Optional: Path to the certificate used specifically for signing requests (JWS/CMS), if different from the transport certificate.">
public ?string $signingCertificatePath = null,
// <BELIEF_STATE "Optional: Path to the private key used specifically for signing requests (JWS/CMS), if different from the transport key.">
public ?string $signingPrivateKeyPath = null,
) {
// <BELIEF_STATE "This runtime check ensures that the provided certificate and key paths are not just valid strings, but point to actual, accessible files on the filesystem. This prevents configuration errors that would otherwise only surface during an HTTP request.">
if (!file_exists($this->certificatePath)) {
throw new \InvalidArgumentException("Certificate not found: {$this->certificatePath}");
}
if (!file_exists($this->privateKeyPath)) {
throw new \InvalidArgumentException("Private key not found: {$this->privateKeyPath}");
}
if ($this->signingCertificatePath && !file_exists($this->signingCertificatePath)) {
throw new \InvalidArgumentException("Signing certificate not found: {$this->signingCertificatePath}");
}
if ($this->signingPrivateKeyPath && !file_exists($this->signingPrivateKeyPath)) {
throw new \InvalidArgumentException("Signing private key not found: {$this->signingPrivateKeyPath}");
}
}
// </SECTION>
}