Skip to content

Commit

Permalink
simplified config objects
Browse files Browse the repository at this point in the history
  • Loading branch information
transistive committed Jun 5, 2023
1 parent b173723 commit 61d3d74
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/Databags/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class Bookmark
/**
* @param list<string> $bookmarks
*/
public function __construct(?iterable $bookmarks = null)
public function __construct(?array $bookmarks = null)
{
$this->values = $bookmarks ?? [];
}
Expand Down
18 changes: 9 additions & 9 deletions src/Databags/DriverConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,32 @@ final class DriverConfiguration
* @psalm-external-mutation-free
*/
public function __construct(
private ?string $userAgent,
$httpPsrBindings,
private string|null $userAgent,
callable|HttpPsrBindings|null $httpPsrBindings,
private SslConfiguration $sslConfig,
private ?int $maxPoolSize,
$cache,
private ?float $acquireConnectionTimeout,
$semaphore
private int|null $maxPoolSize,
CacheInterface|callable|null $cache,
private float|null $acquireConnectionTimeout,
callable|SemaphoreFactoryInterface|null $semaphore
) {
$this->httpPsrBindings = $httpPsrBindings;
$this->cache = $cache;
$this->semaphoreFactory = $semaphore;
}

/**
* @param pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null $httpPsrBindings
* @param callable():(HttpPsrBindings|null)|HttpPsrBindings|null $httpPsrBindings
*
* @pure
*/
public static function create(?string $userAgent, $httpPsrBindings, SslConfiguration $sslConfig, int $maxPoolSize, CacheInterface $cache, float $acquireConnectionTimeout, SemaphoreFactoryInterface $semaphore): self
public static function create(?string $userAgent, callable|HttpPsrBindings|null $httpPsrBindings, SslConfiguration $sslConfig, int $maxPoolSize, CacheInterface $cache, float $acquireConnectionTimeout, SemaphoreFactoryInterface $semaphore): self
{
return new self($userAgent, $httpPsrBindings, $sslConfig, $maxPoolSize, $cache, $acquireConnectionTimeout, $semaphore);
}

/**
* Creates a default configuration with a user agent based on the driver version
* and HTTP PSR implementation auto detected from the environment.
* and HTTP PSR implementation auto-detected from the environment.
*
* @pure
*/
Expand Down
16 changes: 8 additions & 8 deletions src/Databags/HttpPsrBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ final class HttpPsrBindings
/**
* @psalm-mutation-free
*
* @param ClientInterface|callable():ClientInterface|null $client
* @param StreamFactoryInterface|callable():StreamFactoryInterface|null $streamFactory
* @param RequestFactoryInterface|callable():RequestFactoryInterface|null $requestFactory
* @param callable():ClientInterface|ClientInterface|null $client
* @param callable():StreamFactoryInterface|StreamFactoryInterface|null $streamFactory
* @param callable():RequestFactoryInterface|RequestFactoryInterface|null $requestFactory
*/
public function __construct($client = null, $streamFactory = null, $requestFactory = null)
public function __construct(callable|ClientInterface|null $client = null, callable|StreamFactoryInterface $streamFactory = null, callable|RequestFactoryInterface $requestFactory = null)
{
$this->client = $client ?? static fn (): ClientInterface => Psr18ClientDiscovery::find();
$this->streamFactory = $streamFactory ?? static fn (): StreamFactoryInterface => Psr17FactoryDiscovery::findStreamFactory();
Expand All @@ -57,11 +57,11 @@ public function __construct($client = null, $streamFactory = null, $requestFacto
/**
* @pure
*
* @param ClientInterface|callable():ClientInterface|null $client
* @param StreamFactoryInterface|callable():StreamFactoryInterface|null $streamFactory
* @param RequestFactoryInterface|callable():RequestFactoryInterface|null $requestFactory
* @param callable():ClientInterface|ClientInterface|null $client
* @param callable():StreamFactoryInterface|StreamFactoryInterface|null $streamFactory
* @param callable():RequestFactoryInterface|RequestFactoryInterface|null $requestFactory
*/
public static function create($client = null, $streamFactory = null, $requestFactory = null): self
public static function create(callable|ClientInterface $client = null, callable|StreamFactoryInterface $streamFactory = null, callable|RequestFactoryInterface $requestFactory = null): self
{
return new self($client, $streamFactory, $requestFactory);
}
Expand Down
53 changes: 16 additions & 37 deletions src/Databags/SessionConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

namespace Laudis\Neo4j\Databags;

use function call_user_func;
use function is_callable;

use Laudis\Neo4j\Enum\AccessMode;

use function parse_str;
Expand All @@ -35,26 +32,21 @@ final class SessionConfiguration
public const DEFAULT_BOOKMARKS = '[]';

/**
* @param pure-callable():(int|null)|int|null $fetchSize
* @param pure-callable():(iterable<Bookmark>|null)|iterable<Bookmark>|null $bookmarks
* @param pure-callable():((AccessMode | null))|AccessMode|null $accessMode
* @param list<Bookmark>|null $bookmarks
*/
public function __construct(
private ?string $database = null,
private $fetchSize = null,
private $accessMode = null,
private $bookmarks = null
private int|null $fetchSize = null,
private AccessMode|null $accessMode = null,
private array|null $bookmarks = null
) {}

/**
* @pure
*
* @param string|null $database
* @param pure-callable():(int|null)|int|null $fetchSize
* @param pure-callable():(AccessMode|null)|AccessMode|null $defaultAccessMode
* @param pure-callable():(iterable<Bookmark>|null)|iterable<Bookmark> $bookmarks
* @param list<Bookmark>|null $bookmarks
*/
public static function create($database = null, $fetchSize = null, $defaultAccessMode = null, $bookmarks = null): self
public static function create(string|null $database = null, int|null $fetchSize = null, AccessMode|null $defaultAccessMode = null, array|null $bookmarks = null): self
{
return new self($database, $fetchSize, $defaultAccessMode, $bookmarks);
}
Expand All @@ -69,40 +61,34 @@ public static function default(): self

/**
* Creates a new session with the provided database.
*
* @param string|null $database
*/
public function withDatabase($database): self
public function withDatabase(?string $database): self
{
return new self($database, $this->fetchSize, $this->accessMode, $this->bookmarks);
}

/**
* Creates a new session with the provided fetch size.
*
* @param pure-callable():(int|null)|int|null $size
*/
public function withFetchSize($size): self
public function withFetchSize(?int $size): self
{
return new self($this->database, $size, $this->accessMode, $this->bookmarks);
}

/**
* Creates a new session with the provided access mode.
*
* @param pure-callable():(AccessMode|null)|AccessMode|null $defaultAccessMode
*/
public function withAccessMode($defaultAccessMode): self
public function withAccessMode(?AccessMode $defaultAccessMode): self
{
return new self($this->database, $this->fetchSize, $defaultAccessMode, $this->bookmarks);
}

/**
* Creates a new session with the provided bookmarks.
*
* @param pure-callable():(iterable<Bookmark>|null)|iterable<Bookmark>|null $bookmarks
* @param list<Bookmark>|null $bookmarks
*/
public function withBookmarks($bookmarks): self
public function withBookmarks(?array $bookmarks): self
{
return new self($this->database, $this->fetchSize, $this->accessMode, $bookmarks);
}
Expand All @@ -112,9 +98,7 @@ public function withBookmarks($bookmarks): self
*/
public function getFetchSize(): int
{
$fetchSize = is_callable($this->fetchSize) ? call_user_func($this->fetchSize) : $this->fetchSize;

return $fetchSize ?? self::DEFAULT_FETCH_SIZE;
return $this->fetchSize ?? self::DEFAULT_FETCH_SIZE;
}

/**
Expand All @@ -124,10 +108,7 @@ public function getFetchSize(): int
*/
public function getAccessMode(): AccessMode
{
$accessMode = is_callable($this->accessMode) ? call_user_func($this->accessMode) : $this->accessMode;

/** @psalm-suppress ImpureMethodCall */
return $accessMode ?? AccessMode::WRITE();
return $this->accessMode ?? AccessMode::WRITE();
}

/**
Expand All @@ -141,13 +122,11 @@ public function getDatabase(): ?string
/**
* Returns the initial bookmarks.
*
* @return iterable<Bookmark>
* @return list<Bookmark>
*/
public function getBookmarks(): iterable
public function getBookmarks(): array
{
$bookmarks = is_callable($this->bookmarks) ? call_user_func($this->bookmarks) : $this->bookmarks;

return $bookmarks ?? [];
return $this->bookmarks ?? [];
}

/**
Expand Down
38 changes: 13 additions & 25 deletions src/Databags/TransactionConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

namespace Laudis\Neo4j\Databags;

use function is_callable;

/**
* Configuration object for transactions.
*
Expand All @@ -26,21 +24,21 @@ final class TransactionConfiguration
public const DEFAULT_METADATA = '[]';

/**
* @param pure-callable():(float|null)|float|null $timeout timeout in seconds
* @param pure-callable():(iterable<string, scalar|array|null>|null)|iterable<string, scalar|array|null>|null $metaData
* @param float|null $timeout timeout in seconds
* @param iterable<string, scalar|array|null>|null $metaData
*/
public function __construct(
private $timeout = null,
private $metaData = null
private float|null $timeout = null,
private iterable|null $metaData = null
) {}

/**
* @pure
*
* @param pure-callable():(float|null)|float|null $timeout timeout in seconds
* @param pure-callable():(iterable<string, scalar|array|null>|null)|iterable<string, scalar|array|null>|null $metaData
* @param float|null $timeout timeout in seconds
* @param iterable<string, scalar|array|null>|null $metaData
*/
public static function create($timeout = null, $metaData = null): self
public static function create(float|null $timeout = null, iterable|null $metaData = null): self
{
return new self($timeout, $metaData);
}
Expand All @@ -60,43 +58,33 @@ public static function default(): self
*/
public function getMetaData(): ?iterable
{
$tbr = $this->metaData;
if (is_callable($tbr)) {
$tbr = $tbr();
}

return $tbr;
return $this->metaData;
}

/**
* Get the configured transaction timeout in seconds.
*/
public function getTimeout(): ?float
{
$tbr = $this->timeout;
if (is_callable($tbr)) {
$tbr = $tbr();
}

return $tbr;
return $this->timeout;
}

/**
* Creates a new transaction object with the provided timeout.
*
* @param pure-callable():(float|null)|float|null $timeout timeout in seconds
* @param float|null $timeout timeout in seconds
*/
public function withTimeout($timeout): self
public function withTimeout(float|null $timeout): self
{
return new self($timeout, $this->metaData);
}

/**
* Creates a new transaction object with the provided metadata.
*
* @param pure-callable():(iterable<string, scalar|array|null>|null)|iterable<string, scalar|array|null>|null $metaData
* @param iterable<string, scalar|array|null>|null $metaData
*/
public function withMetaData($metaData): self
public function withMetaData(iterable|null $metaData): self
{
return new self($this->timeout, $metaData);
}
Expand Down

0 comments on commit 61d3d74

Please sign in to comment.