uruba/FinanCalc

View on GitHub
src/Interfaces/Calculator/CalculatorFactoryAbstract.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace FinanCalc\Interfaces\Calculator {

    use Exception;
    use FinanCalc\Constants\ErrorMessages;
    use ReflectionClass;

    /**
     * Class CalculatorFactoryAbstract
     * @package FinanCalc\Interfaces
     */
    abstract class CalculatorFactoryAbstract
    {
        const MANUFACTURED_CLASS_NAME = null;

        /**
         * @throws Exception
         */
        final public function __construct()
        {
            if (!is_string(static::MANUFACTURED_CLASS_NAME)) {
                throw new Exception(ErrorMessages::getMustDefineManufacturedClassNameMessage());
            }

            if (!class_exists(static::MANUFACTURED_CLASS_NAME)) {
                throw new Exception(ErrorMessages::getClassNotDefinedMessage(static::MANUFACTURED_CLASS_NAME));
            }
        }

        /**
         * @param array $args
         * @return CalculatorAbstract
         */
        final protected function manufactureInstance(array $args)
        {
            $manufacturedClass = new ReflectionClass(static::MANUFACTURED_CLASS_NAME);
            return $manufacturedClass->newInstanceArgs($args);
        }
    }
}