Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strict types to CategoryManager #466

Merged
merged 1 commit into from Sep 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/Entity/CategoryManager.php
Expand Up @@ -89,7 +89,8 @@ public function getRootCategoryWithChildren(CategoryInterface $category)
if (null !== $category->getParent()) {
throw new \RuntimeException('Method can be called only for root categories');
}
$context = $this->getContext($category->getContext());

$context = $category->getContext();

$this->loadCategories($context);

Expand All @@ -105,7 +106,7 @@ public function getRootCategoryWithChildren(CategoryInterface $category)
/**
* @deprecated since sonata-project/classification-bundle 3.x, to be removed in 4.0.
*
* @param ContextInterface $context
* @param ContextInterface|string|null $context
*
* @return CategoryInterface
*/
Expand All @@ -120,7 +121,9 @@ public function getRootCategory($context = null)

public function getRootCategoriesForContext(ContextInterface $context = null)
{
$context = $this->getContext($context);
if (null === $context) {
$context = $this->getContext();
}

$this->loadCategories($context);

Expand Down Expand Up @@ -273,28 +276,30 @@ protected function loadCategories(ContextInterface $context)
$this->categories[$context->getId()] = $rootCategories;
}

/**
* @param string|ContextInterface $contextCode
*
* @return ContextInterface
*/
private function getContext($contextCode)
private function getContext($context = null): ContextInterface
{
if (empty($contextCode)) {
$contextCode = ContextInterface::DEFAULT_CONTEXT;
if ($context instanceof ContextInterface) {
return $context;
}

if (null === $context) {
$context = ContextInterface::DEFAULT_CONTEXT;
}

if ($contextCode instanceof ContextInterface) {
return $contextCode;
if (!\is_string($context)) {
throw new \InvalidArgumentException(sprintf(
'Invalid parameter given: %s',
(string) $context
));
}

$context = $this->contextManager->find($contextCode);
$context = $this->contextManager->find($context);

if (!$context instanceof ContextInterface) {
$context = $this->contextManager->create();

$context->setId($contextCode);
$context->setName($contextCode);
$context->setId($context);
$context->setName($context);
$context->setEnabled(true);

$this->contextManager->save($context);
Expand Down
4 changes: 2 additions & 2 deletions src/Model/CategoryManagerInterface.php
Expand Up @@ -20,10 +20,10 @@
/**
* @method PagerInterface getRootCategoriesPager(int $page = 1, int $limit = 25, array $criteria = [])
* @method PagerInterface getSubCategoriesPager(int $categoryId, int $page = 1, int $limit = 25, array $criteria = [])
* @method CategoryInterface[] getRootCategoriesForContext(?ContextInterface $context)
* @method CategoryInterface[] getRootCategoriesForContext(ContextInterface|string|null $context)
* @method CategoryInterface[] getAllRootCategories(bool $loadChildren = true)
* @method CategoryInterface[] getRootCategoriesSplitByContexts(bool $loadChildren = true)
* @method CategoryInterface[] getCategories(?ContextInterface $context)
* @method CategoryInterface[] getCategories(ContextInterface|string|null $context)
*/
interface CategoryManagerInterface extends ManagerInterface, PageableManagerInterface
{
Expand Down