Skip to content

Commit

Permalink
NameSchemaService refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Aug 7, 2023
1 parent baee7f7 commit 7d09e3e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Event\NameSchema;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;

abstract class AbstractContentAwareNameSchemaEvent extends AbstractNameSchemaEvent
{
protected Content $content;

public function __construct(
array $schemaIdentifiers,
Content $content
) {
parent::__construct($schemaIdentifiers);
$this->content = $content;
}

public function getContent(): Content
{
return $this->content;
}
}
4 changes: 2 additions & 2 deletions src/contracts/Event/NameSchema/AbstractNameSchemaEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
abstract class AbstractNameSchemaEvent extends Event
{
/** @var array<string, array> */
private array $schemaIdentifiers;
protected array $schemaIdentifiers;

/**
* @var array<string, array<string>>
*/
private array $tokenValues = [];
protected array $tokenValues = [];

public function __construct(array $schemaIdentifiers)
{
Expand Down
18 changes: 1 addition & 17 deletions src/contracts/Event/NameSchema/ResolveContentNameSchemaEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@

namespace Ibexa\Contracts\Core\Event\NameSchema;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;

final class ResolveContentNameSchemaEvent extends AbstractNameSchemaEvent
final class ResolveContentNameSchemaEvent extends AbstractContentAwareNameSchemaEvent
{
private Content $content;

public function __construct(
array $schemaIdentifiers,
Content $content
) {
parent::__construct($schemaIdentifiers);
$this->content = $content;
}

public function getContent(): Content
{
return $this->content;
}
}
18 changes: 1 addition & 17 deletions src/contracts/Event/NameSchema/ResolveUrlAliasSchemaEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@

namespace Ibexa\Contracts\Core\Event\NameSchema;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;

final class ResolveUrlAliasSchemaEvent extends AbstractNameSchemaEvent
final class ResolveUrlAliasSchemaEvent extends AbstractContentAwareNameSchemaEvent
{
private Content $content;

public function __construct(
array $schemaIdentifiers,
Content $content
) {
parent::__construct($schemaIdentifiers);
$this->content = $content;
}

public function getContent(): Content
{
return $this->content;
}
}
93 changes: 38 additions & 55 deletions src/lib/Repository/NameSchema/NameSchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,16 @@ public function resolveUrlAliasSchema(Content $content, ContentType $contentType
{
$contentType ??= $content->getContentType();
$schemaName = $contentType->urlAliasSchema ?: $contentType->nameSchema;
[$filteredNameSchema, $groupLookupTable] = $this->filterNameSchema($schemaName);
$schemaIdentifiers = $this->schemaIdentifierExtractor->extract($schemaName);
$tokens = $this->extractTokens($filteredNameSchema);

/** @var \Ibexa\Contracts\Core\Event\ResolveUrlAliasSchemaEvent $event */
$event = $this->eventDispatcher->dispatch(
new ResolveUrlAliasSchemaEvent(
$schemaIdentifiers,
$content
)
);
$names = [];
$tokenValues = $event->getTokenValues();
foreach ($tokenValues as $languageCode => $tokenValue) {
$name = $filteredNameSchema;
foreach ($tokens as $token) {
$string = $this->resolveToken($token, $tokenValue, $groupLookupTable);
$name = str_replace($token, $string, $name);
}
$name = $this->validateNameLength($name);

$names[$languageCode] = $name;
}

return $names;
return $this->buildNames($event->getTokenValues(), $schemaName);
}

public function resolveNameSchema(
Expand All @@ -121,52 +106,29 @@ public function resolveNameSchema(
$schemaName = $contentType->urlAliasSchema ?: $contentType->nameSchema;
$schemaIdentifiers = $this->schemaIdentifierExtractor->extract($schemaName);

$event = new ResolveContentNameSchemaEvent($schemaIdentifiers, $content);
$this->eventDispatcher->dispatch($event);

[$filteredNameSchema, $groupLookupTable] = $this->filterNameSchema($schemaName);
$tokens = $this->extractTokens($filteredNameSchema);

$names = [];
$tokenValues = $event->getTokenValues();
foreach ($tokenValues as $languageCode => $tokenValue) {
$name = $filteredNameSchema;
foreach ($tokens as $token) {
$string = $this->resolveToken($token, $tokenValue, $groupLookupTable);
$name = str_replace($token, $string, $name);
}
$name = $this->validateNameLength($name);

$names[$languageCode] = $name;
}
$event = $this->eventDispatcher->dispatch(
new ResolveContentNameSchemaEvent(
$schemaIdentifiers,
$content
)
);

return $names;
return $this->buildNames($event->getTokenValues(), $schemaName);
}

public function resolve(string $nameSchema, ContentType $contentType, array $fieldMap, array $languageCodes): array
{
$schemaIdentifiers = $this->schemaIdentifierExtractor->extract($nameSchema);
$event = new ResolveNameSchemaEvent($schemaIdentifiers, $contentType, $fieldMap, $languageCodes);

$this->eventDispatcher->dispatch($event);
$tokenValues = $event->getTokenValues();

[$filteredNameSchema, $groupLookupTable] = $this->filterNameSchema($nameSchema);
$tokens = $this->extractTokens($filteredNameSchema);

$names = [];
foreach ($tokenValues as $languageCode => $tokenValue) {
$name = $filteredNameSchema;
foreach ($tokens as $token) {
$string = $this->resolveToken($token, $tokenValue, $groupLookupTable);
$name = str_replace($token, $string, $name);
}
$name = $this->validateNameLength($name);

$names[$languageCode] = $name;
}
$event = $this->eventDispatcher->dispatch(
new ResolveNameSchemaEvent(
$schemaIdentifiers,
$contentType,
$fieldMap,
$languageCodes
)
);

return $names;
return $this->buildNames($event->getTokenValues(), $nameSchema);
}

/**
Expand Down Expand Up @@ -300,6 +262,27 @@ protected function filterNameSchema(string $nameSchema): array
return [$nameSchema, $groupLookupTable];
}

/**
* @param array $tokenValues
*/
public function buildNames(array $tokenValues, string $nameSchema): array
{
[$filteredNameSchema, $groupLookupTable] = $this->filterNameSchema($nameSchema);
$tokens = $this->extractTokens($filteredNameSchema);

$names = [];
foreach ($tokenValues as $languageCode => $tokenValue) {
$name = $filteredNameSchema;
foreach ($tokens as $token) {
$string = $this->resolveToken($token, $tokenValue, $groupLookupTable);
$name = str_replace($token, $string, $name);
}
$names[$languageCode] = $this->validateNameLength($name);
}

return $names;
}

/**
* @return array<string>
*/
Expand Down

0 comments on commit 7d09e3e

Please sign in to comment.