Commit 5723081d authored by Mike Ryan's avatar Mike Ryan

#58: Final tweaks to factory injection.

parent 063867e9
Pipeline #59015052 passed with stage
in 2 minutes and 25 seconds
beeraccounts:
class: Soong\Task\EtlTask
configuration:
record_factory: Soong\Data\RecordFactory
key_map:
class: Soong\KeyMap\DBAL
configuration:
......@@ -16,7 +15,6 @@ beeraccounts:
extract:
class: Soong\Extractor\Csv
configuration:
record_factory: Soong\Data\RecordFactory
csv_file_path: data/migrate_example_beer_account.csv
key_properties:
aid:
......
beercontent:
class: Soong\Task\EtlTask
configuration:
record_factory: Soong\Data\RecordFactory
key_map:
class: Soong\KeyMap\DBAL
configuration:
......@@ -16,7 +15,6 @@ beercontent:
extract:
class: Soong\Extractor\Csv
configuration:
record_factory: Soong\Data\RecordFactory
csv_file_path: data/migrate_example_beer_content.csv
key_properties:
bid:
......
beertopics:
class: Soong\Task\EtlTask
configuration:
record_factory: Soong\Data\RecordFactory
key_map:
class: Soong\KeyMap\DBAL
configuration:
......@@ -16,7 +15,6 @@ beertopics:
extract:
class: Soong\Extractor\Csv
configuration:
record_factory: Soong\Data\RecordFactory
csv_file_path: data/migrate_example_beer_topic.csv
key_properties:
style:
......
arraytosql:
class: Soong\Task\EtlTask
configuration:
record_factory: Soong\Data\RecordFactory
key_map:
class: Soong\KeyMap\DBAL
configuration:
......@@ -16,7 +15,6 @@ arraytosql:
extract:
class: Soong\Extractor\ArrayExtractor
configuration:
#record_factory: Soong\Data\RecordFactory
key_properties:
id:
type: integer
......@@ -90,12 +88,10 @@ arraytosql:
sqltocsv:
class: Soong\Task\EtlTask
configuration:
record_factory: Soong\Data\RecordFactory
# Note we have no key_map - we are using this migration as an exporter.
extract:
class: Soong\Extractor\DBAL
configuration:
record_factory: Soong\Data\RecordFactory
connection:
# Replace with your test database credentials.
dbname: etltemp
......
......@@ -2,11 +2,9 @@
"sqltocsv_json": {
"class": "Soong\\Task\\EtlTask",
"configuration": {
"record_factory": "Soong\\Data\\RecordFactory",
"extract": {
"class": "Soong\\Extractor\\DBAL",
"configuration": {
"record_factory": "Soong\\Data\\RecordFactory",
"connection": {
"dbname": "etltemp",
"user": "root",
......
......@@ -3,11 +3,9 @@
<sqltocsv_xml>
<class>Soong\Task\EtlTask</class>
<configuration>
<record_factory>Soong\Data\RecordFactory</record_factory>
<extract>
<class>Soong\Extractor\DBAL</class>
<configuration>
<record_factory>Soong\Data\RecordFactory</record_factory>
<connection>
<dbname>etltemp</dbname>
<user>root</user>
......
......@@ -20,7 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The `--select` option has been added to the `migrate` command, allow for ad-hoc filtering of extracted data at runtime.
- `PropertyFactory` and `RecordFactory` interfaces/classes added for creation of `Property` and `Record` instances.
- Added basic console command tests.
- `property_factory` configuration option added to `EtlTask`.
- `property_factory` configuration option added to `EtlTask`, `LoaderBase`.
## [0.5.3] - 2019-04-12
......
......@@ -19,11 +19,11 @@ class RecordFactory implements \Soong\Contracts\Data\RecordFactory
/**
* Create a record factory instance using a property factory.
*
* @param \Soong\Contracts\Data\PropertyFactory|null $propertyFactory
* @param \Soong\Contracts\Data\PropertyFactory $propertyFactory
*/
public function __construct(\Soong\Contracts\Data\PropertyFactory $propertyFactory = null)
public function __construct(\Soong\Contracts\Data\PropertyFactory $propertyFactory)
{
$this->propertyFactory = $propertyFactory ?? new PropertyFactory();
$this->propertyFactory = $propertyFactory;
}
/**
......
......@@ -31,11 +31,8 @@ class Csv extends ExtractorBase
public function extractAll(): iterable
{
$csv = $this->loadCsv();
$recordFactoryClass = $this->getConfigurationValue('record_factory');
/** @var RecordFactory $recordFactory */
$recordFactory = new $recordFactoryClass;
foreach ($csv->getRecords() as $record) {
yield $recordFactory->create($record);
yield $this->getConfigurationValue('record_factory')->create($record);
}
}
......
......@@ -42,11 +42,8 @@ class DBAL extends CountableExtractorBase
// @todo: don't accept raw SQL from configuration
/** @var \Doctrine\DBAL\Driver\Statement $statement */
$statement = $this->connection()->executeQuery($this->getConfigurationValue('query'));
$recordFactoryClass = $this->getConfigurationValue('record_factory');
/** @var RecordFactory $recordFactory */
$recordFactory = new $recordFactoryClass;
while ($row = $statement->fetch(FetchMode::ASSOCIATIVE)) {
yield $recordFactory->create($row);
yield $this->getConfigurationValue('record_factory')->create($row);
}
} catch (DBALException $e) {
// @todo
......
......@@ -46,8 +46,10 @@ class DBAL extends LoaderBase
$id = $this->connection()->lastInsertId();
if ($id) {
$keyKeys = array_keys($this->getKeyProperties());
$data->setProperty(reset($keyKeys),
$this->getConfigurationValue('property_factory')->create($id));
$data->setProperty(
reset($keyKeys),
$this->getConfigurationValue('property_factory')->create($id)
);
}
} catch (DBALException $e) {
print $e->getMessage();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment