<?php
/**
 * @author  Magediary
 * @package Magediary_ProductQuestion
 */

namespace Magediary\ProductQuestion\Observer;

use Magediary\ProductQuestion\Model\Question;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class QuestionSaveAfter extends ModelSaveAfter implements ObserverInterface
{
    /**
     * Refresh product(s) cache assigned to the question
     *
     * @param EventObserver $observer
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        /** @var $answer Question */
        $question = $observer->getEvent()->getObject();
        $this->refreshProductCache($question->getQuestionId());

        if ($question->isObjectNew()) {
            $this->sendEmail($question);
        }
    }

    /**
     * Send email to the admin on post a new question
     *
     * @param mixed $question
     */
    private function sendEmail($question)
    {
        $this->mailQuestion->send($question);
    }
}
