Skip to content

Commit 57fffd3

Browse files
authored
Merge pull request #91 from dotkernel/issue-88
Fixed categories counter by published
2 parents bb10e76 + 5fad167 commit 57fffd3

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/App/templates/partial/left-menu.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<li>
3030
<a href="{{ url('page::category-resource', {slug: category.slug}) }}">
3131
{{ category.name }}
32-
<span class="cat-count">{{ category.posts.count() }}</span>
32+
<span class="cat-count">{{ category.publishedPostsCount }}</span>
3333
</a>
3434
</li>
3535
{% endfor %}

src/Blog/src/Entity/Category.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
use Doctrine\Common\Collections\ArrayCollection;
88
use Doctrine\Common\Collections\Collection;
9+
use Doctrine\Common\Collections\Criteria;
10+
use Doctrine\Common\Collections\Selectable;
911
use Doctrine\ORM\Mapping as ORM;
1012
use Light\App\Entity\AbstractEntity;
13+
use Light\Blog\Enum\PostStatusEnum;
1114
use Light\Blog\Repository\CategoryRepository;
1215

1316
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
@@ -24,7 +27,7 @@ class Category extends AbstractEntity
2427
#[ORM\Column(name: 'isVisible', type: 'boolean')]
2528
private bool $isVisible = true;
2629

27-
/** @var Collection<int, Post> */
30+
/** @var Collection<int, Post>&Selectable<int, Post> */
2831
#[ORM\OneToMany(mappedBy: 'category', targetEntity: Post::class)]
2932
private Collection $posts;
3033

@@ -73,6 +76,13 @@ public function getPosts(): Collection
7376
return $this->posts;
7477
}
7578

79+
public function getPublishedPostsCount(): int
80+
{
81+
$criteria = Criteria::create()->where(Criteria::expr()->eq('status', PostStatusEnum::Published));
82+
83+
return $this->posts->matching($criteria)->count();
84+
}
85+
7686
/**
7787
* @return array{
7888
* id: non-empty-string,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LightTest\Unit\Blog\Entity;
6+
7+
use Light\Blog\Entity\Category;
8+
use Light\Blog\Entity\Post;
9+
use Light\Blog\Enum\PostStatusEnum;
10+
use LightTest\Unit\UnitTest;
11+
12+
class CategoryTest extends UnitTest
13+
{
14+
public function testGetPublishedPostsCountOnlyCountsPublishedPosts(): void
15+
{
16+
$category = new Category();
17+
18+
$published = new Post();
19+
$published->setStatus(PostStatusEnum::Published);
20+
21+
$draft = new Post();
22+
$draft->setStatus(PostStatusEnum::Draft);
23+
24+
$private = new Post();
25+
$private->setStatus(PostStatusEnum::Private);
26+
27+
$category->getPosts()->add($published);
28+
$category->getPosts()->add($draft);
29+
$category->getPosts()->add($private);
30+
31+
self::assertSame(1, $category->getPublishedPostsCount());
32+
}
33+
}

0 commit comments

Comments
 (0)