1
|
|
|
<?php |
2
|
|
|
namespace Germania\Nav\ItemCodes\Actions; |
3
|
|
|
|
4
|
|
|
use Germania\Nav\ItemCodes\ItemCodeInterface; |
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\NullLogger; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Inserts or updates existing ItemCode entry in the database, |
10
|
|
|
* using MySQL's REPLACE statement. |
11
|
|
|
*/ |
12
|
|
|
class InsertOrUpdateItemCode |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The database table to work with |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
public $table; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var PDOStatement |
25
|
|
|
*/ |
26
|
|
|
protected $stmt; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var PDO |
30
|
|
|
*/ |
31
|
|
|
protected $pdo; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $logger; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param \PDO $pdo PDO instance |
42
|
|
|
* @param string $table Table name to work with. |
43
|
|
|
* @param LoggerInterface|null $logger Optional: PSR3 Logger |
44
|
|
|
*/ |
45
|
12 |
|
public function __construct(\PDO $pdo, $table, LoggerInterface $logger = null) |
46
|
|
|
{ |
47
|
12 |
|
$this->table = $table ?: $this->table; |
48
|
|
|
|
49
|
12 |
|
$this->pdo = $pdo; |
|
|
|
|
50
|
12 |
|
$this->logger = $logger ?: new NullLogger; |
51
|
|
|
|
52
|
12 |
|
$sql = "REPLACE INTO {$this->table} |
53
|
|
|
(code, name) |
54
|
|
|
VALUES |
55
|
|
|
(:item_code, :item_name)"; |
56
|
12 |
|
$this->stmt = $this->pdo->prepare($sql); |
|
|
|
|
57
|
12 |
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ItemCodeInterface $itemcode |
62
|
|
|
* @return int Number of affected rows, i.e. 2 on REPLACE, 1 on INSERT) |
63
|
|
|
*/ |
64
|
4 |
|
public function __invoke( ItemCodeInterface $itemcode ) |
65
|
|
|
{ |
66
|
4 |
|
return $this->execute( $itemcode ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ItemCodeInterface $itemcode |
72
|
|
|
* @return int Number of affected rows, i.e. 2 on REPLACE, 1 on INSERT) |
73
|
|
|
*/ |
74
|
12 |
|
public function execute( ItemCodeInterface $itemcode ) |
75
|
|
|
{ |
76
|
12 |
|
$bool = $this->stmt->execute([ |
77
|
12 |
|
':item_code' => $itemcode->getCode(), |
78
|
12 |
|
':item_name' => $itemcode->getName() |
79
|
|
|
]); |
80
|
|
|
|
81
|
12 |
|
$this->logger->debug('Logged sales', [ |
82
|
12 |
|
'item_code' => $itemcode->getCode(), |
83
|
12 |
|
'result' => $bool, |
84
|
12 |
|
'count' => $this->stmt->rowCount() |
85
|
|
|
]); |
86
|
|
|
|
87
|
12 |
|
return $this->stmt->rowCount(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
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..