| 1 |  |  | <?php namespace Comodojo\Extender\Queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use \Comodojo\Foundation\Base\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use \Comodojo\Foundation\Base\ConfigurationTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \Comodojo\Foundation\Events\Manager as EventsManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \Comodojo\Foundation\Logging\LoggerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \Comodojo\Foundation\Events\EventsTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use \Comodojo\Extender\Components\Database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use \Comodojo\Extender\Traits\EntityManagerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use \Comodojo\Extender\Task\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use \Comodojo\Extender\Orm\Entities\Queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use \Comodojo\Extender\Events\QueueEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use \Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use \Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use \Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | * @package     Comodojo Extender | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | * @author      Marco Giovinazzi <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | * @license     MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | * LICENSE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | * THE SOFTWARE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | class Manager { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     use ConfigurationTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     use LoggerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     use EventsTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     use EntityManagerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * If true, $em will be destroyed at shutdown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     private $destroy_em = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Class constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param Configuration $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param LoggerInterface $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param EventsManager $events | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param EntityManager $em | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         Configuration $configuration, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         LoggerInterface $logger, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         EventsManager $events, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         EntityManager $em = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->setConfiguration($configuration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->setLogger($logger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->setEvents($events); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if ( is_null($em) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->setEntityManager( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 Database::init($configuration)->getEntityManager() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $this->setEntityManager($em); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->destroy_em = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         // $logger->debug("Tasks Manager online", array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         //     'lagger_timeout' => $this->lagger_timeout, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         //     'multithread' => $this->multithread, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         //     'max_runtime' => $this->max_runtime, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         //     'max_childs' => $this->max_childs | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // )); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function __destruct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if ($this->destroy_em) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $em = $this->getEntityManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $em->getConnection()->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $em->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     public function get() { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $em = $this->getEntityManager(); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         return $em->getRepository('Comodojo\Extender\Orm\Entities\Queue')->findAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function flush(array $queue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->getEvents()->emit( new QueueEvent('flush', null, $queue) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $em = $this->getEntityManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         foreach ($queue as $record) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $em->remove($record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     public function add(Request $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $em = $this->getEntityManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $uid = $this->doAddRequest($request, $em); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return $uid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function addBulk(array $queue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $em = $this->getEntityManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $records = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         foreach ($queue as $name => $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $records[] = $request instanceof Request ? $this->doAddRequest($request, $em) : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         return $records; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     protected function doAddRequest(Request $request, EntityManager $em) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $this->getEvents()->emit( new QueueEvent('add', $request) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $record = new Queue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $record->setName($request->getName())->setRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $em->persist($record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         return $request->getUid(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |  | 
            
                        
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.