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 af7c707
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
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

0 comments on commit af7c707

Please sign in to comment.