Passed
Pull Request — master (#3)
by Svaťa
04:25 queued 01:59
created

CsvPrices::unitPrice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Simara\Cart\Infrastructure;
5
6
use Simara\Cart\Domain\Price;
7
use Simara\Cart\Domain\PriceNotFoundException;
8
use Simara\Cart\Domain\Prices;
9
10
final class CsvPrices implements Prices
11
{
12
	/**
13
	 * @var string
14
	 */
15
	private $filename;
16
17
	/**
18
	 * @var Price[]|array<string, Price>
19
	 */
20
	private $prices = [];
21
22 4
	public function __construct(string $filename)
23
	{
24 4
		$this->filename = $filename;
25 4
	}
26
27 3
	public function unitPrice(string $productId): Price
28
	{
29 3
		$this->loadPrices();
30 3
		if (!isset ($this->prices[$productId])) {
31 1
			throw new PriceNotFoundException();
32
		}
33 2
		return $this->prices[$productId];
34
	}
35
36 3
	private function loadPrices()
37
	{
38 3
		if ($this->prices === []) {
39 3
			$handle = \fopen($this->filename, 'r');
40 3
			while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fgetcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
			while (($data = fgetcsv(/** @scrutinizer ignore-type */ $handle, 1000, ",")) !== FALSE) {
Loading history...
41 3
				$id = $data[0];
42 3
				$price = new Price($data[1]);
43 3
				$this->prices[$id] = $price;
44
			}
45 3
			fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
			fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
46
		}
47 3
	}
48
}
49