Skip to content

Commit

Permalink
Add review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn committed Jul 29, 2021
1 parent 36a2274 commit 49c3604
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Factory\UserFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;

class AuthorDataMapper implements DataMapperInterface
{
/**
* @var UserFactoryInterface
* @var ContactFactoryInterface
*/
private $userFactory;
private $contactFactory;

public function __construct(UserFactoryInterface $userFactory)
public function __construct(ContactFactoryInterface $contactFactory)
{
$this->userFactory = $userFactory;
$this->contactFactory = $contactFactory;
}

public function map(
Expand Down Expand Up @@ -62,7 +62,7 @@ public function map(
private function setAuthorData(AuthorInterface $dimensionContent, array $data): void
{
if (isset($data['author'])) {
$dimensionContent->setAuthor($this->userFactory->create($data['author']));
$dimensionContent->setAuthor($this->contactFactory->create($data['author']));
}

if (isset($data['authored'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Sulu\Bundle\ContentBundle\Content\Domain\Factory;

use Sulu\Component\Security\Authentication\UserInterface;
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;

interface UserFactoryInterface
interface ContactFactoryInterface
{
public function create(?int $userId): ?UserInterface;
public function create(?int $contactId): ?ContactInterface;
}
6 changes: 3 additions & 3 deletions Content/Domain/Model/AuthorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

namespace Sulu\Bundle\ContentBundle\Content\Domain\Model;

use Sulu\Component\Security\Authentication\UserInterface;
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;

interface AuthorInterface
{
public function getAuthor(): ?UserInterface;
public function getAuthor(): ?ContactInterface;

public function setAuthor(?UserInterface $author): void;
public function setAuthor(?ContactInterface $author): void;

public function getAuthored(): ?\DateTimeImmutable;

Expand Down
8 changes: 4 additions & 4 deletions Content/Domain/Model/AuthorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

namespace Sulu\Bundle\ContentBundle\Content\Domain\Model;

use Sulu\Component\Security\Authentication\UserInterface;
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;

/**
* Basic implementation of the AuthorTrait.
*/
trait AuthorTrait
{
/**
* @var UserInterface|null
* @var ContactInterface|null
*/
private $author;

Expand All @@ -30,12 +30,12 @@ trait AuthorTrait
*/
private $authored;

public function getAuthor(): ?UserInterface
public function getAuthor(): ?ContactInterface
{
return $this->author;
}

public function setAuthor(?UserInterface $author): void
public function setAuthor(?ContactInterface $author): void
{
$this->author = $author;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\UserFactoryInterface;
use Sulu\Component\Security\Authentication\UserInterface;
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface;

class UserFactory implements UserFactoryInterface
class ContactFactory implements ContactFactoryInterface
{
/**
* @var EntityManagerInterface
Expand All @@ -29,18 +29,18 @@ public function __construct(EntityManagerInterface $entityManager)
$this->entityManager = $entityManager;
}

public function create(?int $userId): ?UserInterface
public function create(?int $contactId): ?ContactInterface
{
if (!$userId) {
if (!$contactId) {
return null;
}

/** @var UserInterface|null $user */
$user = $this->entityManager->getPartialReference(
UserInterface::class,
$userId
/** @var ContactInterface|null $contact */
$contact = $this->entityManager->getPartialReference(
ContactInterface::class,
$contactId
);

return $user;
return $contact;
}
}
21 changes: 19 additions & 2 deletions Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,26 @@ class ContentViewBuilderFactory implements ContentViewBuilderFactoryInterface
*/
private $securityChecker;

/**
* @var array<string, mixed[]>
*/
private $settingsForms;

/**
* @param array<string, mixed[]> $settingsForms
*/
public function __construct(
ViewBuilderFactoryInterface $viewBuilderFactory,
PreviewObjectProviderRegistryInterface $objectProviderRegistry,
ContentMetadataInspectorInterface $contentMetadataInspector,
SecurityCheckerInterface $securityChecker
SecurityCheckerInterface $securityChecker,
array $settingsForms
) {
$this->viewBuilderFactory = $viewBuilderFactory;
$this->objectProviderRegistry = $objectProviderRegistry;
$this->contentMetadataInspector = $contentMetadataInspector;
$this->securityChecker = $securityChecker;
$this->settingsForms = $settingsForms;
}

public function getDefaultToolbarActions(
Expand Down Expand Up @@ -286,8 +296,15 @@ private function createSettingsFormView(
array $toolbarActions,
string $dimensionContentClass
): ViewBuilderInterface {
$forms = [];
foreach ($this->settingsForms as $key => $tag) {
if (is_subclass_of($dimensionContentClass, $tag['instanceOf'])) {
$forms[] = $key;
}
}

return $this->createFormViewBuilder($parentView . '.settings', '/settings', $previewEnabled)
->addMetadataRequestParameters(['class' => $dimensionContentClass])
->addMetadataRequestParameters(['forms' => $forms])
->setResourceKey($resourceKey)
->setFormKey('content_settings')
->setTabTitle('sulu_page.settings')
Expand Down
42 changes: 14 additions & 28 deletions Content/Infrastructure/Sulu/Form/SettingsFormMetadataVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,35 @@

namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Form;

use Sulu\Bundle\AdminBundle\FormMetadata\FormXmlLoader;
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata;
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadataVisitorInterface;
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\LocalizedFormMetadataCollection;
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\XmlFormMetadataLoader;

class SettingsFormMetadataVisitor implements FormMetadataVisitorInterface
{
/**
* @var FormXmlLoader
* @var XmlFormMetadataLoader
*/
private $formXmlLoader;
private $xmlFormMetadataLoader;

/**
* @var array<string, mixed[]>
*/
private $formMappingTags;

/**
* @param array<string, mixed[]> $formMappingTags
*/
public function __construct(FormXmlLoader $formXmlLoader, array $formMappingTags)
public function __construct(XmlFormMetadataLoader $xmlFormMetadataLoader)
{
$this->formXmlLoader = $formXmlLoader;
$this->formMappingTags = $formMappingTags;
$this->xmlFormMetadataLoader = $xmlFormMetadataLoader;
}

public function visitFormMetadata(FormMetadata $formMetadata, string $locale, array $metadataOptions = []): void
{
if ('content_settings' === $formMetadata->getKey()) {
usort($this->formMappingTags, static function ($a, $b) {
return $b['priority'] <=> $a['priority'];
});

foreach ($this->formMappingTags as $tag) {
$class = $metadataOptions['class'];
if (is_subclass_of($class, $tag['instanceOf'])) {
/** @var LocalizedFormMetadataCollection $formMetadataCollection */
$formMetadataCollection = $this->formXmlLoader->load($tag['path']);
$settingsMetadata = $formMetadataCollection->getItems()[$locale];

$formMetadata->setItems(array_merge($formMetadata->getItems(), $settingsMetadata->getItems()));
$formMetadata->setSchema($formMetadata->getSchema()->merge($settingsMetadata->getSchema()));
foreach ($metadataOptions['forms'] ?? [] as $form) {
/** @var FormMetadata|null $subFormMetadata */
$subFormMetadata = $this->xmlFormMetadataLoader->getMetadata($form, $locale, $metadataOptions);

if (!$subFormMetadata || !($subFormMetadata instanceof FormMetadata)) {
continue;
}

$formMetadata->setItems(array_merge($formMetadata->getItems(), $subFormMetadata->getItems()));
$formMetadata->setSchema($formMetadata->getSchema()->merge($subFormMetadata->getSchema()));
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions DependencyInjection/Compiler/SettingsFormPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class SettingsFormPass implements CompilerPassInterface
public function process(ContainerBuilder $container): void
{
$formDirectories = $container->getParameter('sulu_admin.forms.directories');
$formMappingTags = [];

$finder = new Finder();
$finder->files()->in($formDirectories);
$settingsForms = [];
foreach ($finder as $file) {
$document = new \DOMDocument();

Expand All @@ -45,19 +45,24 @@ public function process(ContainerBuilder $container): void

/** @var \DOMElement $tagNode */
foreach ($tagNodes as $tagNode) {
$tag = [
'name' => $tagNode->getAttribute('name'),
'instanceOf' => $tagNode->getAttribute('instanceOf'),
'priority' => $tagNode->getAttribute('priority'),
'path' => $file->getPathname(),
];
$instanceOf = $tagNode->getAttribute('instanceOf');
$priority = $tagNode->getAttribute('priority');

if (!empty($tag['instanceOf'])) {
$formMappingTags[] = $tag;
if (empty($instanceOf)) {
continue;
}

$settingsForms[$tagNode->getAttribute('name')] = [
'instanceOf' => $instanceOf,
'priority' => $priority,
];
}
}

$container->setParameter('sulu_content.settings_mapping_tags', $formMappingTags);
uasort($settingsForms, static function ($a, $b) {
return $b['priority'] <=> $a['priority'];
});

$container->setParameter('sulu_content.settings_forms', $settingsForms);
}
}
2 changes: 1 addition & 1 deletion Resources/config/data-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</service>

<service id="sulu_content.author_data_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper\AuthorDataMapper">
<argument type="service" id="sulu_content.user_factory"/>
<argument type="service" id="sulu_content.contact_factory"/>

<tag name="sulu_content.data_mapper" priority="8"/>
</service>
Expand Down
3 changes: 1 addition & 2 deletions Resources/config/form-visitor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
<services>
<!-- Form Visitor -->
<service id="sulu_content.settings_author_visitor" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Form\SettingsFormMetadataVisitor">
<argument type="service" id="sulu_admin.form_metadata.form_xml_loader"/>
<argument>%sulu_content.settings_mapping_tags%</argument>
<argument type="service" id="sulu_admin.xml_form_metadata_loader"/>

<tag name="sulu_admin.form_metadata_visitor" />
</service>
Expand Down
8 changes: 4 additions & 4 deletions Resources/config/forms/content_settings_author.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
>
<key>content_settings_author</key>

<tag name="content_settings" instanceOf="Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface" priority="128"/>
<tag name="content_settings_author" instanceOf="Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface" priority="128"/>

<properties>
<section name="author">
<meta>
<title>sulu_page.editing_information</title>
<title>sulu_content.author</title>
</meta>
<properties>
<property name="authored" type="datetime" colspan="6">
<meta>
<title>sulu_page.authored_date</title>
<title>sulu_content.authored_date</title>
</meta>
</property>
<property name="author" type="single_contact_selection" colspan="6">
<meta>
<title>sulu_page.author</title>
<title>sulu_content.author</title>
</meta>
</property>
</properties>
Expand Down
5 changes: 3 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<argument type="service" id="sulu_preview.preview_object_provider_registry"/>
<argument type="service" id="sulu_content.content_metadata_inspector"/>
<argument type="service" id="sulu_security.security_checker"/>
<argument>%sulu_content.settings_forms%</argument>

<tag name="sulu.context" context="admin"/>
</service>
Expand All @@ -38,8 +39,8 @@
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>

<!-- UserFactory -->
<service id="sulu_content.user_factory" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\UserFactory">
<!-- ContactFactory -->
<service id="sulu_content.contact_factory" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\ContactFactory">
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>

Expand Down
4 changes: 3 additions & 1 deletion Resources/translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"sulu_content.seo": "SEO",
"sulu_content.excerpt": "Auszug & Taxonomien",
"sulu_content.content": "Inhalt",
"sulu_content.published": "Veröffentlicht am"
"sulu_content.published": "Veröffentlicht am",
"sulu_content.author": "Author",
"sulu_page.authored_date": "Authored Date"
}
4 changes: 3 additions & 1 deletion Resources/translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"sulu_content.seo": "SEO",
"sulu_content.excerpt": "Excerpt & Taxonomies",
"sulu_content.content": "Content",
"sulu_content.published": "Published on"
"sulu_content.published": "Published on",
"sulu_content.author": "Autor",
"sulu_content.authored_date": "Verfasst am"
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ protected function createContentViewBuilder(
new ViewBuilderFactory(),
$previewObjectProviderRegistry,
$contentMetadataInspector,
$securityChecker
$securityChecker,
[]
);
}

Expand Down

0 comments on commit 49c3604

Please sign in to comment.