<?php

namespace App\Service\Campaign;

use App\Entity\Campaign\Bolton as BoltonEntity;
use App\Service\DataGrid\DataGridInterface;
use Doctrine\ORM\EntityNotFoundException;

final class Bolton
{
    /**
     * @var BoltonRepositoryInterface
     */
    private $repository;

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

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

        return $bolton;
    }

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

    /**
     * @return array|null
     */
    public function getByGrid(DataGridInterface $dataGrid): ?array
    {
        $criteria = [];

        return $this->repository->findBy(
            $criteria,
            [
                $dataGrid->getSorters()->first()->getSort() => $dataGrid->getSorters()->first()->getOrder(),
            ],
            $dataGrid->getNavigation()->getRpp(),
            $dataGrid->getNavigation()->getRpp() * $dataGrid->getNavigation()->getPage());
    }

    /**
     * @param DataGridInterface $dataGrid
     *
     * @return int
     */
    public function countByGrid(DataGridInterface $dataGrid): int
    {
        $criteria = [];

        return $this->repository->count($criteria);
    }

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

        return $bolton;
    }

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