<?php

namespace PeytzMail\Entity;

use PeytzMail\Entity\Subscriber;

/**
 * @package PeytzMail
 */
class Event extends AbstractEntity
{
    protected $eventId         = '';
    protected $created         = null;
    protected $change          = '';
    protected $source          = '';
    protected $subscriberToken = '';
    protected $subscriber      = null;

    /**
     * Create the event entity from the given stdClass object.
     *
     * @param \stdClass $object
     */
    public function createFromObject(\stdClass $object)
    {
        $this->eventId         = $object->event_id;
        $this->created         = new \DateTime($object->created);
        $this->change          = $object->change;
        $this->source          = $object->source;
        $this->subscriberToken = $object->subscriber_token;

        if (!in_array($this->change, array('subscribe', 'unsubscribe'))) {
            return;
        }

        if (!property_exists($object, 'extra_fields')) {
            return;
        }

        $subscriber = new Subscriber;
        $subscriber->createFromObject($object->extra_fields);

        $this->subscriber = $subscriber;
    }
}
