Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor staging provider #315

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/staging-provider/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
parameters:
level: max
checkMissingIterableValueType: false
paths:
- src
- tests
ignoreErrors:
- identifier: missingType.iterableValue

includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
94 changes: 80 additions & 14 deletions libs/staging-provider/src/AbstractProviderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,108 @@
namespace Keboola\StagingProvider;

use Keboola\InputMapping\Staging\AbstractStrategyFactory;
use Keboola\InputMapping\Staging\ProviderInterface;
use Keboola\InputMapping\Staging\Scope;
use Keboola\StagingProvider\Provider\LocalStagingProvider;
use Keboola\StagingProvider\Staging\LocalStaging;
use Keboola\StagingProvider\Staging\Workspace\WorkspaceStagingInterface;
use Keboola\StagingProvider\WorkspaceProviderFactory\WorkspaceProviderFactoryInterface;

abstract class AbstractProviderInitializer
{
public function __construct(
private readonly AbstractStrategyFactory $stagingFactory,
private readonly WorkspaceProviderFactoryInterface $workspaceProviderFactory,
private readonly string $dataDirectory,
private readonly ProviderInterface $workspaceStagingProvider,
private readonly ProviderInterface $localStagingProvider,
odinuv marked this conversation as resolved.
Show resolved Hide resolved
pepamartinec marked this conversation as resolved.
Show resolved Hide resolved
) {
}

abstract public function initializeProviders(string $stagingType, array $tokenInfo): void;

/**
* @param class-string<WorkspaceStagingInterface> $workspaceClass
* @param array<string, Scope> $scopes
*/
protected function addWorkspaceProvider(string $workspaceClass, array $scopes): void
protected function addWorkspaceProvider(array $scopes): void
{
$stagingProvider = $this->workspaceProviderFactory->getProvider($workspaceClass);
$this->stagingFactory->addProvider($stagingProvider, $scopes);
$this->stagingFactory->addProvider($this->workspaceStagingProvider, $scopes);
}

/**
* @param array<string, Scope> $scopes
*/
protected function addLocalProvider(array $scopes): void
{
$stagingProvider = new LocalStagingProvider(function () {
return new LocalStaging($this->dataDirectory);
});
$this->stagingFactory->addProvider($this->localStagingProvider, $scopes);
}

protected function addWorkspaceProviders(string $stagingType, array $tokenInfo): void
{
if ($stagingType === AbstractStrategyFactory::WORKSPACE_REDSHIFT &&
$tokenInfo['owner']['hasRedshift']
) {
$this->addWorkspaceProvider(
[
AbstractStrategyFactory::WORKSPACE_REDSHIFT => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_SNOWFLAKE &&
$tokenInfo['owner']['hasSnowflake']
) {
$this->addWorkspaceProvider(
[
AbstractStrategyFactory::WORKSPACE_SNOWFLAKE => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_SYNAPSE &&
$tokenInfo['owner']['hasSynapse']
) {
$this->addWorkspaceProvider(
[
AbstractStrategyFactory::WORKSPACE_SYNAPSE => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_ABS &&
$tokenInfo['owner']['fileStorageProvider'] === 'azure'
) {
$this->addWorkspaceProvider(
[
AbstractStrategyFactory::WORKSPACE_ABS => new Scope(
[Scope::FILE_DATA, Scope::FILE_METADATA, Scope::TABLE_DATA],
),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_EXASOL &&
$tokenInfo['owner']['hasExasol']
) {
$this->addWorkspaceProvider(
[
AbstractStrategyFactory::WORKSPACE_EXASOL => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_TERADATA &&
$tokenInfo['owner']['hasTeradata']
) {
$this->addWorkspaceProvider(
[
AbstractStrategyFactory::WORKSPACE_TERADATA => new Scope([Scope::TABLE_DATA]),
],
);
}

$this->stagingFactory->addProvider($stagingProvider, $scopes);
if ($stagingType === AbstractStrategyFactory::WORKSPACE_BIGQUERY &&
$tokenInfo['owner']['hasBigquery']
) {
$this->addWorkspaceProvider(
[
AbstractStrategyFactory::WORKSPACE_BIGQUERY => new Scope([Scope::TABLE_DATA]),
],
);
}
}
}
92 changes: 3 additions & 89 deletions libs/staging-provider/src/InputProviderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,98 +6,12 @@

use Keboola\InputMapping\Staging\AbstractStrategyFactory;
use Keboola\InputMapping\Staging\Scope;
use Keboola\StagingProvider\Staging\Workspace\AbsWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\BigQueryWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\ExasolWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\RedshiftWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\SnowflakeWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\SynapseWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\TeradataWorkspaceStaging;

class InputProviderInitializer extends AbstractProviderInitializer
{
public function initializeProviders(
string $stagingType,
array $tokenInfo,
): void {
if ($stagingType === AbstractStrategyFactory::WORKSPACE_REDSHIFT &&
$tokenInfo['owner']['hasRedshift']
) {
$this->addWorkspaceProvider(
RedshiftWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_REDSHIFT => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_SNOWFLAKE &&
$tokenInfo['owner']['hasSnowflake']
) {
$this->addWorkspaceProvider(
SnowflakeWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_SNOWFLAKE => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_SYNAPSE &&
$tokenInfo['owner']['hasSynapse']
) {
$this->addWorkspaceProvider(
SynapseWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_SYNAPSE => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_ABS &&
$tokenInfo['owner']['fileStorageProvider'] === 'azure'
) {
$this->addWorkspaceProvider(
AbsWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_ABS => new Scope(
[Scope::FILE_DATA, Scope::FILE_METADATA, Scope::TABLE_DATA],
),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_EXASOL &&
$tokenInfo['owner']['hasExasol']
) {
$this->addWorkspaceProvider(
ExasolWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_EXASOL => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_TERADATA &&
$tokenInfo['owner']['hasTeradata']
) {
$this->addWorkspaceProvider(
TeradataWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_TERADATA => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_BIGQUERY &&
$tokenInfo['owner']['hasBigquery']
) {
$this->addWorkspaceProvider(
BigQueryWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_BIGQUERY => new Scope([Scope::TABLE_DATA]),
],
);
}
public function initializeProviders(string $stagingType, array $tokenInfo): void
{
$this->addWorkspaceProviders($stagingType, $tokenInfo);

$this->addLocalProvider(
[
Expand Down
104 changes: 9 additions & 95 deletions libs/staging-provider/src/OutputProviderInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,113 +5,27 @@
namespace Keboola\StagingProvider;

use Keboola\InputMapping\Staging\AbstractStrategyFactory;
use Keboola\InputMapping\Staging\ProviderInterface;
use Keboola\InputMapping\Staging\Scope;
use Keboola\OutputMapping\Staging\StrategyFactory as OutputStrategyFactory;
use Keboola\StagingProvider\Staging\Workspace\AbsWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\BigQueryWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\ExasolWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\RedshiftWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\SnowflakeWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\SynapseWorkspaceStaging;
use Keboola\StagingProvider\Staging\Workspace\TeradataWorkspaceStaging;
use Keboola\StagingProvider\WorkspaceProviderFactory\WorkspaceProviderFactoryInterface;

class OutputProviderInitializer extends AbstractProviderInitializer
{
public function __construct(
OutputStrategyFactory $stagingFactory,
WorkspaceProviderFactoryInterface $workspaceProviderFactory,
string $dataDirectory,
OutputStrategyFactory $stagingFactory, // This is just to make sure the correct StrategyFactory is injected
odinuv marked this conversation as resolved.
Show resolved Hide resolved
private readonly ProviderInterface $workspaceStagingProvider,
private readonly ProviderInterface $localStagingProvider,
) {
parent::__construct(
$stagingFactory,
$workspaceProviderFactory,
$dataDirectory,
$this->workspaceStagingProvider,
$this->localStagingProvider,
);
}

public function initializeProviders(
string $stagingType,
array $tokenInfo,
): void {
if ($stagingType === AbstractStrategyFactory::WORKSPACE_REDSHIFT &&
$tokenInfo['owner']['hasRedshift']
) {
$this->addWorkspaceProvider(
RedshiftWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_REDSHIFT => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_SNOWFLAKE &&
$tokenInfo['owner']['hasSnowflake']
) {
$this->addWorkspaceProvider(
SnowflakeWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_SNOWFLAKE => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_SYNAPSE &&
$tokenInfo['owner']['hasSynapse']
) {
$this->addWorkspaceProvider(
SynapseWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_SYNAPSE => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_ABS &&
$tokenInfo['owner']['fileStorageProvider'] === 'azure'
) {
$this->addWorkspaceProvider(
AbsWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_ABS => new Scope(
[Scope::FILE_DATA, Scope::FILE_METADATA, Scope::TABLE_DATA],
),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_EXASOL &&
$tokenInfo['owner']['hasExasol']
) {
$this->addWorkspaceProvider(
ExasolWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_EXASOL => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_TERADATA &&
$tokenInfo['owner']['hasTeradata']
) {
$this->addWorkspaceProvider(
TeradataWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_TERADATA => new Scope([Scope::TABLE_DATA]),
],
);
}

if ($stagingType === AbstractStrategyFactory::WORKSPACE_BIGQUERY &&
$tokenInfo['owner']['hasBigquery']
) {
$this->addWorkspaceProvider(
BigQueryWorkspaceStaging::class,
[
AbstractStrategyFactory::WORKSPACE_BIGQUERY => new Scope([Scope::TABLE_DATA]),
],
);
}
public function initializeProviders(string $stagingType, array $tokenInfo): void
{
$this->addWorkspaceProviders($stagingType, $tokenInfo);

$this->addLocalProvider(
[
Expand Down
Loading