PdoAddressInserter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
}