<?php 

namespace NPS\Model;

use NPS\Model\User;
use NPS\Model\File;
use NPS\Entity\ErrorReportingQueryEntity;

/**
 * Represents an error reporting query model object
 * @author Lukas Korinek 
 */
final class ErrorReportingQuery extends AbstractUserQuery
{

    /** @var User $user */
    private $user;

    /** @var File $attachment */
    private $attachment;

    public function __construct(ErrorReportingQueryEntity $entity, User $user = null, File $attachment = null)
    {
        $this->user = $user;
        $this->attachment = $attachment;
        parent::__construct($entity);
    }

    /**
     * Determines if the error reporting comes from registrated application user
     * @return bool
     */
    public function isFromApplicationUser()
    {
        return (bool)$this->user;
    }

    /**
     * Gets the email address associated with the query
     * @return string
     */
    public function getEmail()
    {
        if($this->isFromApplicationUser())
        {
            return $this->user->getEmail();
        }

        return $this->entity->email;
    }

    /**
     * Returns user associated with the error report
     * @return User|null
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Returns user history records
     * @return array
     */
    public function getUserHistory()
    {
        return array_filter(explode(';', $this->entity->history));
    }

    /**
     * Gets attached image
     * @return File
     */
    public function getAttachment()
    {
        return $this->attachment;
    }

}

