Skip to content

Commit

Permalink
Change constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
polidog committed Aug 17, 2023
1 parent 8a5304c commit 3f68931
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
16 changes: 2 additions & 14 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,10 @@
/**
* Class ApiMethods.
*/
class Api
final class Api
{
/**
* @var ClientInterface
*/
private $client;

/**
* @var string
*/
private $currentTeam;

public function __construct(ClientInterface $client, string $currentTeam)
public function __construct(private ClientInterface $client, private string $currentTeam)
{
$this->client = $client;
$this->currentTeam = $currentTeam;
}

public function user(array $params = []): array
Expand Down
10 changes: 2 additions & 8 deletions src/Client/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;

class Authorization
final class Authorization
{
/**
* @var string
*/
private $accessToken;

public function __construct(string $accessToken)
public function __construct(private string $accessToken)
{
$this->accessToken = $accessToken;
}

public function push(HandlerStack $stack): void
Expand Down
18 changes: 4 additions & 14 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@

final class Client implements ClientInterface
{
/**
* @var string
*/
private $accessToken;

/**
* @var HttpClientInterface
*/
private $httpClient;

/**
* @var array
*/
Expand All @@ -34,29 +24,29 @@ final class Client implements ClientInterface
],
];

public function __construct(HttpClientInterface $httpClient)
public function __construct(private HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}

/**
* @throws ClientException
* @throws \RuntimeException
* @throws \JsonException
*/
public function request(string $method, string $path, array $data = []): array
{
try {
$response = $this->httpClient->request($method, $path, $data);

return json_decode($response->getBody()->getContents(), true);
return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);
} catch (GuzzleException $e) {
throw ClientException::newException($e, $method, $path, $data);
}
}

public static function factory(string $accessToken, array $httpOptions = []): self
{
$httpOptions = array_merge(static::$httpOptions, $httpOptions);
$httpOptions = array_merge(self::$httpOptions, $httpOptions);
$authorization = new Authorization($accessToken);

$httpOptions['handler'] = HandlerStack::create();
Expand Down
13 changes: 13 additions & 0 deletions tests/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public function testRequest(): void
$client = new Client($httpClient->reveal());
$client->request('GET', '/test', ['query' => ['a' => 'b']]);
$httpClient->request('GET', '/test', ['query' => ['a' => 'b']]);


$stream->getContents()
->willReturn(json_encode(['a' => 'b']))
->shouldHaveBeenCalledOnce();

$response->getBody()
->willReturn($stream->reveal())
->shouldHaveBeenCalledOnce();

$httpClient->request(Argument::any(), Argument::any(), Argument::any())
->willReturn($response->reveal())
->shouldHaveBeenCalledOnce();
}

public function testRequestException(): void
Expand Down

0 comments on commit 3f68931

Please sign in to comment.