| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Alchemy\Zippy\Resource\Teleporter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Alchemy\Zippy\Exception\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Alchemy\Zippy\Exception\IOException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Alchemy\Zippy\Resource\Resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Alchemy\Zippy\Resource\ResourceLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Alchemy\Zippy\Resource\ResourceReaderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Alchemy\Zippy\Resource\ResourceWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class GenericTeleporter implements TeleporterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var ResourceReaderFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $readerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var ResourceWriter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $resourceWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var ResourceLocator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $resourceLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param ResourceReaderFactory $readerFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param ResourceWriter $resourceWriter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param ResourceLocator $resourceLocator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         ResourceReaderFactory $readerFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ResourceWriter $resourceWriter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         ResourceLocator $resourceLocator = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->readerFactory = $readerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->resourceWriter = $resourceWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->resourceLocator = $resourceLocator ?: new ResourceLocator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Teleports a file from a destination to an other | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param \Alchemy\Zippy\Resource\Resource $resource A Resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param string $context The current context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @throws IOException when file could not be written on local | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @throws InvalidArgumentException when path to file is not valid | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |     public function teleport(Resource $resource, $context) | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         $reader = $this->readerFactory->getReader($resource, $context); | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |         $target = $this->resourceLocator->mapResourcePath($resource, $context); | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         $this->resourceWriter->writeFromReader($reader, $target); | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  |