<?php

declare(strict_types=1);

namespace App\Service;

use App\Entity\Country;
use Doctrine\ORM\EntityNotFoundException;

final class CountryService
{
    /**
     * @var CountryRepositoryInterface
     */
    private $repository;

    /**
     * CountryService constructor.
     *
     * @param CountryRepositoryInterface $repository
     */
    public function __construct(CountryRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

    /**
     * @param int $id
     *
     * @return Country
     *
     * @throws EntityNotFoundException
     */
    public function get(int $id): Country
    {
        $country = $this->repository->findById($id);
        if (!$country) {
            throw new EntityNotFoundException('Country with id '.$id.' does not exist!');
        }

        return $country;
    }

    /**
     * @return array|null
     */
    public function getAll(): ?array
    {
        return $this->repository->findAll();
    }

    /**
     * @param Country $country
     *
     * @return Country
     */
    public function save(Country $country): Country
    {
        $this->repository->save($country);

        return $country;
    }

    /**
     * @param Country $country
     */
    public function delete(Country $country): void
    {
        $this->repository->delete($country);
    }
}
