| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the BenGorFile package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Beñat Espiña <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * (c) Gorka Laucirica <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace BenGorFile\File\Domain\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * File domain class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author Beñat Espiña <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author Gorka Laucirica <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | class File extends FileAggregateRoot | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * The id. | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @var FileId | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     protected $id; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * Created on. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @var \DateTimeImmutable | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     protected $createdOn; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * The file mime type. | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @var FileMimeType | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     protected $mimeType; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * The file name. | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @var FileName | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     protected $name; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * Updated on. | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @var \DateTimeImmutable | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     protected $updatedOn; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * Constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @param FileId       $anId      The id | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @param FileName     $aName     The file name | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @param FileMimeType $aMimeType The file mime type | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function __construct(FileId $anId, FileName $aName, FileMimeType $aMimeType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->id = $anId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->name = $aName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->createdOn = new \DateTimeImmutable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->updatedOn = new \DateTimeImmutable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->setMimeType($aMimeType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->publish(new FileUploaded($this->id(), new FileName($aName))); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * Gets the id. | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @return FileId | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function id() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->id; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * Gets the created on. | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @return \DateTimeImmutable | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function createdOn() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return $this->createdOn; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * Gets the file mime type. | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * @return FileMimeType | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function mimeType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return $this->mimeType; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * Gets the file name. | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @return FileName | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     public function name() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return $this->name; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * Overwrites the file. | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * @param FileName     $aName     The file name | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * @param FileMimeType $aMimeType The file mime type | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function overwrite(FileName $aName, FileMimeType $aMimeType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $this->name = $aName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $this->mimeType = $aMimeType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $this->updatedOn = new \DateTimeImmutable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->publish(new FileOverwritten($this->id())); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * Removes the file. | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function remove() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $this->publish(new FileRemoved($this->id())); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * Renames the file. | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * @param FileName $aName The file name | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      * @throws FileNameInvalidException when the file name extension is different of the current | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public function rename(FileName $aName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         if ($aName->extension() !== $this->name()->extension()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             throw new FileNameInvalidException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $this->name = $aName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $this->updatedOn = new \DateTimeImmutable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $this->publish(new FileRenamed($this->id())); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      * Gets the updated on. | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |      * @return \DateTimeImmutable | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     public function updatedOn() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         return $this->updatedOn; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      * Magic method that represents the file domain object in string format. | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     public function __toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         return $this->name->filename(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |      * Gets the available mime types in scalar type. | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |      * This method is an extension point that it allows | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |      * to add more mime types easily in the domain. | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     public static function availableMimeTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         return FileMimeType::mimeTypes(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |      * Sets the given mime type in the file scope. | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |      * @param FileMimeType $aMimeType The mime type | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |      * @throws FileMimeTypeDoesNotSupportException when the given mime type is not support for the file | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     private function setMimeType(FileMimeType $aMimeType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         if (!in_array($aMimeType->mimeType(), $this->availableMimeTypes(), true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             throw new FileMimeTypeDoesNotSupportException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         $this->mimeType = $aMimeType; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 204 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 205 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |  |