GetCache   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 26
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A countItems() 0 5 1
A getCount() 0 7 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service\Cache;
6
7
use Doctrine\Persistence\ManagerRegistry;
8
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
9
10
trait GetCache
11
{
12
    private FilesystemAdapter $cache;
13
    private ManagerRegistry $doctrine;
14
    private string $persistentObjectName;
15
16
    public function __construct(ManagerRegistry $doctrine)
17
    {
18
        $this->cache = new FilesystemAdapter();
19
        $this->doctrine = $doctrine;
20
    }
21
22
    public function getCount(string $key, string $class): int
23
    {
24
        $this->persistentObjectName = $class;
25
26
        $count = $this->cache->get($key, fn () => $this->countItems());
27
28
        return (int) $count;
29
    }
30
31
    private function countItems(): int
32
    {
33
        return (int) $this->doctrine
34
            ->getRepository($this->persistentObjectName)
35
            ->countAll();
36
    }
37
}
38