Skip to content

Commit

Permalink
Merge branch 'b-7.2.x-create-data-collectors-OXDEV-8724' into b-7.2.x…
Browse files Browse the repository at this point in the history
…-create-aggregate-service-OXDEV-8726
  • Loading branch information
RahatHameed committed Sep 23, 2024
2 parents eee2996 + a8ca830 commit 7c98023
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ abstract public function getTableName(): string;

abstract protected function getColumnName(): string;

public function collect(string $id): array
public function collect(string $recordId): array
{
$orderData = $this->repository->getDataFromTable(
table: $this->getTableName(),
columnName: $this->getColumnName(),
columnValue: $id
columnValue: $recordId
);

$relatedData = [];
Expand All @@ -39,8 +39,8 @@ public function collect(string $id): array
$collector->collectRelatedData(
primaryTable: $this->getTableName(),
primaryKey: $this->getColumnName(),
primaryConditionColumn: $this->getColumnName(),
primaryConditionValue: $id
primaryColumn: $this->getColumnName(),
primaryValue: $recordId
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ abstract protected function getColumnName(): string;
public function collectRelatedData(
string $primaryTable,
string $primaryKey,
string $primaryConditionColumn,
string $primaryConditionValue
string $primaryColumn,
string $primaryValue
): array {
return $this->repository->getJoinedData(
$primaryTable,
$primaryKey,
$this->getTableName(),
$this->getColumnName(),
$primaryConditionColumn,
$primaryConditionValue
$primaryColumn,
$primaryValue
);
}
}
4 changes: 2 additions & 2 deletions src/UserData/Application/AbstractTableDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ abstract public function getTableName(): string;

abstract protected function getColumnName(): string;

public function collect(string $id): array
public function collect(string $recordId): array
{
return $this->repository->getDataFromTable($this->getTableName(), $this->getColumnName(), $id);
return $this->repository->getDataFromTable($this->getTableName(), $this->getColumnName(), $recordId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ interface RelatedTableDataCollectorInterface
public function collectRelatedData(
string $primaryTable,
string $primaryKey,
string $primaryConditionColumn,
string $primaryConditionValue
string $primaryColumn,
string $primaryValue
): array;

public function getTableName(): string;
Expand Down
2 changes: 1 addition & 1 deletion src/UserData/Application/TableDataCollectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

interface TableDataCollectorInterface
{
public function collect(string $id): array;
public function collect(string $recordId): array;
public function getTableName(): string;
}
19 changes: 13 additions & 6 deletions src/UserData/Infrastructure/UserDataRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OxidEsales\GdprOptinModule\UserData\Infrastructure;

use Doctrine\DBAL\ForwardCompatibility\Result;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;

class UserDataRepository implements UserDataRepositoryInterface
Expand All @@ -26,24 +27,30 @@ public function getDataFromTable(string $table, string $columnName, string $colu
->where($columnName . ' = :' . $columnName)
->setParameter($columnName, $columnValue);

return $queryBuilder->execute()->fetchAllAssociative();
/** @var Result $result */
$result = $queryBuilder->execute();

return $result->fetchAllAssociative();
}

public function getJoinedData(
string $primaryTable,
string $primaryKey,
string $foreignTable,
string $foreignKey,
string $primaryConditionColumn,
string $primaryConditionValue
string $primaryColumn,
string $primaryValue
): array {
$queryBuilder = $this->queryBuilderFactory->create();
$queryBuilder->select('*')
->from($primaryTable)
->innerJoin($primaryTable, $foreignTable, 'ft', "ft.{$foreignKey} = {$primaryTable}.{$primaryKey}")
->where($primaryTable . '.' . $primaryConditionColumn . ' = :' . $primaryConditionColumn)
->setParameter($primaryConditionColumn, $primaryConditionValue);
->where($primaryTable . '.' . $primaryColumn . ' = :' . $primaryColumn)
->setParameter($primaryColumn, $primaryValue);

/** @var Result $result */
$result = $queryBuilder->execute();

return $queryBuilder->execute()->fetchAllAssociative();
return $result->fetchAllAssociative();
}
}
4 changes: 2 additions & 2 deletions src/UserData/Infrastructure/UserDataRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function getJoinedData(
string $primaryKey,
string $foreignTable,
string $foreignKey,
string $primaryConditionColumn,
string $primaryConditionValue
string $primaryColumn,
string $primaryValue
): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ protected function getColumnName(): string
$result = $sut->collectRelatedData(
primaryTable: self::PRIMARY_TABLE,
primaryKey: self::PRIMARY_KEY,
primaryConditionColumn: self::PRIMARY_CONDITION_COLUMN,
primaryConditionValue: self::PRIMARY_CONDITION_VALUE,
primaryColumn: self::PRIMARY_CONDITION_COLUMN,
primaryValue: self::PRIMARY_CONDITION_VALUE,
);
$this->assertSame($expectedData, $result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ final class AbstractTableDataCollectorTest extends BaseTestCase

public function testCollectReturnsExpectedTableData(): void
{
$userId = uniqid();
$recordId = uniqid();
$expectedData = $this->expectedUserData();
$userDataRepositoryMock = $this->createUserDataRepositoryMock(
expectedUserData: $expectedData,
method: 'getDataFromTable',
withParams: [static::TABLE_NAME, static::COLUMN_NAME, $userId],
withParams: [static::TABLE_NAME, static::COLUMN_NAME, $recordId],
);

$sut = new class ($userDataRepositoryMock) extends AbstractTableDataCollector {
Expand All @@ -42,7 +42,7 @@ protected function getColumnName(): string
}
};

$result = $sut->collect($userId);
$result = $sut->collect(recordId: $recordId);
$this->assertSame($expectedData, $result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testCollectReturnsPrimaryTableDataAndRelatedData(): void
collectors: [$relatedTableCollectorMock]
);

$actualResult = $sut->collect(id: static::PRIMARY_COLUMN_VALUE);
$actualResult = $sut->collect(recordId: static::PRIMARY_COLUMN_VALUE);

$expectedResult = [
static::PRIMARY_TABLE => $expectedPrimaryTableData,
Expand Down Expand Up @@ -90,7 +90,7 @@ public function testCollectReturnsEmptyRelatedDataIfNoPrimaryTableData(): void
collectors: [$relatedTableCollectorMock]
);

$actualResult = $sut->collect(id: static::PRIMARY_COLUMN_VALUE);
$actualResult = $sut->collect(recordId: static::PRIMARY_COLUMN_VALUE);

$expectedResult = [
static::PRIMARY_TABLE => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testCollectReturnsExpectedTableData(): void
);

$sut = new $class($userDataRepositoryMock);
$actualResult = $sut->collect($columnValue);
$actualResult = $sut->collect(recordId: $columnValue);

$this->assertCount(2, $expectedUserData);
$this->assertSame($expectedUserData, $actualResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public function testCollectReturnsExpectedUserData(): void
$actualResult = $sut->collectRelatedData(
primaryTable: static::PRIMARY_TABLE,
primaryKey: static::PRIMARY_KEY,
primaryConditionColumn: static::PRIMARY_CONDITION_COLUMN,
primaryConditionValue: static::PRIMARY_CONDITION_VALUE,
primaryColumn: static::PRIMARY_CONDITION_COLUMN,
primaryValue: static::PRIMARY_CONDITION_VALUE,
);

$this->assertCount(2, $expectedUserData);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/UserData/Infrastructure/UserDataRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function testGetJoinedData(): void
primaryKey: 'primaryKey',
foreignTable: 'foreignTable',
foreignKey: 'foreignKey',
primaryConditionColumn: 'primaryKey',
primaryConditionValue: uniqid()
primaryColumn: 'primaryKey',
primaryValue: uniqid()
);

$this->assertSame($expectedData, $actualResult);
Expand Down

0 comments on commit 7c98023

Please sign in to comment.