Skip to content

Commit

Permalink
fix: unable to switch API version on service (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
reliq authored Apr 27, 2021
1 parent a24e24d commit fabbddf
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 55 deletions.
29 changes: 4 additions & 25 deletions src/ApiV1/Service/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
use Atymic\Twitter\ApiV1\Traits\StatusTrait;
use Atymic\Twitter\ApiV1\Traits\TrendTrait;
use Atymic\Twitter\ApiV1\Traits\UserTrait;
use Atymic\Twitter\Concern\HotSwapper;
use Atymic\Twitter\Contract\Configuration;
use Atymic\Twitter\Contract\Querier;
use Atymic\Twitter\Exception\ClientException as TwitterClientException;
use InvalidArgumentException;

class Twitter implements TwitterContract
{
Expand All @@ -42,6 +42,7 @@ class Twitter implements TwitterContract
use TrendTrait;
use UserTrait;
use AuthTrait;
use HotSwapper;

private const DEFAULT_EXTENSION = 'json';
private const URL_FORMAT = 'https://%s/%s/%s.%s';
Expand All @@ -55,31 +56,9 @@ public function __construct(Querier $querier)
$this->setQuerier($querier);
}

/**
* @throws InvalidArgumentException
*/
public function usingCredentials(
string $accessToken,
string $accessTokenSecret,
?string $consumerKey = null,
?string $consumerSecret = null
): self {
return $this->setQuerier(
$this->querier->usingCredentials(
$accessToken,
$accessTokenSecret,
$consumerKey,
$consumerSecret
)
);
}

/**
* @throws InvalidArgumentException
*/
public function usingConfiguration(Configuration $configuration): self
public function getQuerier(): Querier
{
return $this->setQuerier($this->querier->usingConfiguration($configuration));
return $this->querier;
}

/**
Expand Down
68 changes: 68 additions & 0 deletions src/Concern/HotSwapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Atymic\Twitter\Concern;

use Atymic\Twitter\Contract\Configuration;
use Atymic\Twitter\Contract\Querier;
use Atymic\Twitter\Twitter;
use InvalidArgumentException;

trait HotSwapper
{
abstract public function getQuerier(): Querier;

/**
* @throws InvalidArgumentException
*/
public function usingCredentials(
string $accessToken,
string $accessTokenSecret,
?string $consumerKey = null,
?string $consumerSecret = null
): Twitter {
return $this->setQuerier(
$this->getQuerier()
->usingCredentials(
$accessToken,
$accessTokenSecret,
$consumerKey,
$consumerSecret
)
);
}

/**
* @throws InvalidArgumentException
*/
public function usingConfiguration(Configuration $configuration): Twitter
{
return $this->setQuerier(
$this->getQuerier()
->usingConfiguration($configuration)
);
}

/**
* @throws InvalidArgumentException
*/
public function forApiV1(): Twitter
{
$config = $this->getQuerier()
->getConfiguration();

return $this->usingConfiguration($config->forApiV1());
}

/**
* @throws InvalidArgumentException
*/
public function forApiV2(): Twitter
{
$config = $this->getQuerier()
->getConfiguration();

return $this->usingConfiguration($config->forApiV2());
}
}
35 changes: 7 additions & 28 deletions src/Service/Accessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
use Atymic\Twitter\Concern\FilteredStream;
use Atymic\Twitter\Concern\Follows;
use Atymic\Twitter\Concern\HideReplies;
use Atymic\Twitter\Concern\HotSwapper;
use Atymic\Twitter\Concern\SampledStream;
use Atymic\Twitter\Concern\SearchTweets;
use Atymic\Twitter\Concern\Timelines;
use Atymic\Twitter\Concern\TweetLookup;
use Atymic\Twitter\Concern\UserLookup;
use Atymic\Twitter\Contract\Configuration;
use Atymic\Twitter\Contract\Querier as QuerierContract;
use Atymic\Twitter\Contract\Twitter as TwitterContract;
use InvalidArgumentException;

final class Accessor implements TwitterContract
{
Expand All @@ -27,6 +26,7 @@ final class Accessor implements TwitterContract
use UserLookup;
use Follows;
use HideReplies;
use HotSwapper;

private QuerierContract $querier;

Expand All @@ -35,36 +35,15 @@ public function __construct(QuerierContract $querier)
$this->querier = $querier;
}

/**
* @throws InvalidArgumentException
* @see QuerierContract::usingCredentials()
*/
public function usingCredentials(
string $accessToken,
string $accessTokenSecret,
?string $consumerKey = null,
?string $consumerSecret = null
): self {
$this->querier = $this->getQuerier()
->usingCredentials($accessToken, $accessTokenSecret, $consumerKey, $consumerSecret);

return $this;
public function getQuerier(): QuerierContract
{
return $this->querier;
}

/**
* @throws InvalidArgumentException
* @see QuerierContract::usingConfiguration()
*/
public function usingConfiguration(Configuration $configuration): self
private function setQuerier(QuerierContract $querier): self
{
$this->querier = $this->getQuerier()
->usingConfiguration($configuration);
$this->querier = $querier;

return $this;
}

public function getQuerier(): QuerierContract
{
return $this->querier;
}
}
10 changes: 10 additions & 0 deletions src/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ public function usingCredentials(
* @see Querier::usingConfiguration()
*/
public function usingConfiguration(Configuration $configuration): self;

/**
* @throws InvalidArgumentException
*/
public function forApiV1(): self;

/**
* @throws InvalidArgumentException
*/
public function forApiV2(): self;
}
4 changes: 2 additions & 2 deletions tests/Unit/Service/AccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function setUp(): void
}

/**
* @covers ::usingCredentials
* @covers \Atymic\Twitter\Concern\HotSwapper::usingCredentials
* @covers ::__construct
* @covers ::getQuerier
* @throws Exception
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testUsingCredentials(): void
}

/**
* @covers ::usingConfiguration
* @covers \Atymic\Twitter\Concern\HotSwapper::usingConfiguration
* @covers ::__construct
* @covers ::getQuerier
* @throws Exception
Expand Down

0 comments on commit fabbddf

Please sign in to comment.