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/SBogers10/ste.komma.pro/app/Components/ComponentType/ComponentTypeFactory.php
<?php


namespace App\Components\ComponentType;

use App\Components\ComponentType\Types\AbstractComponentType;
use App\Components\ComponentType\Types\AccordionFixedImage;
use App\Components\ComponentType\Types\Capterra;
use App\Components\ComponentType\Types\Cards;
use App\Components\ComponentType\Types\DataSecurity;
use App\Components\ComponentType\Types\DoubleText;
use App\Components\ComponentType\Types\Heading;
use App\Components\ComponentType\Types\Image;
use App\Components\ComponentType\Types\NamedText;
use App\Components\ComponentType\Types\ProductTable;
use App\Components\ComponentType\Types\Quote;
use App\Components\ComponentType\Types\References;
use App\Components\ComponentType\Types\Text;
use App\Components\ComponentType\Types\TextImage;
use App\Components\ComponentType\Types\TextUsp;
use App\Components\ComponentType\Types\Video;
use Illuminate\Support\Collection;

/**
 * Class ComponentTypeFactory
 *
 * Knows how to create component types by passing it an ComponentTypes enum.
 *
 * WARNING! The Factory only can create one instance of a componentType per request.
 * If you make componentTypes more then once in a request, you will get the same componentType.
 * This is because they must have unique attribute keys.
 *
 * @package App\Components\ComponentType
 */
class ComponentTypeFactory
{
    private static $maked = [];

    /**
     * Make componentTypes
     *
     * @param int $componentTypeId
     * @return AbstractComponentType|Collection
     * @throws \ReflectionException
     */
    static public function make(int $componentTypeId = null) {
        if($componentTypeId === null) {
            $componentTypes = new Collection();
            foreach(ComponentTypes::getAsArray() as $componentTypeId) {
                if($componentTypeId === 0) continue;
                $componentTypes->push(self::makeSpecificType($componentTypeId));
            }
            $componentTypes = $componentTypes->filter(function($type) {
               return !is_null($type);
            });

            $componentTypes = $componentTypes->keyBy(function(AbstractComponentType $componentType) {
                return $componentType->getId();
            });
            return $componentTypes;
        } else {
            return self::makeSpecificType($componentTypeId);
        }
    }

    /**
     * @param $componentTypeId
     * @return AbstractComponentType
     */
    private static function makeSpecificType($componentTypeId) {
        if(in_array($componentTypeId, array_keys(self::$maked), true)) return self::$maked[$componentTypeId];

        switch ($componentTypeId) {
            case ComponentTypes::TEXT_IMAGE:
                self::$maked[$componentTypeId] = new TextImage();
                break;
            case ComponentTypes::DOUBLE_TEXT:
                self::$maked[$componentTypeId] = new DoubleText();
                break;
            case ComponentTypes::VIDEO:
                self::$maked[$componentTypeId] = new Video();
                break;
            case ComponentTypes::TEXT:
                self::$maked[$componentTypeId] = new Text();
                break;
            case ComponentTypes::IMAGE:
                self::$maked[$componentTypeId] = new Image();
                break;
            case ComponentTypes::NAMED_TEXT:
                self::$maked[$componentTypeId] = new NamedText();
                break;
            case ComponentTypes::TEXT_USP:
                self::$maked[$componentTypeId] = new TextUsp();
                break;
            case ComponentTypes::REFERENCES:
                self::$maked[$componentTypeId] = new References();
                break;
            default:
                throw new \RuntimeException('Type "'.$componentTypeId.'" not makable since it is unknown');
        }

        return self::$maked[$componentTypeId];
    }
}