Passed
Push — dev ( 766009...bcc49c )
by Janko
09:42
created

CommodityCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 1
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
    public function __construct(
20
        CommodityRepositoryInterface $commodityRepository
21
    ) {
22
        $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