<?php

namespace App\Services;

use App\Repositories\Branch;

class BranchService
{
    private $branchRepository;

    public function __construct(Branch $branchRepository)
    {
        $this->branchRepository = $branchRepository;
    }

    public function save(array $data)
    {
        if (isset($data['id'])) {
            $branch = $this->branchRepository->findOrFail($data['id']);
            $branch->update($data);
        } else {
            $branch = $this->branchRepository->create($data);
        }

        return $branch;
    }

    public function delete($id)
    {
        $branch = $this->branchRepository->findOrFail($id);
        $branch->delete();
    }

    public function get($id)
    {
        return $this->branchRepository->findOrFail($id);
    }

    public function getAll()
    {
        return $this->branchRepository->all();
    }

    public function with(array $children)
    {
        return $this->branchRepository->with($children)->get();
    }

    public function getOneWith($id, array $children)
    {
        return $this->branchRepository->with($children)->findOrFail($id);
    }
}