PdoAddressInserter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A __invoke() 0 18 2
1
<?php
2
namespace Germania\Addresses;
3
4
class PdoAddressInserter
5
{
6
	/**
7
	 * @var PDOStmt
8
	 */
9
	public $stmt; 
10
11
	/**
12
	 * @var PDO
13
	 */
14
	public $pdo; 
15
16
17
	/**
18
	 * @param \PDO                $pdo
19
	 * @param string              $table_name
20
	 */
21 6
	public function __construct( \PDO $pdo, string $table_name )
22
	{
23 6
		$this->pdo = $pdo;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo of type object<PDO> is incompatible with the declared type object<Germania\Addresses\PDO> of property $pdo.

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...
24
25 6
		$sql = "INSERT INTO {$table_name} 
26
		( type, street1, street2, zip, location, country )
27
		VALUES( :type, :street1, :street2, :zip, :location, :country)";
28
29 6
		$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...
30 6
	}
31
32
33
34 6
	public function __invoke( AddressInterface $address ) : int
35
	{
36 6
		$result = $this->stmt->execute([
37 6
			'type'     => $address->getType(),
38 6
			'street1'  => $address->getStreet1(),
39 6
			'street2'  => $address->getStreet2(),
40 6
			'zip'      => $address->getZip(),
41 6
			'location' => $address->getLocation(),
42 6
			'country'  => $address->getCountry()
43
		]);
44
45 6
		if (!$result) {
46 2
			$msg = sprintf("Could not execute PDOStatement for inserting address");
47 2
			throw new \RuntimeException($msg);
48
		}
49
50 4
		return $this->pdo->lastInsertId();
51
	}
52
}