From 51dd3845671994d0bcd9504f7ca658fa7261b1d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Kr=C3=A1l?= Date: Thu, 12 Nov 2026 15:09:31 +0100 Subject: [PATCH] refactor(client): configure Client with the Config DTO Client::__construct() now takes the existing final readonly Config DTO (and an optional HttpClient) instead of nine scalar parameters, matching CompanyClient, which already received a Config. CompanyClient derives its configuration through the new Config::withEvidence() copy method. BREAKING CHANGE: `new Client($url, $company, $user, $password, $evidence, $verifySSLCertificate, $authSessionId, $debugMode)` becomes `new Client(new Config(...the same values...))`. The README shows the migration. Closes #50 --- README.md | 43 +++++++++++++++++++++++------------ src/Client.php | 15 +------------ src/CompanyClient.php | 12 +--------- src/Config.php | 14 ++++++++++++ tests/ClientOfflineTest.php | 2 +- tests/ClientTest.php | 28 ++++++++++++++--------- tests/ConfigTest.php | 45 +++++++++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 51 deletions(-) create mode 100644 tests/ConfigTest.php diff --git a/README.md b/README.md index 7976839..2766e03 100644 --- a/README.md +++ b/README.md @@ -16,36 +16,51 @@ Podporované verze PHP: 8.2, 8.3, 8.4, 8.5. ## Implementace ``` -$client = new Client( -$accountUrl, -$companyCode, -$restApiUserName, -$restApiPassword, -$evidenceName, -$disableSelfSignedCertificate, -$authSessionId, -); +use EcomailFlexibee\Client; +use EcomailFlexibee\Config; + +$client = new Client(new Config( + $accountUrl, + $companyCode, + $restApiUserName, + $restApiPassword, + $evidenceName, + $verifySSLCertificate, + $authSessionId, + $debugMode, +)); ``` -`$disableSelfSignedCertificate - Vypnutí self signed certifikátu` +`$verifySSLCertificate` - ověřování SSL certifikátu serveru (`false` vypne ověření, např. u self-signed certifikátu) + +`$authSessionId` - hodnota autentizačního id pro Flexibee (volitelné) + +`$debugMode` - při chybě 403 vrátí odeslané hlavičky požadavku (volitelné) -`$authSessionId - Hodnota authentikačního id pro Flexibee` +### Přechod z verze 1.x +Konstruktor `Client` nově přijímá `Config` (a volitelně `HttpClient`) místo jednotlivých hodnot: +``` +// 1.x +$client = new Client($url, $company, $user, $password, $evidence, false, null); +// 2.0 +$client = new Client(new Config($url, $company, $user, $password, $evidence, false, null)); +``` ## Vygenerování autorizačního tokenu ``` -$client = new Client('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null); +$client = new Client(new Config('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null)); $tokens = $client->getAuthAndRefreshToken(); ``` ## Informace o firmách či firmě ``` -$client = new Client('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null); +$client = new Client(new Config('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null)); $companies = $client->getCompanies(); $company = $client->getCompany(); ``` ## Vytvoření či editace záznamu ``` -$client = new Client('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null); +$client = new Client(new Config('https://demo.flexibee.eu', 'demo', 'winstrom', 'winstrom', 'adresar', false, null)); $evidenceData['kod'] = 'prvnizaznam' $evidenceData['nazev'] = 'První kontaktní adresa' $evidenceItemId = $client->save($evidenceData, null, $dryRun, $uriParameters); diff --git a/src/Client.php b/src/Client.php index adbccd8..3bedad8 100644 --- a/src/Client.php +++ b/src/Client.php @@ -32,23 +32,10 @@ class Client protected HttpClient $httpClient; - protected Config $config; - private readonly ResponseHydrator $responseHydrator; - public function __construct( - string $url, - string $company, - string $user, - string $password, - string $evidence, - bool $verifySSLCertificate, - ?string $authSessionId = null, - bool $debugMode = false, - ?HttpClient $httpClient = null, - ) + public function __construct(protected Config $config, ?HttpClient $httpClient = null) { - $this->config = new Config($url, $company, $user, $password, $evidence, $verifySSLCertificate, $authSessionId, $debugMode); $this->queryBuilder = new UrlBuilder($this->config); $this->responseHydrator = new ResponseHydrator($this->config); $this->httpClient = $httpClient ?? new HttpClient(); diff --git a/src/CompanyClient.php b/src/CompanyClient.php index 9f26dde..3f6819a 100644 --- a/src/CompanyClient.php +++ b/src/CompanyClient.php @@ -15,17 +15,7 @@ class CompanyClient extends Client public function __construct(Config $config, ?HttpClient $httpClient = null) { - parent::__construct( - $config->getUrl(), - $config->getCompany(), - $config->getUser(), - $config->getPassword(), - self::EVIDENCE, - $config->verifySSLCertificate(), - $config->getAuthSessionId(), - $config->debugMode(), - $httpClient, - ); + parent::__construct($config->withEvidence(self::EVIDENCE), $httpClient); } public function backup(): Response diff --git a/src/Config.php b/src/Config.php index 1d3ce6c..fbd1cd0 100644 --- a/src/Config.php +++ b/src/Config.php @@ -60,4 +60,18 @@ public function debugMode(): bool return $this->debugMode; } + public function withEvidence(string $evidence): self + { + return new self( + $this->url, + $this->company, + $this->user, + $this->password, + $evidence, + $this->verifySSLCertificate, + $this->authSessionId, + $this->debugMode, + ); + } + } diff --git a/tests/ClientOfflineTest.php b/tests/ClientOfflineTest.php index 389847d..7c1e24c 100644 --- a/tests/ClientOfflineTest.php +++ b/tests/ClientOfflineTest.php @@ -182,7 +182,7 @@ public function testCountInEvidenceReturnsTheRowCount(): void private function createClient(HttpClient $httpClient): Client { - return new Client('https://demo.flexibee.eu', 'demo', 'user', 'pass', 'adresar', verifySSLCertificate: true, httpClient: $httpClient); + return new Client(new ClientConfig('https://demo.flexibee.eu', 'demo', 'user', 'pass', 'adresar', verifySSLCertificate: true), $httpClient); } /** diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 5ae2338..cff8f04 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -5,6 +5,7 @@ namespace EcomailFlexibeeTest; use EcomailFlexibee\Client; +use EcomailFlexibee\Config as ClientConfig; use EcomailFlexibee\Exception\EcomailFlexibeeInvalidAuthorization; use EcomailFlexibee\Exception\EcomailFlexibeeNoEvidenceResult; use EcomailFlexibee\Exception\EcomailFlexibeeRequestFail; @@ -52,7 +53,7 @@ public function testGetLoginFormUrl(): void public function testInvalidAuthorization(): void { - $client = new Client(Config::HOST, Config::COMPANY, 'xxx', 'xxx', Config::EVIDENCE, verifySSLCertificate: false); + $client = new Client(new ClientConfig(Config::HOST, Config::COMPANY, 'xxx', 'xxx', Config::EVIDENCE, verifySSLCertificate: false)); $this->expectException(EcomailFlexibeeInvalidAuthorization::class); $client->findById($this->faker->numberBetween()); } @@ -63,7 +64,7 @@ public function testGetAuthTokenAndMakeSuccessCallWithSessionAuthId(): void Assert::assertArrayHasKey('refreshToken', $authToken); Assert::assertArrayHasKey('authSessionId', $authToken); Assert::assertArrayHasKey('csrfToken', $authToken); - $client = new Client( + $client = new Client(new ClientConfig( Config::HOST, Config::COMPANY, 'xxx', @@ -71,7 +72,7 @@ public function testGetAuthTokenAndMakeSuccessCallWithSessionAuthId(): void Config::EVIDENCE, verifySSLCertificate: false, authSessionId: $this->getString($authToken, 'authSessionId'), - ); + )); Assert::assertNotEmpty($client->allInEvidence()); } @@ -82,7 +83,7 @@ public function testCount(): void public function testInvalidGetAuthToken(): void { - $client = new Client(Config::HOST, Config::COMPANY, 'xxx', 'xxx', Config::EVIDENCE, verifySSLCertificate: false); + $client = new Client(new ClientConfig(Config::HOST, Config::COMPANY, 'xxx', 'xxx', Config::EVIDENCE, verifySSLCertificate: false)); $flexibeeResponse = $client->getAuthAndRefreshToken(); Assert::assertFalse($flexibeeResponse->isSuccess()); $data = $flexibeeResponse->getData(); @@ -155,7 +156,7 @@ public function testEvidenceGetOnlyCustomFields(): void */ public function testCrudOperations(string $evidence, array $evidenceData, array $expectedDataAfterUpdate): void { - $client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, $evidence, verifySSLCertificate: false); + $client = $this->createClient($evidence); $addressBookId = $this->getInt($this->getFirstItem($client->save($evidenceData, id: null)->getData()), 'id'); $client->save($expectedDataAfterUpdate, $addressBookId); $addressBookRefreshed = $this->getFirstItem($client->getById($addressBookId)->getData()); @@ -226,7 +227,7 @@ public function testAllAndChunkInEvidence(): void public function testSearchInEvidence(): void { - $client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false); + $client = $this->createClient('faktura-vydana'); $result = $client->searchInEvidence('(kod neq \'JAN\')', []); Assert::assertTrue(count($result) > 0); $this->expectException(EcomailFlexibeeRequestFail::class); @@ -235,7 +236,7 @@ public function testSearchInEvidence(): void public function testSearchInEvidencePaginated(): void { - $client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false); + $client = $this->createClient('faktura-vydana'); $result = $client->searchInEvidencePaginated('(kod neq \'JAN\')', []); Assert::assertIsInt($result['row_count']); Assert::assertTrue($result['row_count'] > 0); @@ -247,7 +248,7 @@ public function testSearchInEvidencePaginated(): void public function testFindLastInEvidence(): void { - $client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false); + $client = $this->createClient('faktura-vydana'); $result = $client->findLastInEvidence(fullDetail: true); Assert::assertIsArray($result->getData()['faktura-vydana']); Assert::assertCount(1, $result->getData()['faktura-vydana']); @@ -255,7 +256,7 @@ public function testFindLastInEvidence(): void public function testSearchInEvidenceWithInvalidUrl(): void { - $client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false); + $client = $this->createClient('faktura-vydana'); $this->expectException(EcomailFlexibeeRequestFail::class); $client->searchInEvidence(' bla.json', []); } @@ -288,7 +289,7 @@ public function testMakeCustomRequest(): void public function testUnknownCompany(): void { - $client = new Client(Config::HOST, 'xxx', Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false); + $client = new Client(new ClientConfig(Config::HOST, 'xxx', Config::USERNAME, Config::PASSWORD, 'faktura-vydana', verifySSLCertificate: false)); $this->expectException(EcomailFlexibeeRequestFail::class); $client->save([], id: null); } @@ -319,7 +320,12 @@ protected function setUp(): void parent::setUp(); $this->faker = Factory::create(); - $this->client = new Client(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, Config::EVIDENCE, verifySSLCertificate: false); + $this->client = $this->createClient(Config::EVIDENCE); + } + + private function createClient(string $evidence): Client + { + return new Client(new ClientConfig(Config::HOST, Config::COMPANY, Config::USERNAME, Config::PASSWORD, $evidence, verifySSLCertificate: false)); } /** diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 0000000..cacc855 --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,45 @@ +withEvidence('c'); + + Assert::assertEquals( + new ClientConfig( + 'https://demo.flexibee.eu', + 'demo', + 'user', + 'pass', + 'c', + verifySSLCertificate: true, + authSessionId: 'session', + debugMode: true, + ), + $companyConfig, + ); + Assert::assertSame('adresar', $config->getEvidence()); + } + +}