Skip to content

Commit

Permalink
Add strict types to CategoryManager
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Sep 11, 2019
1 parent 8fd3f35 commit 02e7831
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
40 changes: 24 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,13 +106,20 @@ 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
*/
public function getRootCategory($context = null)
{
$context = $this->getContext($context);
if (null === $context || \is_string($context)) {
$context = $this->getContext($context);
} elseif (!$context instanceof ContextInterface) {
throw new \InvalidArgumentException(sprintf(
'Invalid parameter given: %s',
(string) $context
));
}

$this->loadCategories($context);

Expand All @@ -120,7 +128,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 @@ -189,7 +199,14 @@ public function getRootCategoriesSplitByContexts($loadChildren = true)

public function getCategories($context = null)
{
$context = $this->getContext($context);
if (null === $context || \is_string($context)) {
$context = $this->getContext($context);
} elseif (!$context instanceof ContextInterface) {
throw new \InvalidArgumentException(sprintf(
'Invalid parameter given: %s',
(string) $context
));
}

$this->loadCategories($context);

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

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

if ($contextCode instanceof ContextInterface) {
return $contextCode;
}

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

if (!$context instanceof ContextInterface) {
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

0 comments on commit 02e7831

Please sign in to comment.