| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Developtech\AgilityBundle\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Developtech\AgilityBundle\Model\ProjectModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Developtech\AgilityBundle\Model\FeedbackModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Developtech\AgilityBundle\Utils\Slugger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Security\Core\User\UserInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class FeedbackManager { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var EntityManager **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $em; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var Slugger **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $slugger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var string **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $feedbackClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param EntityManager $em | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param Slugger $slugger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param string $feedbackClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 5 |  |     public function __construct(EntityManager $em, Slugger $slugger, $feedbackClass) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 5 |  |         $this->em = $em; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 5 |  |         $this->slugger = $slugger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 5 |  |         $this->feedbackClass = $feedbackClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param ProjectModel $project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |     public function getProjectFeedbacks(ProjectModel $project) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return $this->em->getRepository($this->feedbackClass)->findByProject($project); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     * @param ProjectModel $project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     * @param UserInterface $author | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     * @param array $orderBy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     * @param integer $limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     * @param integer $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |     public function getProjectFeedbacksByAuthor(ProjectModel $project, UserInterface $author, $orderBy = null, $limit = null, $offset = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |         return $this->em->getRepository($this->feedbackClass)->findBy([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             'project' => $project, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'author' => $author | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ], $orderBy, $limit, $offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param integer $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return FeedbackModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |     public function getFeedback($id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if(($feedback = $this->em->getRepository($this->feedbackClass)->find($id)) === null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             throw new NotFoundHttpException('Feedback not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         return $feedback; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param ProjectModel $project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param string $description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param UserInterface $author | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return FeedbackModel | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 76 | 1 |  |     public function createFeedback(ProjectModel $project, $name, $description, UserInterface $author) { | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         $feedback = | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |             (new $this->feedbackClass()) | 
            
                                                        
            
                                    
            
            
                | 79 | 1 |  |             ->setName($name) | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |             ->setSlug($this->slugger->slugify($name)) | 
            
                                                        
            
                                    
            
            
                | 81 | 1 |  |             ->setDescription($description) | 
            
                                                        
            
                                    
            
            
                | 82 | 1 |  |             ->setProject($project) | 
            
                                                        
            
                                    
            
            
                | 83 | 1 |  |             ->setAuthor($author) | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |             ->setStatus(FeedbackModel::STATUS_OPEN) | 
            
                                                        
            
                                    
            
            
                | 85 | 1 |  |         ; | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         $this->em->persist($feedback); | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |         $this->em->flush(); | 
            
                                                        
            
                                    
            
            
                | 88 | 1 |  |         return $feedback; | 
            
                                                        
            
                                    
            
            
                | 89 | 1 |  |     } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                        
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.