Skip to content
Permalink
Browse files

EZP-29613: As a developer I want access ContentType on Content to avo…

…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 0e07bc2fc936a4a50d1941a736c30d7bfb0b9fe4
@@ -16,7 +16,7 @@
"php": ">=7.1",
"symfony/symfony": "^3.4",
"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/ezplatform-admin-ui-modules": "^1.3@dev",
"ezsystems/ez-support-tools": "^0.2@dev",
@@ -208,10 +208,7 @@ private function supplyPathLocations(ContentView $view): void
*/
private function supplyContentType(ContentView $view): void
{
$content = $view->getContent();
$contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId, $this->siteAccessLanguages);
$view->addParameters(['contentType' => $contentType]);
$view->addParameters(['contentType' => $view->getContent()->getContentType()]);
}
/**
@@ -117,11 +117,7 @@ public function moveAction(Request $request): Response
$location = $data->getLocation();
$newParentLocation = $data->getNewParentLocation();
$newParentContentType = $this->contentTypeService->loadContentType(
$newParentLocation->getContentInfo()->contentTypeId
);
if (!$newParentContentType->isContainer) {
if (!$newParentLocation->getContent()->getContentType()->isContainer) {
throw new InvalidArgumentException(
'$newParentLocation',
'Cannot move location to a parent that is not a container'
@@ -173,11 +169,7 @@ public function copyAction(Request $request): Response
$location = $data->getLocation();
$newParentLocation = $data->getNewParentLocation();
$newParentContentType = $this->contentTypeService->loadContentType(
$newParentLocation->getContentInfo()->contentTypeId
);
if (!$newParentContentType->isContainer) {
if (!$newParentLocation->getContent()->getContentType()->isContainer) {
throw new InvalidArgumentException(
'$newParentLocation',
'Cannot copy location to a parent that is not a container'
@@ -283,7 +275,7 @@ public function swapAction(Request $request): Response
$newLocation = $data->getNewLocation();
$childCount = $this->locationService->getLocationChildCount($currentLocation);
$contentType = $this->contentTypeService->loadContentType($newLocation->getContentInfo()->contentTypeId);
$contentType = $newLocation->getContent()->getContentType();
if (!$contentType->isContainer && $childCount) {
throw new \InvalidArgumentException(
@@ -244,7 +244,7 @@ public function viewSectionContentAction(Section $section, int $page = 1, int $l
$assignedContent[] = [
'id' => $content->id,
'name' => $content->getName(),
'type' => $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId)->getName(),
'type' => $content->getContentType()->getName(),
'path' => $this->pathService->loadPathLocations(
$this->locationService->loadLocation($content->contentInfo->mainLocationId)
),
@@ -145,7 +145,7 @@ public function listAction(Request $request): Response
/** @var \eZ\Publish\API\Repository\Values\Content\TrashItem $item */
foreach ($pagerfanta->getCurrentPageResults() as $item) {
$contentType = $this->contentTypeService->loadContentType($item->contentInfo->contentTypeId);
$contentType = $item->getContent()->getContentType();
$ancestors = $this->uiPathService->loadPathLocations($item);
$trashItemsList[] = new TrashItemData($item, $contentType, $ancestors);
@@ -53,9 +53,8 @@ public function onFilterViewParameters(FilterViewParametersEvent $event)
}
$contentInfo = $view->getContent()->contentInfo;
$contentType = $this->contentTypeService->loadContentType(
$contentInfo->contentTypeId
);
$contentType = $view->getContent()->getContentType();
$event->getParameterBag()->add([
'form' => $view->getFormView(),
'location' => $view->getLocation(),
@@ -91,7 +91,7 @@ public function createStructure(array $options): ItemInterface
$parentLocation = $options['parentLocation'];
/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType */
$contentType = $options['content_type'];
$parentContentType = $this->contentTypeService->loadContentType($parentLocation->contentInfo->contentTypeId);
$parentContentType = $parentLocation->getContent()->getContentType();
/** @var \eZ\Publish\API\Repository\Values\Content\Language $language */
$language = $options['language'];
/** @var \Knp\Menu\ItemInterface|\Knp\Menu\ItemInterface[] $menu */
@@ -12,28 +12,13 @@
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
*
* @return bool
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function isSatisfiedBy($item): bool
{
return $this->contentTypeService->loadContentType(
$item->getContentInfo()->contentTypeId
)->isContainer;
return $item->getContent()->getContentType()->isContainer;
}
}
@@ -51,7 +51,7 @@ public function map(Pagerfanta $pager): array
$data = [];
foreach ($pager as $content) {
$contentInfo = $this->contentService->loadContentInfo($content->id);
$contentInfo = $content->getVersionInfo()->getContentInfo();
$contributor = (new UserExists($this->userService))->isSatisfiedBy($contentInfo->ownerId)
? $this->userService->loadUser($contentInfo->ownerId) : null;
@@ -62,7 +62,7 @@ public function map(Pagerfanta $pager): array
'language' => $contentInfo->mainLanguageCode,
'contributor' => $contributor,
'version' => $content->versionInfo->versionNo,
'type' => $this->contentTypeService->loadContentType($contentInfo->contentTypeId)->getName(),
'type' => $content->getContentType()->getName(),
'modified' => $content->versionInfo->modificationDate,
'initialLanguageCode' => $content->versionInfo->initialLanguageCode,
'content_is_user' => (new ContentIsUser($this->userService))->isSatisfiedBy($content),
@@ -106,22 +106,17 @@ public function renderView(array $parameters): string
{
/** @var Content $content */
$content = $parameters['content'];
$versionInfo = $content->getVersionInfo();
$relationsDataset = $this->datasetFactory->relations();
$relationsDataset->load($versionInfo);
$relationsDataset->load($content);
$contentTypes = [];
$contentTypeIds = [];
$relations = $relationsDataset->getRelations();
$viewParameters = [];
foreach ($relations as $relation) {
$contentTypeId = $relation->getDestinationContentInfo()->contentTypeId;
if (!isset($contentTypes[$contentTypeId])) {
$contentTypes[$contentTypeId] = $this->contentTypeService->loadContentType($contentTypeId);
}
$contentTypeIds[] = $relation->getDestinationContentInfo()->contentTypeId;
}
$viewParameters['relations'] = $relations;
@@ -130,17 +125,17 @@ public function renderView(array $parameters): string
$reverseRelations = $relationsDataset->getReverseRelations();
foreach ($reverseRelations as $relation) {
$contentTypeId = $relation->getSourceContentInfo()->contentTypeId;
if (!isset($contentTypes[$contentTypeId])) {
$contentTypes[$contentTypeId] = $this->contentTypeService->loadContentType($contentTypeId);
}
$contentTypeIds[] = $relation->getSourceContentInfo()->contentTypeId;
}
$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(
'@ezdesign/content/tab/relations/tab.html.twig',
@@ -8,8 +8,7 @@
namespace EzSystems\EzPlatformAdminUi\Tests\Validator\Constraint;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use EzSystems\EzPlatformAdminUi\Validator\Constraints\LocationIsContainer;
@@ -19,9 +18,6 @@
class LocationIsContainerValidatorTest extends TestCase
{
/** @var \eZ\Publish\API\Repository\ContentTypeService|\PHPUnit\Framework\MockObject\MockObject */
private $contentTypeService;
/** @var \Symfony\Component\Validator\Context\ExecutionContextInterface */
private $executionContext;
@@ -31,29 +27,35 @@ class LocationIsContainerValidatorTest extends TestCase
/** @var \eZ\Publish\API\Repository\Values\Content\Location|\PHPUnit\Framework\MockObject\MockObject */
private $location;
/** @var \eZ\Publish\API\Repository\Values\ContentType\ContentType|\PHPUnit\Framework\MockObject\MockObject */
private $contentType;
protected function setUp()
{
$this->contentTypeService = $this->createMock(ContentTypeService::class);
$this->executionContext = $this->createMock(ExecutionContextInterface::class);
$this->validator = new LocationIsContainerValidator($this->contentTypeService);
$this->validator = new LocationIsContainerValidator();
$this->validator->initialize($this->executionContext);
$content = $this->createMock(Content::class);
$this->location = $this->createMock(Location::class);
$this->location
->method('getContentInfo')
->willReturn(
$this->createMock(ContentInfo::class)
);
->method('getContent')
->willReturn($content);
$this->contentType = $this->createMock(ContentType::class);
$content
->method('getContentType')
->willReturn($this->contentType);
}
public function testValid()
{
$contentType = $this
->getMockBuilder(ContentType::class)
->setMethodsExcept(['__get'])
->setConstructorArgs([['isContainer' => true]])
->getMock();
$this->contentTypeService->method('loadContentType')->willReturn($contentType);
$this->contentType
->method('__get')
->with('isContainer')
->willReturn(true);
$this->executionContext
->expects($this->never())
@@ -64,13 +66,10 @@ public function testValid()
public function testInvalid()
{
$contentType = $this
->getMockBuilder(ContentType::class)
->setMethodsExcept(['__get'])
->setConstructorArgs([['isContainer' => false]])
->getMock();
$this->contentTypeService->method('loadContentType')->willReturn($contentType);
$this->contentType
->method('__get')
->with('isContainer')
->willReturn(false);
$this->executionContext
->expects($this->once())
@@ -70,12 +70,7 @@ public function getConfig(): array
*/
private function resolveProfilePictureField(ApiUser $user): ?Field
{
try {
$contentType = $this->contentTypeService->loadContentType($user->contentInfo->contentTypeId);
} catch (\Exception $e) {
return null;
}
$contentType = $user->getContentType();
foreach ($user->getFields() as $field) {
$fieldDef = $contentType->getFieldDefinition($field->fieldDefIdentifier);
@@ -13,6 +13,7 @@
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\LocationService;
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 EzSystems\EzPlatformAdminUi\UI\Value\Content\VersionId;
@@ -60,16 +61,18 @@ public function load(User $user = null): self
}
$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) {
$locations = $this->locationService->loadParentLocationsForDraftContent($version);
// empty locations here means Location has been trashed and Draft should be ignored
return !empty($locations);
}
$contentTypes = $contentTypeIds = [];
foreach ($contentDrafts as $contentDraft) {
$contentTypeIds[] = $contentDraft->getContentInfo()->contentTypeId;
}
return true;
});
if (!empty($contentTypeIds)) {
$contentTypes = $this->contentTypeService->loadContentTypeList(array_unique($contentTypeIds));
}
// ContentService::loadContentDrafts returns unsorted list of VersionInfo.
// Sort results by modification date, descending.
@@ -78,8 +81,11 @@ public function load(User $user = null): self
});
$this->data = array_map(
function (VersionInfo $versionInfo) {
return $this->mapContentDraft($versionInfo);
function (VersionInfo $versionInfo) use ($contentTypes) {
return $this->mapContentDraft(
$versionInfo,
$contentTypes[$versionInfo->getContentInfo()->contentTypeId]
);
},
$contentDrafts
);
@@ -102,12 +108,9 @@ public function getContentDrafts(): array
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
private function mapContentDraft(VersionInfo $draft): array
private function mapContentDraft(VersionInfo $draft, ContentType $contentType): array
{
$contentInfo = $draft->getContentInfo();
$contentType = $this->contentTypeService->loadContentType(
$contentInfo->contentTypeId
);
return [
'id' => new VersionId(
@@ -84,8 +84,8 @@ public function __construct(
public function load(Location $location): self
{
$roleAssignments = [];
$content = $this->contentService->loadContentByContentInfo($location->contentInfo);
$contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);
$content = $location->getContent();
$contentType = $content->getContentType();
if ((new ContentTypeIsUser($this->userContentTypeIdentifier))->isSatisfiedBy($contentType)) {
$user = $this->userService->loadUser($content->id);
@@ -10,7 +10,7 @@
use eZ\Publish\API\Repository\ContentService;
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 as UIValue;
@@ -47,16 +47,16 @@ public function __construct(ContentService $contentService, ValueFactory $valueF
*
* @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) {
$this->relations[] = $this->valueFactory->createRelation($relation, $contentInfo);
$this->relations[] = $this->valueFactory->createRelation($relation, $content);
}
foreach ($this->contentService->loadReverseRelations($versionInfo->getContentInfo()) as $reverseRelation) {
$this->reverseRelations[] = $this->valueFactory->createRelation($reverseRelation, $contentInfo);
$this->reverseRelations[] = $this->valueFactory->createRelation($reverseRelation, $content);
}
return $this;
Oops, something went wrong.

0 comments on commit 0e07bc2

Please sign in to comment.
You can’t perform that action at this time.