| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Kunstmaan\FixturesBundle\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\ORM\EntityManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Kunstmaan\FixturesBundle\Loader\Fixture; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Kunstmaan\MediaBundle\Entity\Folder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Kunstmaan\MediaBundle\Entity\Media; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Kunstmaan\MediaBundle\Helper\File\FileHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Kunstmaan\MediaBundle\Helper\MimeTypeGuesserFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\HttpFoundation\File\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Mime\MimeTypes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class MediaBuilder implements BuilderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** @var EntityManagerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $em; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var FileHandler */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $fileHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var MimeTypes|ExtensionGuesserInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $mimeTypeGuesser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $folder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function __construct(EntityManager $em, FileHandler $fileHandler, /* MimeTypes */$mimeTypeGuesser) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->em = $em; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->fileHandler = $fileHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->mimeTypeGuesser = $mimeTypeGuesser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         if ($mimeTypeGuesser instanceof MimeTypeGuesserFactoryInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             @trigger_error(sprintf('Passing an instance of "%s" for the "$mimeTypeGuesser" parameter is deprecated since KunstmaanMediaBundle 5.7 and will be replaced by the "@mime_types" service in KunstmaanMediaBundle 6.0. Inject the correct service instead.', MimeTypeGuesserFactoryInterface::class), E_USER_DEPRECATED); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $this->mimeTypeGuesser = $mimeTypeGuesser->get(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function canBuild(Fixture $fixture) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if ($fixture->getEntity() instanceof Media) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function preBuild(Fixture $fixture) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $properties = $fixture->getProperties(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (!isset($properties['folder'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             throw new \Exception('There is no folder specified for media fixture '.$fixture->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->folder = $this->em->getRepository(Folder::class)->findOneBy(['rel' => $properties['folder']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if (!$this->folder instanceof Folder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $this->folder = $this->em->getRepository(Folder::class)->findOneBy(['internalName' => $properties['folder']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (!$this->folder instanceof Folder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             throw new \Exception('Could not find the specified folder for media fixture '.$fixture->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     public function postBuild(Fixture $fixture) | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         /** @var Media $media */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $media = $fixture->getEntity(); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         $filePath = $media->getOriginalFilename(); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $data = new File($filePath, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $contentType = $this->guessMimeType($data->getPathname()); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         if (method_exists($data, 'getClientOriginalName')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             $media->setOriginalFilename($data->getClientOriginalName()); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             $media->setOriginalFilename($data->getFilename()); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         if ($media->getName() === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             $media->setName($media->getOriginalFilename()); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $media->setContent($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         $media->setContentType($contentType); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $media->setFolder($this->folder); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $this->fileHandler->prepareMedia($media); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $this->fileHandler->updateMedia($media); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $this->fileHandler->saveMedia($media); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     private function guessMimeType($pathName): ?string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         if ($this->mimeTypeGuesser instanceof MimeTypeGuesserInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             return $this->mimeTypeGuesser->guess($pathName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return $this->mimeTypeGuesser->guessMimeType($pathName); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function postFlushBuild(Fixture $fixture) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  | 
            
                        
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.