Completed
Push — develop ( ae060f...1a4eee )
by Alejandro
08:12 queued 08:08
created

AbstractCacheableCountPaginatorAdapter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 5
dl 0
loc 20
ccs 4
cts 4
cp 1
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A count() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Paginator\Adapter;
6
7
use Laminas\Paginator\Adapter\AdapterInterface;
8
9
abstract class AbstractCacheableCountPaginatorAdapter implements AdapterInterface
10
{
11
    private ?int $count = null;
12
13 4
    final public function count(): int
14
    {
15
        // Since a new adapter instance is created every time visits are fetched, it is reasonably safe to internally
16
        // cache the count value.
17
        // The reason it is cached is because the Paginator is actually calling the method twice.
18
        // An inconsistent value could be returned if between the first call and the second one, a new visit is created.
19
        // However, it's almost instant, and then the adapter instance is discarded immediately after.
20
21 4
        if ($this->count !== null) {
22 4
            return $this->count;
23
        }
24
25 4
        return $this->count = $this->doCount();
26
    }
27
28
    abstract protected function doCount(): int;
29
}
30