Skip to content

Commit

Permalink
Convert RETSVersion to enum and only accept in the constructor. This …
Browse files Browse the repository at this point in the history
…ensures we initialize our parsers with the correct version and avoid various bugs.
  • Loading branch information
nicosp committed Nov 26, 2024
1 parent c74499c commit 4bc3124
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 265 deletions.
61 changes: 26 additions & 35 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace PHRETS;

use PHRETS\Enums\RETSVersion;
use PHRETS\Exceptions\InvalidConfiguration;
use PHRETS\Strategies\SimpleStrategy;
use PHRETS\Strategies\Strategy;
use PHRETS\Versions\RETSVersion;

class Configuration
{
Expand All @@ -17,22 +17,21 @@ class Configuration
protected ?string $login_url = null;
protected string $user_agent = 'PHRETS/2.6.4';
protected ?string $user_agent_password = null;
protected RETSVersion $rets_version;
protected readonly RETSVersion $rets_version;
protected readonly Strategy $strategy;
protected string $http_authentication = 'digest';

/** @var array<string,mixed> */
protected array $options = [];

public function __construct(?Strategy $strategy = null)
{
if ($strategy === null) {
$strategy = new SimpleStrategy();
}
public function __construct(
?Strategy $strategy = null,
?RETSVersion $version = null
) {
$this->rets_version = $version ?? RETSVersion::VERSION_1_5;
$this->strategy = $strategy ?? new SimpleStrategy();

$this->rets_version = (new RETSVersion())->setVersion('1.5');
$this->strategy = $strategy;
$strategy->initialize($this);
$this->strategy->initialize($this);
}

public function getLoginUrl(): ?string
Expand Down Expand Up @@ -66,18 +65,6 @@ public function getRetsVersion(): RETSVersion
return $this->rets_version;
}

/**
* @param string $rets_version
*
* @return $this
*/
public function setRetsVersion(string $rets_version)
{
$this->rets_version = (new RETSVersion())->setVersion($rets_version);

return $this;
}

public function getUserAgent(): string
{
return $this->user_agent;
Expand Down Expand Up @@ -140,11 +127,22 @@ public static function load(array $configuration = []): self
'login_url' => 'LoginUrl',
'user_agent' => 'UserAgent',
'user_agent_password' => 'UserAgentPassword',
'rets_version' => 'RetsVersion',
'http_authentication' => 'HttpAuthenticationMethod',
];

$me = new self();
$version = null;
$retsVersion = $configuration['rets_version'] ?? null;
if ($retsVersion !== null && $retsVersion !== '') {
if (str_starts_with($retsVersion, 'RETS/')) {
$retsVersion = substr($retsVersion, strlen('RETS/'));
}
$version = RETSVersion::tryFrom($retsVersion);
if ($version === null) {
throw new InvalidConfiguration('Invalid RETS version: ' . $retsVersion);
}
}

$me = new self(version: $version);

foreach ($variables as $k => $m) {
if (array_key_exists($k, $configuration)) {
Expand All @@ -169,7 +167,7 @@ public function valid(): bool

/**
*/
public function getStrategy(): \PHRETS\Strategies\Strategy
public function getStrategy(): Strategy
{
return $this->strategy;
}
Expand All @@ -181,17 +179,12 @@ public function userAgentDigestHash(Session $session): string
$ua_a1 = md5($this->getUserAgent() . ':' . $this->getUserAgentPassword());

return md5(
trim((string) $ua_a1) . '::' . trim((string) $session->getRetsSessionId()) .
':' . trim((string) $this->getRetsVersion()->asHeader())
trim($ua_a1) . '::' . trim((string) $session->getRetsSessionId()) .
':' . trim($this->getRetsVersion()->asHeader())
);
}

/**
* @param $auth_method
*
* @return $this
*/
public function setHttpAuthenticationMethod(string $auth_method)
public function setHttpAuthenticationMethod(string $auth_method): self
{
if (!in_array($auth_method, [self::AUTH_BASIC, self::AUTH_DIGEST])) {
throw new \InvalidArgumentException("Given authentication method is invalid. Must be 'basic' or 'digest'");
Expand All @@ -201,8 +194,6 @@ public function setHttpAuthenticationMethod(string $auth_method)
return $this;
}

/**
*/
public function getHttpAuthenticationMethod(): string
{
return $this->http_authentication;
Expand Down
23 changes: 23 additions & 0 deletions src/Enums/RETSVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace PHRETS\Enums;

enum RETSVersion: string
{
case VERSION_1_5 = '1.5';
case VERSION_1_7 = '1.7';
case VERSION_1_7_1 = '1.7.1';
case VERSION_1_7_2 = '1.7.2';
case VERSION_1_8 = '1.8';

public function asHeader(): string
{
return 'RETS/' . $this->value;
}

public function isAtLeast(self $version): bool
{
return version_compare($this->value, $version->value) >= 0;
}
}
7 changes: 0 additions & 7 deletions src/Exceptions/InvalidRETSVersion.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Parsers/GetMetadata/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHRETS\Parsers\GetMetadata;

use PHRETS\Enums\RETSVersion;
use PHRETS\Http\Response;
use PHRETS\Models\Metadata\System as SystemModel;
use PHRETS\Parsers\ParserType;
Expand All @@ -22,7 +23,7 @@ public function parse(Session $rets, Response $response): SystemModel

$configuration = $rets->getConfiguration();

if ($configuration->getRetsVersion()->is1_5()) {
if ($configuration->getRetsVersion() === RETSVersion::VERSION_1_5) {
if (property_exists($base->System, 'SystemID') && $base->System->SystemID !== null) {
$metadata->setSystemID((string) $base->System->SystemID);
}
Expand Down
27 changes: 17 additions & 10 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ public function Login(): Bulletin
* @param int $location
*
*/
public function GetPreferredObject(string $resource, string $type, string $content_id, int $location = 0): ?BaseObject
{
public function GetPreferredObject(
string $resource,
string $type,
string $content_id,
int $location = 0
): ?BaseObject {
$collection = $this->GetObject($resource, $type, $content_id, '0', $location);

return $collection[0] ?? null;
Expand All @@ -115,8 +119,13 @@ public function GetPreferredObject(string $resource, string $type, string $conte
*
* @throws \PHRETS\Exceptions\CapabilityUnavailable
*/
public function GetObject(string $resource, string $type, string $content_ids, string|int $object_ids = '*', $location = 0): array
{
public function GetObject(
string $resource,
string $type,
string $content_ids,
string|int $object_ids = '*',
int $location = 0
): array {
$request_id = GetObject::ids($content_ids, $object_ids);

$response = $this->request(
Expand Down Expand Up @@ -476,7 +485,10 @@ protected function request(string $capability, array $options = [], bool $is_ret
'headers' => $options['headers'],
'body' => $options['body'],
]);
} elseif ($this->configuration->readOption('use_post_method') || array_key_exists('form_params', $options)) {
} elseif (
$this->configuration->readOption('use_post_method') ||
array_key_exists('form_params', $options)
) {
if (array_key_exists('form_params', $options)) {
$this->debug('Using POST method per form_params option');
$query = $options['form_params'];
Expand Down Expand Up @@ -669,11 +681,6 @@ public function getLastResponse(): string
return (string) $this->last_response?->getBody();
}

public function getClient(): ClientInterface
{
return $this->client;
}

public function getRetsSessionId(): ?string
{
return $this->rets_session_id;
Expand Down
3 changes: 2 additions & 1 deletion src/Strategies/SimpleStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHRETS\Strategies;

use PHRETS\Configuration;
use PHRETS\Enums\RETSVersion;
use PHRETS\Exceptions\ParserNotFound;
use PHRETS\Parsers\ParserType;

Expand Down Expand Up @@ -57,7 +58,7 @@ public function provide(ParserType $parser): mixed
*/
public function initialize(Configuration $configuration): void
{
if ($configuration->getRetsVersion()->isAtLeast1_8()) {
if ($configuration->getRetsVersion()->isAtLeast(RETSVersion::VERSION_1_8)) {
$this->classes[ParserType::LOGIN->value] = \PHRETS\Parsers\Login\OneEight::class;
$this->classes[ParserType::OBJECT_POST->value] = \PHRETS\Parsers\PostObject\OneEight::class;
$this->classes[ParserType::UPDATE->value] = \PHRETS\Parsers\Update\OneEight::class;
Expand Down
110 changes: 0 additions & 110 deletions src/Versions/RETSVersion.php

This file was deleted.

13 changes: 6 additions & 7 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use PHRETS\Configuration;
use PHRETS\Enums\RETSVersion;
use PHRETS\Session;
use PHRETS\Strategies\SimpleStrategy;

Expand Down Expand Up @@ -48,15 +49,14 @@ public function itComplainsAboutBadConfig(): void
public function itLoadsDefaultRetsVersion(): void
{
$config = new Configuration();
$this->assertTrue($config->getRetsVersion()->is1_5());
$this->assertSame(RETSVersion::VERSION_1_5, $config->getRetsVersion());
}

#[Test]
public function itHandlesVersionsCorrectly(): void
{
$config = new Configuration();
$config->setRetsVersion('1.7.2');
$this->assertTrue($config->getRetsVersion()->is1_7_2());
$config = new Configuration(version: RETSVersion::VERSION_1_7_2);
$this->assertSame(RETSVersion::VERSION_1_7_2, $config->getRetsVersion());
}

#[Test]
Expand Down Expand Up @@ -103,11 +103,10 @@ public function itAllowsOverridingTheStrategy(): void
#[Test]
public function itGeneratesUserAgentAuthHashesCorrectly(): void
{
$c = new Configuration();
$c = new Configuration(version: RETSVersion::VERSION_1_7_2);
$c->setLoginUrl('http://www.reso.org/login')
->setUserAgent('PHRETS/2.0')
->setUserAgentPassword('12345')
->setRetsVersion('1.7.2');
->setUserAgentPassword('12345');

$s = new Session($c);
$this->assertSame('123c96e02e514da469db6bc61ab998dc', $c->userAgentDigestHash($s));
Expand Down
Loading

0 comments on commit 4bc3124

Please sign in to comment.