PdoAddressUpdater   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
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 54
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A __invoke() 0 22 3
1
<?php
2
namespace Germania\Addresses;
3
4
class PdoAddressUpdater
5
{
6
	/**
7
	 * @var PDOStmt
8
	 */
9
	public $stmt; 
10
11
12
	/**
13
	 * @param \PDO                $pdo
14
	 * @param string              $table_name
15
	 */
16 6
	public function __construct( \PDO $pdo, string $table_name )
17
	{
18
19 6
		$sql = "UPDATE {$table_name} 
20
		SET
21
		type     = :type,
22
		street1  = :street1,
23
		street2  = :street2,
24
		zip      = :zip,
25
		location = :location,
26
		country  = :country
27
		WHERE id = :id
28
		LIMIT 1";
29
30 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...
31 6
	}
32
33
34
35 6
	public function __invoke( PdoAddressInterface $address ) : int
36
	{
37 6
		if (!$id = $address->getId())
38 2
			throw new \UnexpectedValueException("Addess object 'getId' did not return primary key value");
39
40 4
		$result = $this->stmt->execute([
41 4
			'id'       => $id,
42 4
			'type'     => $address->getType(),
43 4
			'street1'  => $address->getStreet1(),
44 4
			'street2'  => $address->getStreet2(),
45 4
			'zip'      => $address->getZip(),
46 4
			'location' => $address->getLocation(),
47 4
			'country'  => $address->getCountry()
48
		]);
49
50 4
		if (!$result) {
51 2
			$msg = sprintf("Could not execute PDOStatement for address ID '%s'", $id);
52 2
			throw new \RuntimeException($msg);
53
		}
54
55 2
		return $this->stmt->rowCount();
56
	}
57
}