13
13
14
14
namespace Laudis \Neo4j \Http ;
15
15
16
- use JsonException ;
16
+ use function array_intersect ;
17
+ use function array_unique ;
18
+ use Laudis \Neo4j \Common \TransactionHelper ;
17
19
use Laudis \Neo4j \Contracts \ConnectionInterface ;
18
20
use Laudis \Neo4j \Contracts \FormatterInterface ;
19
21
use Laudis \Neo4j \Contracts \UnmanagedTransactionInterface ;
22
+ use Laudis \Neo4j \Databags \Neo4jError ;
20
23
use Laudis \Neo4j \Databags \Statement ;
24
+ use Laudis \Neo4j \Exception \Neo4jException ;
21
25
use Laudis \Neo4j \Types \CypherList ;
22
26
use function microtime ;
23
- use Psr \Http \Client \ClientExceptionInterface ;
24
27
use Psr \Http \Client \ClientInterface ;
25
28
use Psr \Http \Message \RequestInterface ;
29
+ use Psr \Http \Message \ResponseInterface ;
26
30
use Psr \Http \Message \StreamFactoryInterface ;
31
+ use stdClass ;
27
32
28
33
/**
29
34
* @template T
@@ -71,25 +76,16 @@ public function __construct(
71
76
$ this ->formatter = $ formatter ;
72
77
}
73
78
74
- /**
75
- * @throws JsonException|ClientExceptionInterface
76
- */
77
79
public function run (string $ statement , iterable $ parameters = [])
78
80
{
79
81
return $ this ->runStatement (new Statement ($ statement , $ parameters ));
80
82
}
81
83
82
- /**
83
- * @throws JsonException|ClientExceptionInterface
84
- */
85
84
public function runStatement (Statement $ statement )
86
85
{
87
86
return $ this ->runStatements ([$ statement ])->first ();
88
87
}
89
88
90
- /**
91
- * @throws JsonException
92
- */
93
89
public function runStatements (iterable $ statements ): CypherList
94
90
{
95
91
$ request = $ this ->request ->withMethod ('POST ' );
@@ -100,39 +96,39 @@ public function runStatements(iterable $statements): CypherList
100
96
$ start = microtime (true );
101
97
$ response = $ this ->connection ->getImplementation ()->sendRequest ($ request );
102
98
$ total = microtime (true ) - $ start ;
103
- $ data = HttpHelper::interpretResponse ($ response );
99
+
100
+ $ data = $ this ->handleResponse ($ response );
104
101
105
102
return $ this ->formatter ->formatHttpResult ($ response , $ data , $ this ->connection , $ total , $ total , $ statements );
106
103
}
107
104
108
- /**
109
- * @throws JsonException
110
- */
111
105
public function commit (iterable $ statements = []): CypherList
112
106
{
113
107
$ uri = $ this ->request ->getUri ();
114
108
$ request = $ this ->request ->withUri ($ uri ->withPath ($ uri ->getPath ().'/commit ' ))->withMethod ('POST ' );
109
+
115
110
$ content = HttpHelper::statementsToJson ($ this ->formatter , $ statements );
116
111
$ request = $ request ->withBody ($ this ->factory ->createStream ($ content ));
117
112
118
113
$ start = microtime (true );
119
114
$ response = $ this ->connection ->getImplementation ()->sendRequest ($ request );
120
115
$ total = microtime (true ) - $ start ;
121
116
122
- $ data = HttpHelper::interpretResponse ($ response );
117
+ $ data = $ this ->handleResponse ($ response );
118
+
119
+ $ this ->isCommitted = true ;
123
120
124
121
return $ this ->formatter ->formatHttpResult ($ response , $ data , $ this ->connection , $ total , $ total , $ statements );
125
122
}
126
123
127
- /**
128
- * @throws JsonException
129
- */
130
124
public function rollback (): void
131
125
{
132
126
$ request = $ this ->request ->withMethod ('DELETE ' );
133
127
$ response = $ this ->connection ->getImplementation ()->sendRequest ($ request );
134
128
135
- HttpHelper::interpretResponse ($ response );
129
+ $ this ->handleResponse ($ response );
130
+
131
+ $ this ->isRolledBack = true ;
136
132
}
137
133
138
134
public function __destruct ()
@@ -163,4 +159,36 @@ public function isFinished(): bool
163
159
{
164
160
return $ this ->isRolledBack () || $ this ->isCommitted ();
165
161
}
162
+
163
+ /**
164
+ * @throws Neo4jException
165
+ *
166
+ * @return never
167
+ */
168
+ private function handleNeo4jException (Neo4jException $ e ): void
169
+ {
170
+ $ classifications = array_map (static fn (Neo4jError $ e ) => $ e ->getClassification (), $ e ->getErrors ());
171
+ $ classifications = array_unique ($ classifications );
172
+
173
+ $ intersection = array_intersect ($ classifications , TransactionHelper::ROLLBACK_CLASSIFICATIONS );
174
+ if ($ intersection !== []) {
175
+ $ this ->isRolledBack = true ;
176
+ }
177
+
178
+ throw $ e ;
179
+ }
180
+
181
+ /**
182
+ * @throws Neo4jException
183
+ */
184
+ private function handleResponse (ResponseInterface $ response ): stdClass
185
+ {
186
+ try {
187
+ $ data = HttpHelper::interpretResponse ($ response );
188
+ } catch (Neo4jException $ e ) {
189
+ $ this ->handleNeo4jException ($ e );
190
+ }
191
+
192
+ return $ data ;
193
+ }
166
194
}
0 commit comments