@@ -10,12 +10,14 @@ final class Loader implements StepBuilderInterface
1010 private ?Node \Expr $ logger ;
1111 private ?Node \Expr $ rejection ;
1212 private ?Node \Expr $ state ;
13+ private array $ params ;
1314
14- public function __construct ()
15+ public function __construct (private Node \ Expr $ query , private Node \ Expr $ dsn , private ? Node \ Expr $ username = null , private ? Node \ Expr $ password = null )
1516 {
1617 $ this ->logger = null ;
1718 $ this ->rejection = null ;
1819 $ this ->state = null ;
20+ $ this ->params = [];
1921 }
2022
2123 public function withLogger (Node \Expr $ logger ): StepBuilderInterface
@@ -39,8 +41,196 @@ public function withState(Node\Expr $state): StepBuilderInterface
3941 return $ this ;
4042 }
4143
44+ public function withUsername (Node \Expr $ username ): StepBuilderInterface
45+ {
46+ $ this ->username = $ username ;
47+
48+ return $ this ;
49+ }
50+
51+ public function withPassword (Node \Expr $ password ): StepBuilderInterface
52+ {
53+ $ this ->password = $ password ;
54+
55+ return $ this ;
56+ }
57+
58+ public function addParam (int |string $ key , Node \Expr $ param ): StepBuilderInterface
59+ {
60+ $ this ->params [$ key ] = $ param ;
61+
62+ return $ this ;
63+ }
64+
4265 public function getNode (): Node
4366 {
44- // TODO: Implement getNode() method.
67+ return new Node \Expr \New_ (
68+ class: new Node \Stmt \Class_ (
69+ name: null ,
70+ subNodes: [
71+ 'implements ' => [
72+ new Node \Name \FullyQualified (name: 'Kiboko \\Contract \\Pipeline \\LoaderInterface ' ),
73+ ],
74+ 'stmts ' => [
75+ new Node \Stmt \ClassMethod (
76+ name: new Node \Identifier (name: '__construct ' ),
77+ subNodes: [
78+ 'flags ' => Node \Stmt \Class_::MODIFIER_PUBLIC ,
79+ 'params ' => [
80+ new Node \Param (
81+ var: new Node \Expr \Variable ('logger ' ),
82+ type: new Node \Name \FullyQualified (name: 'Psr \\Log \\LoggerInterface ' ),
83+ flags: Node \Stmt \Class_::MODIFIER_PUBLIC ,
84+ ),
85+ ],
86+ ],
87+ ),
88+ new Node \Stmt \ClassMethod (
89+ name: new Node \Identifier ('load ' ),
90+ subNodes: [
91+ 'flags ' => Node \Stmt \Class_::MODIFIER_PUBLIC ,
92+ 'returnType ' => new Node \Name \FullyQualified (name: 'Generator ' ),
93+ 'stmts ' => [
94+ new Node \Stmt \Expression (
95+ expr: new Node \Expr \Assign (
96+ var: new Node \Expr \Variable ('input ' ),
97+ expr: new Node \Expr \Yield_ ()
98+ )
99+ ),
100+ new Node \Stmt \TryCatch (
101+ stmts: [
102+ new Node \Stmt \Expression (
103+ expr: new Node \Expr \Assign (
104+ var: new Node \Expr \Variable ('dbh ' ),
105+ expr: new Node \Expr \New_ (
106+ class: new Node \Name \FullyQualified ('PDO ' ),
107+ args: [
108+ new Node \Arg ($ this ->dsn ),
109+ $ this ->username ? new Node \Arg ($ this ->username ) : new Node \Expr \ConstFetch (new Node \Name ('null ' )),
110+ $ this ->password ? new Node \Arg ($ this ->password ) : new Node \Expr \ConstFetch (new Node \Name ('null ' ))
111+ ],
112+ ),
113+ ),
114+ ),
115+ new Node \Stmt \Do_ (
116+ cond: new Node \Expr \Assign (
117+ var: new Node \Expr \Variable (name: 'input ' ),
118+ expr: new Node \Expr \Yield_ (
119+ value: new Node \Expr \New_ (
120+ class: new Node \Name \FullyQualified ('Kiboko\Component\Bucket\AcceptanceResultBucket ' ),
121+ args: [
122+ new Node \Arg (
123+ new Node \Expr \Variable ('input ' )
124+ ),
125+ ],
126+ ),
127+ ),
128+ ),
129+ stmts: [
130+ new Node \Stmt \Expression (
131+ expr: new Node \Expr \Assign (
132+ var: new Node \Expr \Variable ('stmt ' ),
133+ expr: new Node \Expr \MethodCall (
134+ var: new Node \Expr \Variable ('dbh ' ),
135+ name: new Node \Name ('prepare ' ),
136+ args: [
137+ new Node \Arg ($ this ->query )
138+ ],
139+ ),
140+ ),
141+ ),
142+ ...$ this ->compileParams (),
143+ new Node \Stmt \Expression (
144+ expr: new Node \Expr \MethodCall (
145+ var: new Node \Expr \Variable ('stmt ' ),
146+ name: new Node \Name ('execute ' )
147+ ),
148+ ),
149+ ],
150+ ),
151+ new Node \Stmt \Expression (
152+ expr: new Node \Expr \Assign (
153+ var: new Node \Expr \Variable ('dbh ' ),
154+ expr: new Node \Expr \ConstFetch (
155+ name: new Node \Name ('null ' )
156+ ),
157+ ),
158+ ),
159+ ],
160+ catches: [
161+ new Node \Stmt \Catch_ (
162+ types: [
163+ new Node \Name ('PDOException ' )
164+ ],
165+ var: new Node \Expr \Variable ('exception ' ),
166+ stmts: [
167+ new Node \Stmt \Expression (
168+ expr: new Node \Expr \MethodCall (
169+ var: new Node \Expr \PropertyFetch (
170+ var: new Node \Expr \Variable ('this ' ),
171+ name: 'logger ' ,
172+ ),
173+ name: new Node \Identifier ('critical ' ),
174+ args: [
175+ new Node \Arg (
176+ value: new Node \Expr \MethodCall (
177+ var: new Node \Expr \Variable ('exception ' ),
178+ name: new Node \Identifier ('getMessage ' ),
179+ ),
180+ ),
181+ new Node \Arg (
182+ value: new Node \Expr \Array_ (
183+ items: [
184+ new Node \Expr \ArrayItem (
185+ value: new Node \Expr \Variable ('exception ' ),
186+ key: new Node \Scalar \String_ ('exception ' ),
187+ ),
188+ ],
189+ attributes: [
190+ 'kind ' => Node \Expr \Array_::KIND_SHORT ,
191+ ],
192+ ),
193+ ),
194+ ]
195+ ),
196+ ),
197+ ],
198+ ),
199+ ],
200+ ),
201+ ],
202+ ],
203+ ),
204+ ],
205+ ],
206+ ),
207+ args: [
208+ new Node \Arg (value: $ this ->logger ?? new Node \Expr \New_ (new Node \Name \FullyQualified ('Psr \\Log \\NullLogger ' ))),
209+ ],
210+ );
211+ }
212+
213+ public function compileParams (): array
214+ {
215+ $ output = [];
216+
217+ foreach ($ this ->params as $ key => $ param ) {
218+ $ output [] = new Node \Stmt \Expression (
219+ expr: new Node \Expr \MethodCall (
220+ var: new Node \Expr \Variable ('stmt ' ),
221+ name: new Node \Name ('bindParam ' ),
222+ args: [
223+ new Node \Arg (
224+ is_string ($ key ) ? new Node \Scalar \String_ ($ key ) : new Node \Scalar \LNumber ($ key )
225+ ),
226+ new Node \Arg (
227+ $ param
228+ )
229+ ],
230+ ),
231+ );
232+ }
233+
234+ return $ output ;
45235 }
46236}
0 commit comments