Skip to content

Commit

Permalink
Merge pull request #227 from plausible/improve_token_validation
Browse files Browse the repository at this point in the history
Fix #224 - If no token is entered, the ClientFactory will no longer return a Client instance.
  • Loading branch information
Dan0sz authored Sep 25, 2024
2 parents 2b73e33 + 4213390 commit d75c72e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
8 changes: 8 additions & 0 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/ClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function testBuild() {
$clientFactory = new ClientFactory();
$client = $clientFactory->build();

$this->assertFalse( $client );

$clientFactory = new ClientFactory( 'test' );
$client = $clientFactory->build();

$this->assertInstanceOf( Client::class, $client );
}
}

0 comments on commit d75c72e

Please sign in to comment.