Exemplo n.º 1
0
 /**
  * @param UploadedFile|string $file
  * @throws \InvalidArgumentException|\RuntimeException
  * @return bool
  */
 public function attachDocument($file)
 {
     $document = new Document();
     if ($file instanceof UploadedFile) {
         $document->filename = $file->name;
         $document->contentType = $file->type;
     } elseif (is_file($file)) {
         $document->filename = basename($file);
         $document->contentType = mime_content_type($file);
     } else {
         throw new \InvalidArgumentException(__METHOD__ . ": unable attach document '{$file}'.'");
     }
     $document->ownerId = $this->getPrimaryKey();
     $document->file = $file;
     if ($document->save()) {
         return true;
     } else {
         throw new \RuntimeException(__METHOD__ . ": unable attach document '{$document->filename}': " . implode(", ", array_keys($document->getErrors())) . " is invalid");
     }
 }