Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Add named constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Oct 13, 2021
1 parent a4d6ad4 commit ca5b1c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ $client = new Client(getenv('GH_SPONSORS_TOKEN'));

This will be the client we'll use throughout the rest of these docs. We'll re-use the `$client` variable in the below examples.

Additionally, you have two named constructors that you can use: `withEnv` that accepts an environment variable and `withToken` that accepts a token.

```php
use GitHub\Sponsors\Client;

$client = Client::withEnv('GH_SPONSORS_TOKEN');

$client = Client::withToken(getenv('GH_SPONSORS_TOKEN'));
```

### Initializing the client using Laravel

If you're using Laravel, the client is already bound to the container as a singleton. Simply retrieve it from the container:
Expand Down
10 changes: 10 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public function __construct(string $token, Factory $http = null)
$this->token = $token;
}

public static function withEnv(string $env, Factory $http = null): self
{
return new self(getenv($env), $http);
}

public static function withToken(string $token, Factory $http = null): self
{
return new self($token, $http);
}

public function viewer(): Viewer
{
return new Viewer($this);
Expand Down

0 comments on commit ca5b1c2

Please sign in to comment.