1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* tubee |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tubee\Endpoint; |
13
|
|
|
|
14
|
|
|
use Generator; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
use Tubee\AttributeMap\AttributeMapInterface; |
17
|
|
|
use Tubee\Collection\CollectionInterface; |
18
|
|
|
use Tubee\Endpoint\Pdo\Exception\InvalidQuery; |
19
|
|
|
use Tubee\Endpoint\Pdo\Wrapper as PdoWrapper; |
20
|
|
|
use Tubee\EndpointObject\EndpointObjectInterface; |
21
|
|
|
use Tubee\Workflow\Factory as WorkflowFactory; |
22
|
|
|
|
23
|
|
|
class Pdo extends AbstractSqlDatabase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Kind. |
27
|
|
|
*/ |
28
|
|
|
public const KIND = 'PdoEndpoint'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Init endpoint. |
32
|
|
|
*/ |
33
|
5 |
|
public function __construct(string $name, string $type, string $table, PdoWrapper $socket, CollectionInterface $collection, WorkflowFactory $workflow, LoggerInterface $logger, array $resource = []) |
34
|
|
|
{ |
35
|
5 |
|
$this->socket = $socket; |
36
|
5 |
|
$this->table = $table; |
37
|
5 |
|
parent::__construct($name, $type, $collection, $workflow, $logger, $resource); |
38
|
5 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function count(?array $query = null): int |
44
|
|
|
{ |
45
|
|
|
list($filter, $values) = $this->transformQuery($query); |
46
|
|
|
|
47
|
|
|
if ($filter === null) { |
48
|
|
|
$sql = 'SELECT COUNT(*) as count FROM '.$this->table; |
49
|
|
|
} else { |
50
|
|
|
$sql = 'SELECT COUNT(*) as count FROM '.$this->table.' WHERE '.$filter; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$result = $this->socket->prepareValues($sql, $values); |
55
|
|
|
|
56
|
|
|
return (int) $result->fetch(\PDO::FETCH_ASSOC)['count']; |
57
|
|
|
} catch (InvalidQuery $e) { |
58
|
|
|
$this->logger->error('failed to count number of objects from endpoint', [ |
59
|
|
|
'class' => get_class($this), |
60
|
|
|
'exception' => $e, |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
return 0; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function getAll(?array $query = null): Generator |
71
|
|
|
{ |
72
|
|
|
list($filter, $values) = $this->transformQuery($query); |
73
|
|
|
$this->logGetAll($filter); |
74
|
|
|
|
75
|
|
|
if ($filter === null) { |
76
|
|
|
$sql = 'SELECT * FROM '.$this->table; |
77
|
|
|
} else { |
78
|
|
|
$sql = 'SELECT * FROM '.$this->table.' WHERE '.$filter; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
$result = $this->socket->prepareValues($sql, $values); |
83
|
|
|
} catch (InvalidQuery $e) { |
84
|
|
|
$this->logger->error('failed to fetch resources from endpoint', [ |
85
|
|
|
'class' => get_class($this), |
86
|
|
|
'exception' => $e, |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
return 0; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$i = 0; |
93
|
|
|
while ($row = $result->fetch(\PDO::FETCH_ASSOC)) { |
94
|
|
|
yield $this->build($row); |
95
|
|
|
++$i; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $i; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getOne(array $object, array $attributes = []): EndpointObjectInterface |
105
|
|
|
{ |
106
|
|
|
list($filter, $values) = $query = $this->transformQuery($this->getFilterOne($object)); |
107
|
|
|
$this->logGetOne($filter); |
108
|
|
|
$sql = 'SELECT * FROM '.$this->table.' WHERE '.$filter; |
109
|
|
|
$result = $this->socket->prepareValues($sql, $values); |
110
|
|
|
$row = $result->fetch(\PDO::FETCH_ASSOC); |
111
|
|
|
$seccond = $result->fetch(\PDO::FETCH_ASSOC); |
112
|
|
|
|
113
|
|
|
if ($seccond !== false) { |
114
|
|
|
throw new Exception\ObjectMultipleFound('found more than one object with filter '.$filter); |
115
|
|
|
} |
116
|
|
|
if ($row === false) { |
117
|
|
|
throw new Exception\ObjectNotFound('no object found with filter '.$filter); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->build($row, $query); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function create(AttributeMapInterface $map, array $object, bool $simulate = false): ?string |
127
|
|
|
{ |
128
|
|
|
$this->logCreate($object); |
129
|
|
|
$result = $this->prepareCreate($object, $simulate); |
|
|
|
|
130
|
|
|
|
131
|
|
|
if ($simulate === true) { |
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->socket->lastInsertId(); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.