PdoAddressFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 49
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A __invoke() 0 8 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( PdoAddress::class );
34
35 8
		$sql = "SELECT " . implode(",", static::$DB_FIELDS) ."
36 8
		FROM {$table_name}
37
		WHERE id = :id
38
		LIMIT 1";
39
40 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...
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
		$pdo_address_array = $this->stmt->fetch(\PDO::FETCH_ASSOC);
50 4
		return ($this->address_factory)($pdo_address_array);
51
	}
52
}