| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @description: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @authors: Nicolaas [at] Sunny Side Up .co.nz | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package: ecommerce | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @sub-package: tasks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @inspiration: Silverstripe Ltd, Jeremy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class EcommerceTaskProcessOrderQueue extends BuildTask | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $sendEmails = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $limit = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $title = 'Process The Order Queue'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $description = 'Go through order queue and try to finalise all the orders in it.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      *@return int - number of carts destroyed | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function run($request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         //as this may run every minute, we have to limit it to fifty seconds. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         set_time_limit(50); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $now = microtime(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         //IMPORTANT! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         if ( ! $this->sendEmails) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             Config::inst()->update('Email', 'send_all_emails_to', 'no-one@localhost'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             Email::set_mailer(new Ecommerce_Dummy_Mailer()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $id = intval($request->getVar('id')) - 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $queueObjectSingleton = Injector::inst()->get('OrderProcessQueue'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $ordersinQueue = $queueObjectSingleton->OrdersToBeProcessed($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if($ordersinQueue->count() == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             echo 'No orders in queue'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         echo '<h3>There are '.$ordersinQueue->count().' in the queue, processing '.$this->limit.' now</h3>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if($id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             echo '<h3>FORCING Order with ID</h3>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $ordersinQueue = $ordersinQueue->filter(array('ID' => $id)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->tryToFinaliseOrders($ordersinQueue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         echo '<hr />'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         echo '<hr />'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         echo 'PROCECESSED IN: '.round(((microtime(true) - $now) / 1), 5).' seconds'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |     protected function tryToFinaliseOrders($orders) | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         //limit orders | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         $orders = $orders->limit($this->limit); | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         $queueObjectSingleton = Injector::inst()->get('OrderProcessQueue'); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         foreach ($orders as $order) { | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |             echo '<hr />Processing order: '.$order->ID; | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             $queueObjectSingleton->process($order); | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                        
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.