Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 authored and greg0ire committed Apr 10, 2018
1 parent a70328f commit 10abbc2
Showing 1 changed file with 23 additions and 48 deletions.
71 changes: 23 additions & 48 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,7 @@ public function deleteAction($id)
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}

if ($parentAdmin = $this->admin->getParent()) {
$parentId = $request->get($parentAdmin->getIdParameter());

$propertyAccessor = PropertyAccess::createPropertyAccessor();
$propertyPath = new PropertyPath($this->admin->getParentAssociationMapping());

$parent = $propertyAccessor->getValue($object, $propertyPath);

if ($parentAdmin->getObject($parentId) !== $parent) {
// NEXT_MAJOR: make this exception
@trigger_error("Deleting a child that isn't connected to a given parent is deprecated since 3.x"
." and won't be allowed in 4.0.",
E_USER_DEPRECATED
);
}
}
$this->checkParentChildAssociation($request, $object);

$this->admin->checkAccess('delete', $object);

Expand Down Expand Up @@ -325,22 +310,7 @@ public function editAction($id = null)
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}

if ($parentAdmin = $this->admin->getParent()) {
$parentId = $request->get($parentAdmin->getIdParameter());

$propertyAccessor = PropertyAccess::createPropertyAccessor();
$propertyPath = new PropertyPath($this->admin->getParentAssociationMapping());

$parent = $propertyAccessor->getValue($existingObject, $propertyPath);

if ($parentAdmin->getObject($parentId) !== $parent) {
// NEXT_MAJOR: make this exception
@trigger_error("Editing a child that isn't connected to a given parent is deprecated since 3.x"
." and won't be allowed in 4.0.",
E_USER_DEPRECATED
);
}
}
$this->checkParentChildAssociation($request, $existingObject);

$this->admin->checkAccess('edit', $existingObject);

Expand Down Expand Up @@ -692,22 +662,7 @@ public function showAction($id = null)
throw $this->createNotFoundException(sprintf('unable to find the object with id: %s', $id));
}

if ($parentAdmin = $this->admin->getParent()) {
$parentId = $request->get($parentAdmin->getIdParameter());

$propertyAccessor = PropertyAccess::createPropertyAccessor();
$propertyPath = new PropertyPath($this->admin->getParentAssociationMapping());

$parent = $propertyAccessor->getValue($object, $propertyPath);

if ($parentAdmin->getObject($parentId) !== $parent) {
// NEXT_MAJOR: make this exception
@trigger_error("Viewing a child that isn't connected to a given parent is deprecated since 3.x"
." and won't be allowed in 4.0.",
E_USER_DEPRECATED
);
}
}
$this->checkParentChildAssociation($request, $object);

$this->admin->checkAccess('show', $object);

Expand Down Expand Up @@ -1497,6 +1452,26 @@ final protected function trans($id, array $parameters = [], $domain = null, $loc
return $this->get('translator')->trans($id, $parameters, $domain, $locale);
}

private function checkParentChildAssociation(Request $request, $object)
{
if (!($parentAdmin = $this->admin->getParent())) {
return;
}

$parentId = $request->get($parentAdmin->getIdParameter());

$propertyAccessor = PropertyAccess::createPropertyAccessor();
$propertyPath = new PropertyPath($this->admin->getParentAssociationMapping());

if ($parentAdmin->getObject($parentId) !== $propertyAccessor->getValue($object, $propertyPath)) {
// NEXT_MAJOR: make this exception
@trigger_error("Accessing a child that isn't connected to a given parent is deprecated since 3.x"
." and won't be allowed in 4.0.",
E_USER_DEPRECATED
);
}
}

/**
* Sets the admin form theme to form view. Used for compatibility between Symfony versions.
*
Expand Down

0 comments on commit 10abbc2

Please sign in to comment.