Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
gradinarufelix committed Sep 28, 2023
1 parent d29f356 commit a498c6a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
3 changes: 1 addition & 2 deletions Classes/Domain/Service/ZoomApiAccessTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ public function createFromConfiguration(): ZoomApiAccessToken
throw new ZoomApiException('Please ensure your Zoom app has the following scopes: user:read:admin, recording:read:admin, meeting:read:admin', 1695040540417);
}

$zoomApiAccessToken = new ZoomApiAccessToken(
return new ZoomApiAccessToken(
$responseBodyAsArray['access_token'],
explode(',', $responseBodyAsArray['scope']));
return $zoomApiAccessToken;
}

/**
Expand Down
43 changes: 28 additions & 15 deletions Classes/Domain/Service/ZoomApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,7 @@ public function getUpcomingMeetings(bool $skipCache = false): array
*/
public function getRecordings(DateTime|string $from, DateTime|string $to, bool $skipCache = false): array
{
if (is_string($from)) {
$from = new DateTimeImmutable($from);
} else {
$from = DateTimeImmutable::createFromMutable($from);
}

if (is_string($to)) {
$to = new DateTimeImmutable($to);
} else {
$to = DateTimeImmutable::createFromMutable($to);
}

if ($from > $to) {
throw new InvalidArgumentException('The from date must be after the to date');
}
list($from, $to) = $this->convertFromAndToAsDateTimeImmutable($from, $to);

$cacheEntryIdentifier = sprintf('recordings_%s_%s', $from->format('Y-m-d'), $to->format('Y-m-d'));
$recordings = $this->getCacheEntry($cacheEntryIdentifier);
Expand Down Expand Up @@ -257,4 +243,31 @@ private function getCacheEntry(string $entryIdentifier): array|bool
{
return $this->requestsCache->get($entryIdentifier);
}

/**
* @param DateTime|string $from
* @param DateTime|string $to
*
* @return DateTimeImmutable[]
* @throws Exception
*/
protected function convertFromAndToAsDateTimeImmutable(DateTime|string $from, DateTime|string $to): array
{
if (is_string($from)) {
$from = new DateTimeImmutable($from);
} else {
$from = DateTimeImmutable::createFromMutable($from);
}

if (is_string($to)) {
$to = new DateTimeImmutable($to);
} else {
$to = DateTimeImmutable::createFromMutable($to);
}

if ($from > $to) {
throw new InvalidArgumentException('The from date must be after the to date');
}
return array($from, $to);
}
}
21 changes: 21 additions & 0 deletions Tests/Unit/ZoomApiAccessTokenFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CodeQ\ZoomApi\Domain\Model\ZoomApiAccessToken;
use CodeQ\ZoomApi\Domain\Service\ZoomApiAccessTokenFactory;
use CodeQ\ZoomApi\ZoomApiException;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
Expand All @@ -22,10 +23,30 @@ public function createFromConfigurationWillReturnAccessToken(): void
])
);
$factoryMock = $this->getFactory($handlerStack);


$accessToken = $factoryMock->createFromConfiguration();
$this->assertInstanceOf(ZoomApiAccessToken::class, $accessToken);
}

/** @test */
public function invalidConfigurationWillThrowException(): void
{
$this->expectException(ZoomApiException::class);
$this->expectExceptionMessage('Please set a Zoom Account ID, Client ID and Secret for CodeQ.ZoomApi to be able to authenticate.');


$factoryMock = $this->getFactory();
$this->inject(
$factoryMock,
'settings',
['auth' => ['accountId' => '', 'clientId' => null, 'clientSecret' => false]]
);


$factoryMock->initializeObject();
}

private function getFactory(HandlerStack $handlerStack = null): ZoomApiAccessTokenFactory|MockObject
{
$factory = $this->getAccessibleMock(ZoomApiAccessTokenFactory::class, ['buildClient'], [], '', false);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/ZoomApiServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function setUp(): void
}

/** @test */
public function it_can_fetch_data()
public function canFetchData(): void
{
$handlerStack = HandlerStack::create(
new MockHandler([
Expand Down

0 comments on commit a498c6a

Please sign in to comment.