@@ -110,35 +110,59 @@ public function getStatus($project=null) {
110110
111111 public function waitToFinish ($ delay =5 , $ callback =null ) {
112112
113- $ max_loops = 60 ;
113+ $ max_loops = 60 ; // loop up to 60 times
114114 $ loops = 0 ;
115115
116116 while ($ loops <$ max_loops ) {
117- $ response = $ this ->getStatus ();
117+ $ response = $ this ->getStatus (); // get the movie rendering status
118118
119119 if ($ response && ($ response ['success ' ]??false ) && !empty ($ response ['movie ' ])) {
120+ // if the API returns a valid response
120121
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+ }
123130
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+ }
126143 }
127144 }
128145 else {
146+ // if the API doesn't return a valid response
129147 throw new \Error ('Invalid API response ' );
130148 }
131149
132- sleep ($ delay );
150+ sleep ($ delay ); // wait for $delay
133151 $ loops ++;
134152 }
153+
154+ throw new \Error ('The rendering process took more than expected or maybe failed ' );
135155 }
136156
137157 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
139162 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 ;
142166 }
143167 }
144168}
0 commit comments