<?php

namespace App\Service;

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

final class StatService
{
    /**
     * @var StatRepositoryInterface
     */
    private $repository;

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

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

        return $stat;
    }

    /**
     * @param string $symbol
     *
     * @return Stat
     * @throws EntityNotFoundException
     */
    public function bySymbol(string $symbol): Stat
    {
        $stat = $this->repository->findOneBySymbol($symbol);
        if (!$stat) {
            throw new EntityNotFoundException('Stat with symbol ' . $symbol . ' does not exist!');
        }

        return $stat;
    }

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

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

        return $stat;
    }

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