<?php

namespace App\EntityListener;

use App\Entity\Negotiation;
use App\Service\NegotiationEmailGenerator;
use Doctrine\ORM\Mapping\PostPersist;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class NegotiationListener
{
    /**
     * @var NegotiationEmailGenerator
     */
    private $generator;
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(NegotiationEmailGenerator $generator, ContainerInterface $container)
    {
        $this->generator = $generator;
        $this->container = $container;
    }

    /**
     * @PostPersist
     *
     * @param Negotiation $negotiation
     */
    public function createEmail(Negotiation $negotiation)
    {
        $negotiation->setEmail($this->generator->generate((string) $negotiation->getId()));

        $negotiationService = $this->container->get('App\Service\NegotiationService');

        $negotiationService->save($negotiation);
    }
}
