Skip to content

Commit

Permalink
Remove usage of one magic method.
Browse files Browse the repository at this point in the history
  • Loading branch information
madpilot78 committed Mar 2, 2025
1 parent 9d328ce commit 515633b
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/Auth/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ private function login(): string

$result = $this->client->get(
['challenge'],
$this->boxInfo->apiUrl . '/login/',
$this->boxInfo->getApiUrl() . '/login/',
);

$this->authManager->setChallenge($result['challenge']);

$result = $this->client->post(
['session_token', 'challenge', 'permissions'],
$this->boxInfo->apiUrl . '/login/session/',
$this->boxInfo->getApiUrl() . '/login/session/',
[
'json' => [
'app_id' => $this->config->appId,
Expand Down
17 changes: 9 additions & 8 deletions src/BoxInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,26 @@ public function getInfo(): array
return $this->info;
}

public function getApiUrl(): string
{
$this->apiUrl ??= $this->makeApiUrl();

return $this->apiUrl ?? '';
}

/**
* @throws OutOfBoundsException
*/
public function __get(string $name): null|bool|int|string
public function getProperty(string $name): null|bool|int|string
{
if ($name === 'apiUrl') {
$this->apiUrl ??= $this->makeApiUrl();

return $this->apiUrl;
}

if (array_key_exists($name, $this->info)) {
return $this->info[$name];
}

throw new OutOfBoundsException('Property ' . $name . ' not found', 101);
}

public function __isset(string $name): bool
public function isPropertySet(string $name): bool
{
return isset($this->info[$name]);
}
Expand Down
6 changes: 6 additions & 0 deletions src/BoxInfoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ interface BoxInfoInterface
public function save(array $data): self;

public function getInfo(): array;

public function getApiUrl(): string;

public function getProperty(string $name): null|bool|int|string;

public function isPropertySet(string $name): bool;
}
12 changes: 6 additions & 6 deletions src/Methods/AbstractMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function get(?int $id, array $params): array
$hasId = isset($id);
return $this->client->get(
$hasId ? static::REQUIRED_GET_ID : static::REQUIRED_GET,
$this->boxInfo->apiUrl . static::API . ($hasId ? $this->separator . $id : ''),
$this->boxInfo->getApiUrl() . static::API . ($hasId ? $this->separator . $id : ''),
['headers' => $this->authHeader],
);
}
Expand All @@ -80,7 +80,7 @@ protected function set(null|int|string $id, array $params): ?array

if (empty(static::REQUIRED_SET)) {
$response = $this->client->post(
$this->boxInfo->apiUrl . static::API . (isset($id) ? $this->separator . $id : ''),
$this->boxInfo->getApiUrl() . static::API . (isset($id) ? $this->separator . $id : ''),
[
'headers' => $this->authHeader,
'json' => $params,
Expand All @@ -95,7 +95,7 @@ protected function set(null|int|string $id, array $params): ?array
} else {
return $this->client->post(
static::REQUIRED_SET,
$this->boxInfo->apiUrl . static::API . (isset($id) ? $this->separator . $id : ''),
$this->boxInfo->getApiUrl() . static::API . (isset($id) ? $this->separator . $id : ''),
[
'headers' => $this->authHeader,
'json' => $params,
Expand All @@ -112,7 +112,7 @@ protected function update(?int $id, array $params): ?array

if (empty(static::REQUIRED_PUT)) {
$response = $this->client->put(
$this->boxInfo->apiUrl . static::API . (isset($id) ? $this->separator . $id : ''),
$this->boxInfo->getApiUrl() . static::API . (isset($id) ? $this->separator . $id : ''),
[
'headers' => $this->authHeader,
'json' => $params,
Expand All @@ -127,7 +127,7 @@ protected function update(?int $id, array $params): ?array
} else {
return $this->client->put(
static::REQUIRED_PUT,
$this->boxInfo->apiUrl . static::API . (isset($id) ? $this->separator . $id : ''),
$this->boxInfo->getApiUrl() . static::API . (isset($id) ? $this->separator . $id : ''),
[
'headers' => $this->authHeader,
'json' => $params,
Expand All @@ -143,7 +143,7 @@ protected function delete(int $id, array $params): void
}

$response = $this->client->delete(
$this->boxInfo->apiUrl . static::API . $this->separator . $id,
$this->boxInfo->getApiUrl() . static::API . $this->separator . $id,
['headers' => $this->authHeader],
);

Expand Down
2 changes: 1 addition & 1 deletion src/Methods/Discover.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function run(string $action = 'get', array|int|string $id = [], array $pa
'/api_version',
));

if (!$this->boxInfo->https_available) {
if (!$this->boxInfo->getProperty('https_available')) {
throw new NotSupportedException('Only https enabled boxes supported.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Methods/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Logout extends AbstractMethod implements MethodInterface
public function run(string $action = 'get', array|int|string $id = [], array $params = []): ?array
{
$response = $this->client->get(
$this->boxInfo->apiUrl . '/login/logout',
$this->boxInfo->getApiUrl() . '/login/logout',
['headers' => $this->authSession->getAuthHeader()],
);

Expand Down
4 changes: 2 additions & 2 deletions src/Methods/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function run(bool $quiet = true, bool $skipSleep = false): ?string

$result = $this->client->post(
['app_token', 'track_id'],
$this->boxInfo->apiUrl . '/login/authorize',
$this->boxInfo->getApiUrl() . '/login/authorize',
[
'json' => [
'app_id' => $this->config->appId,
Expand Down Expand Up @@ -68,7 +68,7 @@ private function poll(bool $quiet = true, bool $skipSleep = false): string
for ($i = 0; $i < self::POLL_MAX; $i++) {
$result = $this->client->get(
['status', 'challenge'],
$this->boxInfo->apiUrl . '/login/authorize/' . $this->trackId,
$this->boxInfo->getApiUrl() . '/login/authorize/' . $this->trackId,
);

$this->authManager->setChallenge($result['challenge']);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Auth/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testGetAuthMissingChallenge(): void
->method('setPermissions')
->with($this->equalTo(self::PERMISSIONS))
->willReturnSelf();
$this->boxInfoStub->method('__get')->willReturn(self::MOCK_URL);
$this->boxInfoStub->method('getApiUrl')->willReturn(self::MOCK_URL);
$this->httpClientStub->method('__call')->willReturn(
[
'logged_in' => false,
Expand Down Expand Up @@ -106,7 +106,7 @@ public function testGetAuthWithChallenge(): void
->method('setPermissions')
->with($this->equalTo(self::PERMISSIONS))
->willReturnSelf();
$this->boxInfoStub->method('__get')->willReturn(self::MOCK_URL);
$this->boxInfoStub->method('getApiUrl')->willReturn(self::MOCK_URL);
$this->httpClientStub->method('__call')->willReturn([
'session_token' => self::SESSION_TOKEN,
'challenge' => self::CHALLENGE,
Expand Down
24 changes: 12 additions & 12 deletions tests/Unit/BoxInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,37 @@ public function testSavingBoxinfoMissing(): void
$this->boxInfo->save($info);
}

public function testBoxInfoGetValue(): void
public function testBoxInfoGetProperty(): void
{
$this->assertInstanceOf(
BoxInfo::class,
$this->boxInfo->save(self::INFO),
);

$this->assertEquals(self::INFO['api_domain'], $this->boxInfo->api_domain);
$this->assertTrue($this->boxInfo->https_available);
$this->assertEquals(self::INFO['api_domain'], $this->boxInfo->getProperty('api_domain'));
$this->assertTrue($this->boxInfo->getProperty('https_available'));
}

public function testBoxInfoIsset(): void
public function testBoxInfoIsPropertySet(): void
{
$this->assertInstanceOf(
BoxInfo::class,
$this->boxInfo->save(self::INFO),
);

$this->assertTrue(isset($this->boxInfo->api_domain));
$this->assertFalse(isset($this->boxInfo->notset));
$this->assertTrue($this->boxInfo->isPropertySet('api_domain'));
$this->assertFalse($this->boxInfo->isPropertySet('notset'));
}

public function testBoxInfoGetValueNotSaved(): void
public function testBoxInfoGetPropertyNotSaved(): void
{
$this->assertInstanceOf(
BoxInfo::class,
$this->boxInfo->save(self::INFO),
);

$this->expectException(OutOfBoundsException::class);
$result = $this->boxInfo->foo;
$result = $this->boxInfo->getProperty('foo');
}

public function testBoxInfoGetApiUrlHttps(): void
Expand All @@ -107,7 +107,7 @@ public function testBoxInfoGetApiUrlHttps(): void

$this->assertEquals(
'https://' . self::INFO['api_domain'] . self::INFO['api_base_url'] . 'v' . $major,
$this->boxInfo->apiUrl,
$this->boxInfo->getApiUrl(),
);
}

Expand All @@ -124,7 +124,7 @@ public function testBoxInfoGetApiUrlHttp(): void

$this->assertEquals(
'http://' . $info['api_domain'] . $info['api_base_url'] . 'v' . $major,
$this->boxInfo->apiUrl,
$this->boxInfo->getApiUrl(),
);
}

Expand All @@ -139,7 +139,7 @@ public function testBoxInfoGetApiUrlNonLocal(): void

$this->assertEquals(
'https://' . self::INFO['api_domain'] . ':' . self::INFO['https_port'] . self::INFO['api_base_url'] . 'v' . $major,
$this->boxInfo->apiUrl,
$this->boxInfo->getApiUrl(),
);
}

Expand All @@ -152,7 +152,7 @@ public function testBoxInfoNonDefaultHostname(): void
$this->boxInfo->save(self::INFO),
);

$this->assertEquals($custom, $this->boxInfo->api_domain);
$this->assertEquals($custom, $this->boxInfo->getProperty('api_domain'));
}

public function testBoxInfoGetInfo(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Methods/DiscoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function setUp(): void
public function testDiscoverSuccess(): void
{
$this->boxInfoStub
->method('__get')
->method('getProperty')
->willReturn(true);

$this->assertNull($this->discover->run());
Expand All @@ -51,7 +51,7 @@ public function testDiscoverSuccess(): void
public function testDiscoverNoHttps(): void
{
$this->boxInfoStub
->method('__get')
->method('getProperty')
->willReturn(false);

$this->expectException(NotSupportedException::class);
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Methods/FwRedirTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function testGetFwRedirs(): void
$this->equalTo('get'),
$this->equalTo([
[''],
$this->boxInfoStub->apiUrl . '/fw/redir/',
$this->boxInfoStub->getApiUrl() . '/fw/redir/',
['headers' => $this->authSessionStub->getAuthHeader()],
]),
)
Expand All @@ -227,7 +227,7 @@ public function testGetFwRedir(): void
$this->equalTo('get'),
$this->equalTo([
self::REQUIREDARGS,
$this->boxInfoStub->apiUrl . '/fw/redir/1',
$this->boxInfoStub->getApiUrl() . '/fw/redir/1',
['headers' => $this->authSessionStub->getAuthHeader()],
]),
)
Expand All @@ -245,7 +245,7 @@ public function testSetFwRedir(): void
$this->equalTo('post'),
$this->equalTo([
self::REQUIREDARGS,
$this->boxInfoStub->apiUrl . '/fw/redir/',
$this->boxInfoStub->getApiUrl() . '/fw/redir/',
[
'headers' => $this->authSessionStub->getAuthHeader(),
'json' => self::FWREDIRSET,
Expand Down Expand Up @@ -287,7 +287,7 @@ public function testUpdateFwRedir(): void
$this->equalTo('put'),
$this->equalTo([
self::REQUIREDARGS,
$this->boxInfoStub->apiUrl . '/fw/redir/1',
$this->boxInfoStub->getApiUrl() . '/fw/redir/1',
[
'headers' => $this->authSessionStub->getAuthHeader(),
'json' => ['enabled' => false],
Expand Down Expand Up @@ -325,7 +325,7 @@ public function testDeleteFwRedir(): void
->with(
$this->equalTo('delete'),
$this->equalTo([
$this->boxInfoStub->apiUrl . '/fw/redir/3',
$this->boxInfoStub->getApiUrl() . '/fw/redir/3',
['headers' => $this->authSessionStub->getAuthHeader()],
]),
)
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Methods/LanBrowserInterfacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testGetLanBroserInterfaces(): void
$this->equalTo('get'),
$this->equalTo([
[''],
$this->boxInfoStub->apiUrl . '/lan/browser/interfaces/',
$this->boxInfoStub->getApiUrl() . '/lan/browser/interfaces/',
['headers' => $this->authSessionStub->getAuthHeader()],
]),
)
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Methods/LanWolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testSetLanWol(): void
->with(
$this->equalTo('post'),
$this->equalTo([
$this->boxInfoStub->apiUrl . '/lan/wol/pub',
$this->boxInfoStub->getApiUrl() . '/lan/wol/pub',
[
'headers' => $this->authSessionStub->getAuthHeader(),
'json' => self::LANWOLSET,
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Methods/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testGetLanguage(): void
$this->equalTo('get'),
$this->equalTo([
['lang', 'avalaible'],
$this->boxInfoStub->apiUrl . '/lang',
$this->boxInfoStub->getApiUrl() . '/lang',
['headers' => $this->authSessionStub->getAuthHeader()],
]),
)
Expand All @@ -77,7 +77,7 @@ public function testSetLanguage(): void
->with(
$this->equalTo('post'),
$this->equalTo([
$this->boxInfoStub->apiUrl . '/lang',
$this->boxInfoStub->getApiUrl() . '/lang',
[
'headers' => $this->authSessionStub->getAuthHeader(),
'json' => ['lang' => 'eng'],
Expand Down Expand Up @@ -115,7 +115,7 @@ public function testSetLanguageFail(): void
->with(
$this->equalTo('post'),
$this->equalTo([
$this->boxInfoStub->apiUrl . '/lang',
$this->boxInfoStub->getApiUrl() . '/lang',
[
'headers' => $this->authSessionStub->getAuthHeader(),
'json' => ['lang' => 'eng'],
Expand Down

0 comments on commit 515633b

Please sign in to comment.