@@ -110,35 +110,59 @@ public function getStatus($project=null) {
110
110
111
111
public function waitToFinish ($ delay =5 , $ callback =null ) {
112
112
113
- $ max_loops = 60 ;
113
+ $ max_loops = 60 ; // loop up to 60 times
114
114
$ loops = 0 ;
115
115
116
116
while ($ loops <$ max_loops ) {
117
- $ response = $ this ->getStatus ();
117
+ $ response = $ this ->getStatus (); // get the movie rendering status
118
118
119
119
if ($ response && ($ response ['success ' ]??false ) && !empty ($ response ['movie ' ])) {
120
+ // if the API returns a valid response
120
121
121
- if (is_callable ($ callback )) $ callback ($ response ['movie ' ], $ response ['remaining_quota ' ]);
122
- else $ this ->printStatus ($ response ['movie ' ], $ response ['remaining_quota ' ]);
122
+ if (is_callable ($ callback )) {
123
+ // if the callback function is set, use it
124
+ $ callback ($ response ['movie ' ], $ response ['remaining_quota ' ]);
125
+ }
126
+ else {
127
+ // if not, print the status
128
+ $ this ->printStatus ($ response ['movie ' ], $ response ['remaining_quota ' ]);
129
+ }
123
130
124
- if (!empty ($ response ['movie ' ]['status ' ]) && $ response ['movie ' ]['status ' ]=='done ' ) {
125
- return $ response ;
131
+ if (!empty ($ response ['movie ' ]['status ' ])) {
132
+ // if the response has a status (it should), check what is the status...
133
+
134
+ if ($ response ['movie ' ]['status ' ]=='done ' ) {
135
+ // if the movie is done
136
+ return $ response ;
137
+ }
138
+
139
+ if ($ response ['movie ' ]['status ' ]=='error ' ) {
140
+ // if the movie rendering has failed
141
+ throw new \Exception ($ response ['movie ' ]['message ' ]);
142
+ }
126
143
}
127
144
}
128
145
else {
146
+ // if the API doesn't return a valid response
129
147
throw new \Error ('Invalid API response ' );
130
148
}
131
149
132
- sleep ($ delay );
150
+ sleep ($ delay ); // wait for $delay
133
151
$ loops ++;
134
152
}
153
+
154
+ throw new \Error ('The rendering process took more than expected or maybe failed ' );
135
155
}
136
156
137
157
public function printStatus ($ response , $ quota ) {
138
- echo 'Status: ' , $ response ['status ' ], ' / ' , $ response ['message ' ], PHP_EOL ;
158
+ // print the status
159
+ echo 'Status: ' , $ response ['status ' ]??'' , ' / ' , $ response ['message ' ]??'' , PHP_EOL ;
160
+
161
+ // if the movie is done
139
162
if ($ response ['status ' ]=='done ' ) {
140
- echo PHP_EOL , 'Movie URL: ' , $ response ['url ' ], PHP_EOL ;
141
- echo 'Remaining quota: movies( ' , $ quota ['movies ' ], ') and drafts( ' , $ quota ['drafts ' ], ') ' , PHP_EOL , PHP_EOL ;
163
+ // print the URL and remaining quota
164
+ echo PHP_EOL , 'Movie URL: ' , $ response ['url ' ]??'No URL ' , PHP_EOL ;
165
+ echo 'Remaining time quota: ' , $ quota ['time ' ]??'No quota ' , ' seconds ' , PHP_EOL , PHP_EOL ;
142
166
}
143
167
}
144
168
}
0 commit comments