<?php

namespace PeytzMail\Entity;

/**
 * @package PeytzMail
 */
class TrackingEvent extends AbstractEntity
{
    protected $trackId      = '';
    protected $subscriberId = '';
    protected $email        = '';
    protected $foreignId    = '';
    protected $created      = null;
    protected $type         = null;
    protected $newsletterId = null;

    /**
     * Create the tracking event entity from the given stdClass object.
     *
     * @param \stdClass $object
     */
    public function createFromObject(\stdClass $object)
    {
        $this->trackId      = $object->track_id;
        $this->subscriberId = $object->subscriber_id;
        $this->email        = $object->email;
        $this->foreignId    = $object->foreign_id;
        $this->created      = new \DateTime($object->created);

        if (null === $this->type = $this->getProperty($object, 'type')) {
            unset($this->type);
        }

        if (null === $this->newsletterId = $this->getProperty($object, 'newsletter_id')) {
            unset($this->newsletterId);
        }
    }

    /**
     * Return the property's value of the given object if it exists, null otherwise.
     *
     * @param  \stdClass  $object
     * @param  string     $property
     * @return mixed|null
     */
    protected function getProperty(\stdClass $object, $property)
    {
        return property_exists($object, $property) ? $object->$property : null;
    }
}
