<?php namespace App\Libs\Backoffice;

use App\Libs\Backoffice\Exceptions\InvalidConfigurationException;
use Illuminate\Support\Collection;

class ContentTypeCollection extends Collection
{
    public function find($type)
    {
        $contentType = $this->filter(function (ContentType $contentTypeItem) use ($type) {
            return ($contentTypeItem->getType() == $type);
        })->first();

        if (empty($contentType)) {
            if (empty($config) || !is_array($config)) {
                throw new InvalidConfigurationException("The content type {$type} doesn't exists");
            }
        }

        return $contentType;
    }

    public function findByModel($model)
    {
        $contentType = $this->filter(function (ContentType $contentTypeItem) use ($model) {
            return ($contentTypeItem->getModelType() == $model);
        })->first();
        if (empty($model)) {
            if (empty($config) || !is_array($config)) {
                throw new InvalidConfigurationException("The Model {$model} doesn't exists");
            }
        }
        return $contentType;
    }
}
