HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/Neopoints/momsecurity.be/app/Komma/Dynamic/ComponentType/ComponentTypeResolver.php
<?php


namespace App\Komma\Dynamic\ComponentType;


use App\Komma\Dynamic\Componentables\ComponentableType;
use App\Komma\Dynamic\ComponentType\SaveState\SaveStateResolver;
use Closure;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Support\Collection;

/**
 * Class ComponentTypeResolver
 *
 * Returns a component
 *
 * @package App\Komma\Dynamic\ComponentType
 */
class ComponentTypeResolver implements ComponentTypeResolverInterface
{
    private function __construct() {} //Do not allow instantiation

    /**
     * Resolve a dynamic group 
     *
     * @param string $nameOrId
     * @return \Illuminate\Database\Eloquent\Collection|ComponentType
     */
    public static function resolve($nameOrId)
    {
        /** @var DatabaseCollection $componentTypes */
        $componentTypes = self::getComponentTypes($nameOrId);
        if($componentTypes->count() == 0) return $componentTypes;

        if(is_a($componentTypes, ComponentType::class)) {
            $componentTypeSaveState = SaveStateResolver::resolve($componentTypes->save_state);
            /** @var ComponentType $componentTypes */
            $componentTypes->setSaveStateInstanceAttribute($componentTypeSaveState);
        } else {
            $componentTypes = $componentTypes->map(function (ComponentType $dynamicGroupType) {
                $dynamicGroupSaveState = SaveStateResolver::resolve($dynamicGroupType->save_state);
                $dynamicGroupType->setSaveStateInstanceAttribute($dynamicGroupSaveState);
                return $dynamicGroupType;
            });
        }

        return $componentTypes;
    }

    /**
     * Resolve all dynamic groups
     *
     * @return DatabaseCollection
     */
    public static function resolveAll(): DatabaseCollection
    {
        /** @var DatabaseCollection $componentTypes */
        $componentTypes = self::getComponentTypes();
        if(!$componentTypes || $componentTypes->count() == 0) return new DatabaseCollection();

        $componentTypes->each(function(ComponentType $dynamicGroupType) {
            $dynamicGroupSaveState = SaveStateResolver::resolve($dynamicGroupType->save_state);
            $dynamicGroupType->setSaveStateInstanceAttribute($dynamicGroupSaveState);
        });

        return $componentTypes;
    }

    /**
     * Returns all dynamicGroups or a specific one.
     *
     * @param string $nameOrId
     * @return DatabaseCollection|ComponentType
     */
    private static function getComponentTypes(string $nameOrId = '') {
        if(is_numeric($nameOrId)) return ComponentType::find((int) $nameOrId);
        if(is_string($nameOrId) && $nameOrId != '') {
            $componentTypes = ComponentType::where('name', '=', $nameOrId)->get();
            if($componentTypes->count() == 1) return $componentTypes->first();
            return new DatabaseCollection();
        }

        return ComponentType::all();
    }

    /**
     * @param ComponentType $componentType
     * @return mixed
     */
    public static function passComponentTypeComponentableTypeModelCollectionsToClosure(ComponentType $componentType, Closure $closure): ?Collection
    {
        $componentType->componentableTypes()->get()->each(function(ComponentableType $componentableType, $key) use($componentType, $closure) {
            $parts = explode('@', $componentableType->model_collection_provider);
            $class = $parts[0];
            $method = $parts[1];
            $instance = null;

            try {
                $instance = \App::make($class);
            } catch (\Exception $e) {
               throw new \RuntimeException('Could get componentType componentable models. An error occured while instantiating the model collection provider class: ');
            }

            if(!is_callable([$instance, $method])) throw new \RuntimeException('Could get componentType componentable models. The method "'.$method.' is not callable on class "'.$class.'"');

            $collection = call_user_func([$instance, $method]);
            if(!is_a($collection, Collection::class)) throw new \RuntimeException('Could get componentType componentable models. The method "'.$method.' from class "'.$class.'" must return an instance of "'.Collection::class.'" but did not.');

            $closure->call($componentType, $collection, $key);
        });


        return null;
    }
}