From 8b1bef25e947c7dd50e777f1a3d07b541d21c4d1 Mon Sep 17 00:00:00 2001 From: Ryan Leeson Date: Tue, 6 Dec 2022 18:21:38 -0500 Subject: [PATCH] Rework Stream readers and Sets to track stream properties --- src/Model/Data/IStream.php | 15 ++++ src/Model/Data/IStreamCollection.php | 19 +++++ src/Model/Data/IStreamProperties.php | 33 +++++++++ src/Model/Data/Stream.php | 36 ++++++++++ src/Model/Data/StreamCollection.php | 37 ++++++++++ src/Model/Data/StreamProperties.php | 71 +++++++++++++++++++ src/Repositories/ISetStream.php | 10 +-- src/Repositories/IStreamReader.php | 5 +- src/Repositories/JsonSetStream.php | 9 ++- src/Repositories/LocalFileReader.php | 19 ++++- src/Services/External/ExternalJsonSet.php | 49 ------------- src/Services/External/ExternalStreamSet.php | 38 ++++++++-- .../External/IExternalStreamReader.php | 4 +- 13 files changed, 277 insertions(+), 68 deletions(-) create mode 100644 src/Model/Data/IStream.php create mode 100644 src/Model/Data/IStreamCollection.php create mode 100644 src/Model/Data/IStreamProperties.php create mode 100644 src/Model/Data/Stream.php create mode 100644 src/Model/Data/StreamCollection.php create mode 100644 src/Model/Data/StreamProperties.php delete mode 100644 src/Services/External/ExternalJsonSet.php diff --git a/src/Model/Data/IStream.php b/src/Model/Data/IStream.php new file mode 100644 index 0000000..1eade39 --- /dev/null +++ b/src/Model/Data/IStream.php @@ -0,0 +1,15 @@ +_properties; + } + + /** + * @inheritDoc + */ + function stream(): string { + return $this->_stream; + } +} \ No newline at end of file diff --git a/src/Model/Data/StreamCollection.php b/src/Model/Data/StreamCollection.php new file mode 100644 index 0000000..1c0970f --- /dev/null +++ b/src/Model/Data/StreamCollection.php @@ -0,0 +1,37 @@ +_collection = $_collection; + $this->_stream = $_stream; + } + + /** + * @inheritDoc + */ + function collection(): iterable { + return $this->_collection; + } + + /** + * @inheritDoc + */ + function stream(): IStream { + return $this->_stream; + } +} \ No newline at end of file diff --git a/src/Model/Data/StreamProperties.php b/src/Model/Data/StreamProperties.php new file mode 100644 index 0000000..00c1f94 --- /dev/null +++ b/src/Model/Data/StreamProperties.php @@ -0,0 +1,71 @@ +_length = $_length; + $this->_lastModifiedTimestamp = $_lastModifiedTimestamp; + $this->_readTimestamp = $_readTimestamp; + $this->_uri = $_uri; + } + + /** + * @inheritDoc + */ + function lastModifiedTimestamp(): int { + return $this->_lastModifiedTimestamp; + } + + /** + * @inheritDoc + */ + function length(): int { + return $this->_length; + } + + /** + * @inheritDoc + */ + function readTimestamp(): int { + return $this->_readTimestamp; + } + + /** + * @inheritDoc + */ + function uri(): string { + return $this->_uri; + } +} \ No newline at end of file diff --git a/src/Repositories/ISetStream.php b/src/Repositories/ISetStream.php index ceef949..7ac94b8 100644 --- a/src/Repositories/ISetStream.php +++ b/src/Repositories/ISetStream.php @@ -1,21 +1,23 @@ process( $_input_stream ?? '' ); + public function read( IStream $_input_stream ): IStreamCollection { + $output = $this->process( $_input_stream->stream() ?? '' ); $error_code = max( 0, json_last_error() ); if ( 0 < $error_code ) { @@ -51,6 +54,6 @@ public function read( string $_input_stream ): iterable { self::ERROR_REFERENCE[ $error_code ] ?? "Unknown JSON read error ($error_code)" ); } - return $output; + return new StreamCollection( $output, $_input_stream ); } } \ No newline at end of file diff --git a/src/Repositories/LocalFileReader.php b/src/Repositories/LocalFileReader.php index e262b34..4221b88 100644 --- a/src/Repositories/LocalFileReader.php +++ b/src/Repositories/LocalFileReader.php @@ -6,13 +6,17 @@ namespace Kanopi\Components\Repositories; use InvalidArgumentException; +use Kanopi\Components\Model\Data\IStream; +use Kanopi\Components\Model\Data\Stream; +use Kanopi\Components\Model\Data\StreamProperties; class LocalFileReader implements IStreamReader { /** * @inheritDoc */ - function read( string $_stream_path ): string { - if ( false === file_exists( $_stream_path ) ) { + function read( string $_stream_path ): IStream { + $lastModifiedTimestamp = filemtime( $_stream_path ); + if ( false === $lastModifiedTimestamp ) { throw new InvalidArgumentException( "Import file not found at: $_stream_path", 'Import file does not exists' ); @@ -20,7 +24,16 @@ function read( string $_stream_path ): string { // phpcs:ignore -- File read is intentionally uncached, intended for singular read $contents = file_get_contents( $_stream_path ); + $readContents = !empty( $contents ) ? $contents : ''; - return !empty( $contents ) ? $contents : ''; + return new Stream( + $readContents, + new StreamProperties( + $_stream_path, + $lastModifiedTimestamp, + strlen( $readContents ), + time() + ) + ); } } \ No newline at end of file diff --git a/src/Services/External/ExternalJsonSet.php b/src/Services/External/ExternalJsonSet.php deleted file mode 100644 index 2fa9853..0000000 --- a/src/Services/External/ExternalJsonSet.php +++ /dev/null @@ -1,49 +0,0 @@ -file_reader = $_local_file_reader; - $this->stream_reader = $_json_stream_reader; - } - - /** - * @inheritDoc - */ - function readStream( string $_stream_path, IEntitySet $_transform ): void { - $stream = $this->file_reader->read( $_stream_path ); - $json_set = $this->stream_reader->transform( $stream ); - $this->entities = $_transform->transform( $json_set ); - } -} \ No newline at end of file diff --git a/src/Services/External/ExternalStreamSet.php b/src/Services/External/ExternalStreamSet.php index dd3b5d1..56948b6 100644 --- a/src/Services/External/ExternalStreamSet.php +++ b/src/Services/External/ExternalStreamSet.php @@ -3,21 +3,47 @@ namespace Kanopi\Components\Services\External; use Kanopi\Components\Model\Data\Entities; +use Kanopi\Components\Model\Data\IStreamProperties; use Kanopi\Components\Model\Transform\IEntitySet; use Kanopi\Components\Repositories\ISetStream; +use Kanopi\Components\Repositories\IStreamReader; class ExternalStreamSet implements IExternalStreamReader { + /** + * Entity set holds the set of read, transformed external data models + */ use Entities; - protected ISetStream $stream_repository; + /** + * @var IStreamReader + */ + protected IStreamReader $stream_reader; - public function __construct( ISetStream $_stream_repository ) { - $this->stream_repository = $_stream_repository; + /** + * @var ISetStream + */ + protected ISetStream $set_reader; + + /** + * @param IStreamReader $_stream_reader + * @param ISetStream $_set_reader + */ + public function __construct( + IStreamReader $_stream_reader, + ISetStream $_set_reader + ) { + $this->stream_reader = $_stream_reader; + $this->set_reader = $_set_reader; } /** * @inheritDoc */ - function readStream( string $_stream_path, IEntitySet $_transform ): void { - $this->entities = $_transform->transform( $this->stream_repository->read( $_stream_path ) ); - }} \ No newline at end of file + function readStream( string $_stream_path, IEntitySet $_transform ): IStreamProperties { + $stream = $this->stream_reader->read( $_stream_path ); + $streamCollection = $this->set_reader->read( $stream ); + $this->entities = $_transform->transform( $streamCollection->collection() ); + + return $stream->properties(); + } +} \ No newline at end of file diff --git a/src/Services/External/IExternalStreamReader.php b/src/Services/External/IExternalStreamReader.php index b6d50b3..244ba92 100644 --- a/src/Services/External/IExternalStreamReader.php +++ b/src/Services/External/IExternalStreamReader.php @@ -2,6 +2,7 @@ namespace Kanopi\Components\Services\External; +use Kanopi\Components\Model\Data\IStreamProperties; use Kanopi\Components\Model\Exception\SetStreamException; use Kanopi\Components\Model\Transform\IEntitySet; use Kanopi\Components\Services\IIndexedEntityReader; @@ -13,7 +14,8 @@ interface IExternalStreamReader extends IIndexedEntityReader { * @param string $_stream_path * @param IEntitySet $_transform * + * @returns IStreamProperties * @throws SetStreamException */ - function readStream( string $_stream_path, IEntitySet $_transform ): void; + function readStream( string $_stream_path, IEntitySet $_transform ): IStreamProperties; } \ No newline at end of file