Passed
Push — master ( 288b46...98b0e3 )
by Nico
31:30 queued 08:00
created

CommodityCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Commodity\Lib;
6
7
use Stu\Orm\Entity\CommodityInterface;
8
use Stu\Orm\Repository\CommodityRepositoryInterface;
9
10
final class CommodityCache implements CommodityCacheInterface
11
{
12
    private CommodityRepositoryInterface $commodityRepository;
13
14
    /**
15
     * @var array<int, CommodityInterface>|null
16
     */
17
    private ?array $commodityArray = null;
18
19 2
    public function __construct(
20
        CommodityRepositoryInterface $commodityRepository
21
    ) {
22 2
        $this->commodityRepository = $commodityRepository;
23
    }
24
25
    public function get(int $commodityId): CommodityInterface
26
    {
27
        if ($this->commodityArray === null) {
28
            $this->commodityArray = $this->commodityRepository->getAll();
29
        }
30
31
        return $this->commodityArray[$commodityId];
32
    }
33
}
34