Test Failed
Pull Request — master (#3)
by Svaťa
03:32
created

CsvPrices::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
	public function __construct(string $filename)
23
	{
24
		$this->filename = $filename;
25
	}
26
27
	public function unitPrice(string $productId): Price
28
	{
29
		$this->loadPrices();
30
		if (!isset ($this->prices[$productId])) {
31
			throw new PriceNotFoundException();
32
		}
33
		return $this->prices[$productId];
34
	}
35
36
	private function loadPrices()
37
	{
38
		if ($this->prices === []) {
39
			$handle = \fopen($this->filename, 'r');
40
			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
				$id = $data[0];
42
				$price = new Price($data[1]);
43
				$this->prices[$id] = $price;
44
			}
45
			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
	}
48
}
49