Skip to content

Commit

Permalink
EZP-29613: As a developer I want access ContentType on Content to avo…
Browse files Browse the repository at this point in the history
…id re-loading it (#633)

* EZP-29613: As a developer I want access ContentType on Content to avoid re-loading it
* composer: bump kernel requirements to 7.4
  • Loading branch information
andrerom committed Dec 4, 2018
1 parent 819edb9 commit 0e07bc2
Show file tree
Hide file tree
Showing 22 changed files with 114 additions and 186 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"php": ">=7.1", "php": ">=7.1",
"symfony/symfony": "^3.4", "symfony/symfony": "^3.4",
"jms/translation-bundle": "^1.3.2", "jms/translation-bundle": "^1.3.2",
"ezsystems/ezpublish-kernel": "^7.3@dev", "ezsystems/ezpublish-kernel": "^7.4@dev",
"ezsystems/repository-forms": "^2.3@dev", "ezsystems/repository-forms": "^2.3@dev",
"ezsystems/ezplatform-admin-ui-modules": "^1.3@dev", "ezsystems/ezplatform-admin-ui-modules": "^1.3@dev",
"ezsystems/ez-support-tools": "^0.2@dev", "ezsystems/ez-support-tools": "^0.2@dev",
Expand Down
5 changes: 1 addition & 4 deletions src/bundle/Controller/ContentViewController.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ private function supplyPathLocations(ContentView $view): void
*/ */
private function supplyContentType(ContentView $view): void private function supplyContentType(ContentView $view): void
{ {
$content = $view->getContent(); $view->addParameters(['contentType' => $view->getContent()->getContentType()]);
$contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId, $this->siteAccessLanguages);

$view->addParameters(['contentType' => $contentType]);
} }


/** /**
Expand Down
14 changes: 3 additions & 11 deletions src/bundle/Controller/LocationController.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ public function moveAction(Request $request): Response
$location = $data->getLocation(); $location = $data->getLocation();
$newParentLocation = $data->getNewParentLocation(); $newParentLocation = $data->getNewParentLocation();


$newParentContentType = $this->contentTypeService->loadContentType( if (!$newParentLocation->getContent()->getContentType()->isContainer) {
$newParentLocation->getContentInfo()->contentTypeId
);

if (!$newParentContentType->isContainer) {
throw new InvalidArgumentException( throw new InvalidArgumentException(
'$newParentLocation', '$newParentLocation',
'Cannot move location to a parent that is not a container' 'Cannot move location to a parent that is not a container'
Expand Down Expand Up @@ -173,11 +169,7 @@ public function copyAction(Request $request): Response
$location = $data->getLocation(); $location = $data->getLocation();
$newParentLocation = $data->getNewParentLocation(); $newParentLocation = $data->getNewParentLocation();


$newParentContentType = $this->contentTypeService->loadContentType( if (!$newParentLocation->getContent()->getContentType()->isContainer) {
$newParentLocation->getContentInfo()->contentTypeId
);

if (!$newParentContentType->isContainer) {
throw new InvalidArgumentException( throw new InvalidArgumentException(
'$newParentLocation', '$newParentLocation',
'Cannot copy location to a parent that is not a container' 'Cannot copy location to a parent that is not a container'
Expand Down Expand Up @@ -283,7 +275,7 @@ public function swapAction(Request $request): Response
$newLocation = $data->getNewLocation(); $newLocation = $data->getNewLocation();


$childCount = $this->locationService->getLocationChildCount($currentLocation); $childCount = $this->locationService->getLocationChildCount($currentLocation);
$contentType = $this->contentTypeService->loadContentType($newLocation->getContentInfo()->contentTypeId); $contentType = $newLocation->getContent()->getContentType();


if (!$contentType->isContainer && $childCount) { if (!$contentType->isContainer && $childCount) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Controller/SectionController.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function viewSectionContentAction(Section $section, int $page = 1, int $l
$assignedContent[] = [ $assignedContent[] = [
'id' => $content->id, 'id' => $content->id,
'name' => $content->getName(), 'name' => $content->getName(),
'type' => $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId)->getName(), 'type' => $content->getContentType()->getName(),
'path' => $this->pathService->loadPathLocations( 'path' => $this->pathService->loadPathLocations(
$this->locationService->loadLocation($content->contentInfo->mainLocationId) $this->locationService->loadLocation($content->contentInfo->mainLocationId)
), ),
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Controller/TrashController.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function listAction(Request $request): Response


/** @var \eZ\Publish\API\Repository\Values\Content\TrashItem $item */ /** @var \eZ\Publish\API\Repository\Values\Content\TrashItem $item */
foreach ($pagerfanta->getCurrentPageResults() as $item) { foreach ($pagerfanta->getCurrentPageResults() as $item) {
$contentType = $this->contentTypeService->loadContentType($item->contentInfo->contentTypeId); $contentType = $item->getContent()->getContentType();
$ancestors = $this->uiPathService->loadPathLocations($item); $ancestors = $this->uiPathService->loadPathLocations($item);


$trashItemsList[] = new TrashItemData($item, $contentType, $ancestors); $trashItemsList[] = new TrashItemData($item, $contentType, $ancestors);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public function onFilterViewParameters(FilterViewParametersEvent $event)
} }


$contentInfo = $view->getContent()->contentInfo; $contentInfo = $view->getContent()->contentInfo;
$contentType = $this->contentTypeService->loadContentType( $contentType = $view->getContent()->getContentType();
$contentInfo->contentTypeId
);
$event->getParameterBag()->add([ $event->getParameterBag()->add([
'form' => $view->getFormView(), 'form' => $view->getFormView(),
'location' => $view->getLocation(), 'location' => $view->getLocation(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Menu/ContentCreateRightSidebarBuilder.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function createStructure(array $options): ItemInterface
$parentLocation = $options['parentLocation']; $parentLocation = $options['parentLocation'];
/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */ /** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */
$contentType = $options['content_type']; $contentType = $options['content_type'];
$parentContentType = $this->contentTypeService->loadContentType($parentLocation->contentInfo->contentTypeId); $parentContentType = $parentLocation->getContent()->getContentType();
/** @var \eZ\Publish\API\Repository\Values\Content\Language $language */ /** @var \eZ\Publish\API\Repository\Values\Content\Language $language */
$language = $options['language']; $language = $options['language'];
/** @var \Knp\Menu\ItemInterface|\Knp\Menu\ItemInterface[] $menu */ /** @var \Knp\Menu\ItemInterface|\Knp\Menu\ItemInterface[] $menu */
Expand Down
17 changes: 1 addition & 16 deletions src/lib/Specification/Location/IsContainer.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,28 +12,13 @@


class IsContainer extends AbstractSpecification class IsContainer extends AbstractSpecification
{ {
/** @var \eZ\Publish\API\Repository\ContentTypeService */
private $contentTypeService;

/**
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
*/
public function __construct($contentTypeService)
{
$this->contentTypeService = $contentTypeService;
}

/** /**
* @param \eZ\Publish\API\Repository\Values\Content\Location $item * @param \eZ\Publish\API\Repository\Values\Content\Location $item
* *
* @return bool * @return bool
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/ */
public function isSatisfiedBy($item): bool public function isSatisfiedBy($item): bool
{ {
return $this->contentTypeService->loadContentType( return $item->getContent()->getContentType()->isContainer;
$item->getContentInfo()->contentTypeId
)->isContainer;
} }
} }
4 changes: 2 additions & 2 deletions src/lib/Tab/Dashboard/PagerContentToDataMapper.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function map(Pagerfanta $pager): array
$data = []; $data = [];


foreach ($pager as $content) { foreach ($pager as $content) {
$contentInfo = $this->contentService->loadContentInfo($content->id); $contentInfo = $content->getVersionInfo()->getContentInfo();


$contributor = (new UserExists($this->userService))->isSatisfiedBy($contentInfo->ownerId) $contributor = (new UserExists($this->userService))->isSatisfiedBy($contentInfo->ownerId)
? $this->userService->loadUser($contentInfo->ownerId) : null; ? $this->userService->loadUser($contentInfo->ownerId) : null;
Expand All @@ -62,7 +62,7 @@ public function map(Pagerfanta $pager): array
'language' => $contentInfo->mainLanguageCode, 'language' => $contentInfo->mainLanguageCode,
'contributor' => $contributor, 'contributor' => $contributor,
'version' => $content->versionInfo->versionNo, 'version' => $content->versionInfo->versionNo,
'type' => $this->contentTypeService->loadContentType($contentInfo->contentTypeId)->getName(), 'type' => $content->getContentType()->getName(),
'modified' => $content->versionInfo->modificationDate, 'modified' => $content->versionInfo->modificationDate,
'initialLanguageCode' => $content->versionInfo->initialLanguageCode, 'initialLanguageCode' => $content->versionInfo->initialLanguageCode,
'content_is_user' => (new ContentIsUser($this->userService))->isSatisfiedBy($content), 'content_is_user' => (new ContentIsUser($this->userService))->isSatisfiedBy($content),
Expand Down
23 changes: 9 additions & 14 deletions src/lib/Tab/LocationView/RelationsTab.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -106,22 +106,17 @@ public function renderView(array $parameters): string
{ {
/** @var Content $content */ /** @var Content $content */
$content = $parameters['content']; $content = $parameters['content'];
$versionInfo = $content->getVersionInfo();
$relationsDataset = $this->datasetFactory->relations(); $relationsDataset = $this->datasetFactory->relations();
$relationsDataset->load($versionInfo); $relationsDataset->load($content);


$contentTypes = []; $contentTypeIds = [];


$relations = $relationsDataset->getRelations(); $relations = $relationsDataset->getRelations();


$viewParameters = []; $viewParameters = [];


foreach ($relations as $relation) { foreach ($relations as $relation) {
$contentTypeId = $relation->getDestinationContentInfo()->contentTypeId; $contentTypeIds[] = $relation->getDestinationContentInfo()->contentTypeId;

if (!isset($contentTypes[$contentTypeId])) {
$contentTypes[$contentTypeId] = $this->contentTypeService->loadContentType($contentTypeId);
}
} }


$viewParameters['relations'] = $relations; $viewParameters['relations'] = $relations;
Expand All @@ -130,17 +125,17 @@ public function renderView(array $parameters): string
$reverseRelations = $relationsDataset->getReverseRelations(); $reverseRelations = $relationsDataset->getReverseRelations();


foreach ($reverseRelations as $relation) { foreach ($reverseRelations as $relation) {
$contentTypeId = $relation->getSourceContentInfo()->contentTypeId; $contentTypeIds[] = $relation->getSourceContentInfo()->contentTypeId;

if (!isset($contentTypes[$contentTypeId])) {
$contentTypes[$contentTypeId] = $this->contentTypeService->loadContentType($contentTypeId);
}
} }


$viewParameters['reverse_relations'] = $reverseRelations; $viewParameters['reverse_relations'] = $reverseRelations;
} }


$viewParameters['contentTypes'] = $contentTypes; if (!empty($contentTypeIds)) {
$viewParameters['contentTypes'] = $this->contentTypeService->loadContentTypeList(array_unique($contentTypeIds));
} else {
$viewParameters['contentTypes'] = [];
}


return $this->twig->render( return $this->twig->render(
'@ezdesign/content/tab/relations/tab.html.twig', '@ezdesign/content/tab/relations/tab.html.twig',
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@


namespace EzSystems\EzPlatformAdminUi\Tests\Validator\Constraint; namespace EzSystems\EzPlatformAdminUi\Tests\Validator\Constraint;


use eZ\Publish\API\Repository\ContentTypeService; use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location; use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use EzSystems\EzPlatformAdminUi\Validator\Constraints\LocationIsContainer; use EzSystems\EzPlatformAdminUi\Validator\Constraints\LocationIsContainer;
Expand All @@ -19,9 +18,6 @@


class LocationIsContainerValidatorTest extends TestCase class LocationIsContainerValidatorTest extends TestCase
{ {
/** @var \eZ\Publish\API\Repository\ContentTypeService|\PHPUnit\Framework\MockObject\MockObject */
private $contentTypeService;

/** @var \Symfony\Component\Validator\Context\ExecutionContextInterface */ /** @var \Symfony\Component\Validator\Context\ExecutionContextInterface */
private $executionContext; private $executionContext;


Expand All @@ -31,29 +27,35 @@ class LocationIsContainerValidatorTest extends TestCase
/** @var \eZ\Publish\API\Repository\Values\Content\Location|\PHPUnit\Framework\MockObject\MockObject */ /** @var \eZ\Publish\API\Repository\Values\Content\Location|\PHPUnit\Framework\MockObject\MockObject */
private $location; private $location;


/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType|\PHPUnit\Framework\MockObject\MockObject */
private $contentType;

protected function setUp() protected function setUp()
{ {
$this->contentTypeService = $this->createMock(ContentTypeService::class);
$this->executionContext = $this->createMock(ExecutionContextInterface::class); $this->executionContext = $this->createMock(ExecutionContextInterface::class);
$this->validator = new LocationIsContainerValidator($this->contentTypeService); $this->validator = new LocationIsContainerValidator();
$this->validator->initialize($this->executionContext); $this->validator->initialize($this->executionContext);

$content = $this->createMock(Content::class);

$this->location = $this->createMock(Location::class); $this->location = $this->createMock(Location::class);
$this->location $this->location
->method('getContentInfo') ->method('getContent')
->willReturn( ->willReturn($content);
$this->createMock(ContentInfo::class)
); $this->contentType = $this->createMock(ContentType::class);

$content
->method('getContentType')
->willReturn($this->contentType);
} }


public function testValid() public function testValid()
{ {
$contentType = $this $this->contentType
->getMockBuilder(ContentType::class) ->method('__get')
->setMethodsExcept(['__get']) ->with('isContainer')
->setConstructorArgs([['isContainer' => true]]) ->willReturn(true);
->getMock();

$this->contentTypeService->method('loadContentType')->willReturn($contentType);


$this->executionContext $this->executionContext
->expects($this->never()) ->expects($this->never())
Expand All @@ -64,13 +66,10 @@ public function testValid()


public function testInvalid() public function testInvalid()
{ {
$contentType = $this $this->contentType
->getMockBuilder(ContentType::class) ->method('__get')
->setMethodsExcept(['__get']) ->with('isContainer')
->setConstructorArgs([['isContainer' => false]]) ->willReturn(false);
->getMock();

$this->contentTypeService->method('loadContentType')->willReturn($contentType);


$this->executionContext $this->executionContext
->expects($this->once()) ->expects($this->once())
Expand Down
7 changes: 1 addition & 6 deletions src/lib/UI/Config/Provider/User.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@ public function getConfig(): array
*/ */
private function resolveProfilePictureField(ApiUser $user): ?Field private function resolveProfilePictureField(ApiUser $user): ?Field
{ {
try { $contentType = $user->getContentType();
$contentType = $this->contentTypeService->loadContentType($user->contentInfo->contentTypeId);
} catch (\Exception $e) {
return null;
}

foreach ($user->getFields() as $field) { foreach ($user->getFields() as $field) {
$fieldDef = $contentType->getFieldDefinition($field->fieldDefIdentifier); $fieldDef = $contentType->getFieldDefinition($field->fieldDefIdentifier);


Expand Down
31 changes: 17 additions & 14 deletions src/lib/UI/Dataset/ContentDraftsDataset.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\LocationService; use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\VersionInfo; use eZ\Publish\API\Repository\Values\Content\VersionInfo;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\API\Repository\Values\User\User; use eZ\Publish\API\Repository\Values\User\User;
use EzSystems\EzPlatformAdminUi\UI\Value\Content\VersionId; use EzSystems\EzPlatformAdminUi\UI\Value\Content\VersionId;


Expand Down Expand Up @@ -60,16 +61,18 @@ public function load(User $user = null): self
} }


$contentDrafts = array_filter($contentDrafts, function (VersionInfo $version) { $contentDrafts = array_filter($contentDrafts, function (VersionInfo $version) {
$contentInfo = $version->getContentInfo(); // Filter out content that has been sent to trash
return !$version->getContentInfo()->isTrashed();
});


if (null === $contentInfo->mainLocationId) { $contentTypes = $contentTypeIds = [];
$locations = $this->locationService->loadParentLocationsForDraftContent($version); foreach ($contentDrafts as $contentDraft) {
// empty locations here means Location has been trashed and Draft should be ignored $contentTypeIds[] = $contentDraft->getContentInfo()->contentTypeId;
return !empty($locations); }
}


return true; if (!empty($contentTypeIds)) {
}); $contentTypes = $this->contentTypeService->loadContentTypeList(array_unique($contentTypeIds));
}


// ContentService::loadContentDrafts returns unsorted list of VersionInfo. // ContentService::loadContentDrafts returns unsorted list of VersionInfo.
// Sort results by modification date, descending. // Sort results by modification date, descending.
Expand All @@ -78,8 +81,11 @@ public function load(User $user = null): self
}); });


$this->data = array_map( $this->data = array_map(
function (VersionInfo $versionInfo) { function (VersionInfo $versionInfo) use ($contentTypes) {
return $this->mapContentDraft($versionInfo); return $this->mapContentDraft(
$versionInfo,
$contentTypes[$versionInfo->getContentInfo()->contentTypeId]
);
}, },
$contentDrafts $contentDrafts
); );
Expand All @@ -102,12 +108,9 @@ public function getContentDrafts(): array
* *
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/ */
private function mapContentDraft(VersionInfo $draft): array private function mapContentDraft(VersionInfo $draft, ContentType $contentType): array
{ {
$contentInfo = $draft->getContentInfo(); $contentInfo = $draft->getContentInfo();
$contentType = $this->contentTypeService->loadContentType(
$contentInfo->contentTypeId
);


return [ return [
'id' => new VersionId( 'id' => new VersionId(
Expand Down
4 changes: 2 additions & 2 deletions src/lib/UI/Dataset/PoliciesDataset.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public function __construct(
public function load(Location $location): self public function load(Location $location): self
{ {
$roleAssignments = []; $roleAssignments = [];
$content = $this->contentService->loadContentByContentInfo($location->contentInfo); $content = $location->getContent();
$contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId); $contentType = $content->getContentType();


if ((new ContentTypeIsUser($this->userContentTypeIdentifier))->isSatisfiedBy($contentType)) { if ((new ContentTypeIsUser($this->userContentTypeIdentifier))->isSatisfiedBy($contentType)) {
$user = $this->userService->loadUser($content->id); $user = $this->userService->loadUser($content->id);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/UI/Dataset/RelationsDataset.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


use eZ\Publish\API\Repository\ContentService; use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\Values\Content\VersionInfo; use eZ\Publish\API\Repository\Values\Content\Content;
use EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory; use EzSystems\EzPlatformAdminUi\UI\Value\ValueFactory;
use EzSystems\EzPlatformAdminUi\UI\Value as UIValue; use EzSystems\EzPlatformAdminUi\UI\Value as UIValue;


Expand Down Expand Up @@ -47,16 +47,16 @@ public function __construct(ContentService $contentService, ValueFactory $valueF
* *
* @throws UnauthorizedException * @throws UnauthorizedException
*/ */
public function load(VersionInfo $versionInfo): self public function load(Content $content): self
{ {
$contentInfo = $versionInfo->getContentInfo(); $versionInfo = $content->getVersionInfo();


foreach ($this->contentService->loadRelations($versionInfo) as $relation) { foreach ($this->contentService->loadRelations($versionInfo) as $relation) {
$this->relations[] = $this->valueFactory->createRelation($relation, $contentInfo); $this->relations[] = $this->valueFactory->createRelation($relation, $content);
} }


foreach ($this->contentService->loadReverseRelations($versionInfo->getContentInfo()) as $reverseRelation) { foreach ($this->contentService->loadReverseRelations($versionInfo->getContentInfo()) as $reverseRelation) {
$this->reverseRelations[] = $this->valueFactory->createRelation($reverseRelation, $contentInfo); $this->reverseRelations[] = $this->valueFactory->createRelation($reverseRelation, $content);
} }


return $this; return $this;
Expand Down
Loading

0 comments on commit 0e07bc2

Please sign in to comment.