From 7d0f87902c73130a00c555d3547c4795e3f09371 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 20 Sep 2024 10:24:30 +0200 Subject: [PATCH 1/3] Improved: if no token is entered, the ClientFactory will no longer return a Client instance. --- src/Client.php | 2 +- src/ClientFactory.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 93147e96..b9f7e26e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -34,7 +34,7 @@ class Client { */ public function __construct( $token = '' ) { $config = Configuration::getDefaultConfiguration()->setUsername( 'WordPress' )->setPassword( - $token ?: Helpers::get_settings()[ 'api_token' ] + $token )->setHost( Helpers::get_hosted_domain_url() ); $this->api_instance = new DefaultApi( new GuzzleClient(), $config ); } diff --git a/src/ClientFactory.php b/src/ClientFactory.php index ab33cb33..f52c1b49 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -34,6 +34,14 @@ public function build() { return false; // @codeCoverageIgnore } + if ( ! $this->token ) { + $this->token = Helpers::get_settings()[ 'api_token' ]; + } + + if ( ! $this->token ) { + return false; + } + return new Client( $this->token ); } From 85cddae8969d16dde7ca965d75353a3c06be3e7a Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:00:00 +0200 Subject: [PATCH 2/3] Fixed test. --- tests/unit/ClientFactoryTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/unit/ClientFactoryTest.php b/tests/unit/ClientFactoryTest.php index 06d1211a..c3292254 100644 --- a/tests/unit/ClientFactoryTest.php +++ b/tests/unit/ClientFactoryTest.php @@ -17,6 +17,11 @@ public function testBuild() { $clientFactory = new ClientFactory(); $client = $clientFactory->build(); + $this->assertInstanceOf( false, $client ); + + $clientFactory = new ClientFactory( 'test' ); + $client = $clientFactory->build(); + $this->assertInstanceOf( Client::class, $client ); } } From 421339044d338d74a2145ec9376a3f45100eb21d Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:03:00 +0200 Subject: [PATCH 3/3] Fixed test. --- tests/unit/ClientFactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/ClientFactoryTest.php b/tests/unit/ClientFactoryTest.php index c3292254..74486046 100644 --- a/tests/unit/ClientFactoryTest.php +++ b/tests/unit/ClientFactoryTest.php @@ -17,7 +17,7 @@ public function testBuild() { $clientFactory = new ClientFactory(); $client = $clientFactory->build(); - $this->assertInstanceOf( false, $client ); + $this->assertFalse( $client ); $clientFactory = new ClientFactory( 'test' ); $client = $clientFactory->build();