<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


class Attachment extends Datamodul implements Kufer\Mail\Attachment\IEMailAttachment
{
  public $name;
  public $localname;
  public $type; 
  public $size;
  public $errorMsg;
  public $errorType; 
  public $nr; 
  public $typ; 
  public $versandtVom;
  public $bearbeitetVon;
  public $daten;
  public $beschreibung; 
  public $nachweis; 

  public function __construct($pName = '', $pLocalname = '', $pType = '', $pSize = '', $pErrorType = UPLOAD_ERR_OK, $pErrorMsg = '', $pBeschreibung = '', $pNachweis = '')
  {
    parent::__construct();
    $this->name = $pName;
    $this->localname = $pLocalname;
    $this->type = $pType;
    $this->errorMsg = $pErrorMsg;
    $this->errorType = $pErrorType;
    $this->size = $pSize;
    $this->nachweis = $pNachweis;
    $this->beschreibung = $pBeschreibung;
  }

  protected function getFilepath(): string
  {
    if (\file_exists($this->localname)) {
      return $this->localname;
    }

    return $this->name;
  }

  public function getContent(): string
  {
    return \file_get_contents($this->getFilepath());
  }

  public function getContentType(): string
  {
    if (isset($this->type)) {
      return $this->type;
    }

    $result = \mime_content_type($this->getFilepath());
    if ($result === false) {
      return 'application/octetstream';
    } else {
      return $result;
    }
  }

  public function getName(): string
  {
    return \basename($this->name);
  }
}