Completed
Push — master ( 982641...a62c86 )
by Carsten
03:18 queued 11s
created

PdoAddressFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Germania\Addresses;
3
4
class PdoAddressFactory
5
{
6
	/**
7
	 * @var AddressFactory
8
	 */
9
	public $address_factory; 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
10
11
	/**
12
	 * @var PDOStmt
13
	 */
14
	public $stmt; 
15
16
	public static $DB_FIELDS = array(
17
		'id',
18
		'type',
19
		'street1',
20
		'street2',
21
		'zip',
22
		'location',
23
		'country'
24
	);
25
26
	/**
27
	 * @param \PDO                $pdo
28
	 * @param string              $table_name
29
	 * @param AddressFactory|null $address_factory
30
	 */
31 8
	public function __construct( \PDO $pdo, string $table_name, AddressFactory $address_factory = null )
32
	{
33 8
		$this->address_factory = $address_factory ?: new AddressFactory;
34
35 8
		$sql = "SELECT " . implode(",", static::$DB_FIELDS) ."
36 8
		FROM {$table_name}
37
		WHERE id = :id";
38
39 8
		$this->stmt = $pdo->prepare( $sql );
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo->prepare($sql) of type object<PDOStatement> is incompatible with the declared type object<Germania\Addresses\PDOStmt> of property $stmt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40 8
		$this->stmt->setFetchMode(\PDO::FETCH_CLASS, PdoAddress::class);
41 8
	}
42
43
44 6
	public function __invoke( int $id ) : AddressInterface
45
	{
46 6
		if (!$this->stmt->execute(['id' => $id]))
47 2
			throw new \RuntimeException("Could not execute SELECT Address PDOStatement");
48
49 4
		return $this->stmt->fetch();
50
	}
51
}