Skip to content

Commit

Permalink
feat: run with new sync
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink committed Mar 3, 2025
1 parent 48be030 commit e56adb5
Show file tree
Hide file tree
Showing 23 changed files with 726 additions and 216 deletions.
28 changes: 20 additions & 8 deletions library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -951,16 +951,28 @@ private function setupExternalContent(): void
$this->wpService
))->createSources();

/**
* Sync external content.
*/
$this->wpService->addAction('Municipio/ExternalContent/Sync', function (string $postType, ?int $postId = null) use ($sourceConfigs) {
$sourceConfig = reset(array_filter($sourceConfigs, fn($config) => $config->getPostType() === $postType));
$sourceReader = (new \Municipio\ExternalContent\SourceReaders\Factories\SourceReaderFromConfig())->create($sourceConfig);
$wpPostArgsFromSchemaObject = (new \Municipio\ExternalContent\WpPostArgsFromSchemaObject\Factory\Factory($sourceConfig))->create();
$syncHandler = new \Municipio\ExternalContent\SyncHandler\SyncHandler($sourceReader, $wpPostArgsFromSchemaObject, $this->wpService);

$syncHandler->sync();
});

/**
* Start sync if event is triggered.
*/
$syncEventListener = new \Municipio\ExternalContent\Sync\SyncEventListener(
$sources,
$taxonomyItems,
$this->wpService,
$this->wpdb
);
$this->hooksRegistrar->register($syncEventListener);
// $syncEventListener = new \Municipio\ExternalContent\Sync\SyncEventListener(
// $sources,
// $taxonomyItems,
// $this->wpService,
// $this->wpdb
// );
// $this->hooksRegistrar->register($syncEventListener);

/**
* Only run the following if user is admin.
Expand Down Expand Up @@ -1031,7 +1043,7 @@ private function setupExternalContent(): void
* Trigger sync of external content.
*/
$triggerSync = new TriggerSync($this->wpService);
$triggerSync = new TriggerSyncIfNotInProgress(new PostTypeSyncInProgress($this->wpService), $triggerSync);
// $triggerSync = new TriggerSyncIfNotInProgress(new PostTypeSyncInProgress($this->wpService), $triggerSync);
$triggerSync = new \Municipio\ExternalContent\Sync\Triggers\TriggerSyncFromGetParams(
$this->wpService,
$triggerSync
Expand Down
8 changes: 8 additions & 0 deletions library/ExternalContent/Config/SourceConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,12 @@ public function getSourceTypesenseCollection(): string
{
return $this->sourceTypesenseCollection;
}

/**
* @inheritDoc
*/
public function getId(): string
{
return $this->postType;
}
}
197 changes: 62 additions & 135 deletions library/ExternalContent/Config/SourceConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,149 +65,76 @@ private function createSourceConfigsFromNamedSettings(array $namedSettings): Sou
isset($namedSettings['post_type'])
? ($this->schemaDataConfig->tryGetSchemaTypeFromPostType($namedSettings['post_type']) ?? "")
: '';
return new class ($namedSettings, $schemaType) implements SourceConfigInterface {
/**
* Constructor.
*/
public function __construct(
private array $namedSettings,
private string $schemaType
) {
}

/**
* @inheritDoc
*/
public function getPostType(): string
{
return $this->namedSettings['post_type'];
}

/**
* @inheritDoc
*/
public function getSchemaType(): string
{
return $this->schemaType;
}

/**
* @inheritDoc
*/
public function getAutomaticImportSchedule(): string
{
return $this->namedSettings['automatic_import_schedule'];
}

/**
* @inheritDoc
*/
public function getSourceType(): string
{
return $this->namedSettings['source_type'];
}

/**
* @inheritDoc
*/
public function getSourceJsonFilePath(): string
{
return $this->namedSettings['source_json_file_path'];
}

/**
* @inheritDoc
*/
public function getSourceTypesenseApiKey(): string
{
return $this->namedSettings['source_typesense_api_key'];
}
return new SourceConfig(
$namedSettings['post_type'] ?? '',
$namedSettings['automatic_import_schedule'] ?? '',
$schemaType,
$namedSettings['source_type'] ?? '',
$this->getArrayOfSourceTaxonomyConfigs($namedSettings['taxonomies']),
$namedSettings['source_json_file_path'] ?? '',
$namedSettings['source_typesense_api_key'] ?? '',
$namedSettings['source_typesense_protocol'] ?? '',
$namedSettings['source_typesense_host'] ?? '',
$namedSettings['source_typesense_port'] ?? '',
$namedSettings['source_typesense_collection'] ?? ''
);
}

/**
* @inheritDoc
*/
public function getSourceTypesenseProtocol(): string
{
return $this->namedSettings['source_typesense_protocol'];
}
/**
* Retrieves an array of source taxonomy configurations.
*
* @param array $taxonomies An array of taxonomies to get configurations for.
* @return array An array of source taxonomy configurations.
*/
private function getArrayOfSourceTaxonomyConfigs(array $taxonomies): array
{
if (empty($taxonomies)) {
return [];
}

/**
* @inheritDoc
*/
public function getSourceTypesenseHost(): string
{
return $this->namedSettings['source_typesense_host'];
}
return array_map(function ($taxonomy) {
return new class ($taxonomy) implements SourceTaxonomyConfigInterface {
/**
* Constructor.
*/
public function __construct(private array $taxonomy)
{
}

/**
* @inheritDoc
*/
public function getSourceTypesensePort(): string
{
return $this->namedSettings['source_typesense_port'];
}
/**
* @inheritDoc
*/
public function getFromSchemaProperty(): string
{
return $this->taxonomy['from_schema_property'];
}

/**
* @inheritDoc
*/
public function getSourceTypesenseCollection(): string
{
return $this->namedSettings['source_typesense_collection'];
}
/**
* @inheritDoc
*/
public function getSingularName(): string
{
return $this->taxonomy['singular_name'];
}

/**
* @inheritDoc
*/
public function getTaxonomies(): array
{
if (empty($this->namedSettings['taxonomies'])) {
return [];
/**
* @inheritDoc
*/
public function getName(): string
{
return $this->taxonomy['name'];
}

return array_map(function ($taxonomy) {
return new class ($taxonomy) implements SourceTaxonomyConfigInterface {
/**
* Constructor.
*/
public function __construct(private array $taxonomy)
{
}

/**
* @inheritDoc
*/
public function getFromSchemaProperty(): string
{
return $this->taxonomy['from_schema_property'];
}

/**
* @inheritDoc
*/
public function getSingularName(): string
{
return $this->taxonomy['singular_name'];
}

/**
* @inheritDoc
*/
public function getName(): string
{
return $this->taxonomy['name'];
}

/**
* @inheritDoc
*/
public function isHierarchical(): bool
{
return in_array($this->taxonomy['hierarchical'], [1, true, '1', 'true']);
}
};
}, $this->namedSettings['taxonomies']);
}
};
/**
* @inheritDoc
*/
public function isHierarchical(): bool
{
return in_array($this->taxonomy['hierarchical'], [1, true, '1', 'true']);
}
};
}, $taxonomies);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions library/ExternalContent/Config/SourceConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public function getSourceTypesensePort(): string;
* @return string
*/
public function getSourceTypesenseCollection(): string;

/**
* Get the source unique ID
*/
public function getId(): string;
}
Loading

0 comments on commit e56adb5

Please sign in to comment.