Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
25 / 25
NewInstancePlugin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
15
100.00% covered (success)
100.00%
25 / 25
 getUseClassNameList
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
12 / 12
 getDependantPluginList
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
0 / 0
 generate
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 generateNewInstance
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 getConstructCall
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
4 / 4
<?php
declare(strict_types = 1);
namespace Siesta\GeneratorPlugin\ServiceClass;
use Siesta\CodeGenerator\CodeGenerator;
use Siesta\GeneratorPlugin\BasePlugin;
use Siesta\Model\Entity;
/**
 * @author Gregor Müller
 */
class NewInstancePlugin extends BasePlugin
{
    const METHOD_NEW_INSTANCE = "newInstance";
    /**
     * @param Entity $entity
     *
     * @return string[]
     */
    public function getUseClassNameList(Entity $entity) : array
    {
        $useList = [];
        $serviceClass = $entity->getServiceClass();
        if ($serviceClass !== null && $serviceClass->getClassName() !== null) {
            $useList[] = $serviceClass->getClassName();
        }
        if ($serviceClass !== null && $serviceClass->getConstructFactoryClassName() !== null) {
            $useList[] = $serviceClass->getConstructFactoryClassName();
        }
        $constructor = $entity->getConstructor();
        if ($constructor !== null && $constructor->getClassName() !== null) {
            $useList[] = $constructor->getClassName();
        }
        if ($constructor !== null && $constructor->getConstructFactoryClassName() !== null) {
            $useList[] = $constructor->getConstructFactoryClassName();
        }
        return $useList;
    }
    /**
     * @return string[]
     */
    public function getDependantPluginList() : array
    {
        return [];
    }
    /**
     * @param Entity $entity
     * @param CodeGenerator $codeGenerator
     */
    public function generate(Entity $entity, CodeGenerator $codeGenerator)
    {
        $this->setup($entity, $codeGenerator);
        $this->generateNewInstance();
    }
    /**
     *
     */
    protected function generateNewInstance()
    {
        $instantiationClass = $this->entity->getInstantiationClassShortName();
        $method = $this->codeGenerator->newPublicMethod(self::METHOD_NEW_INSTANCE);
        $method->setReturnType($instantiationClass);
        $method->addLine('return ' . $this->getConstructCall() . ';');
        $method->end();
    }
    /**
     * @return string
     */
    protected function getConstructCall()
    {
        $constructor = $this->entity->getConstructor();
        if ($constructor !== null && $constructor->getConstructCall() !== null) {
            return $constructor->getConstructCall();
        }
        return 'new ' . $this->entity->getInstantiationClassShortName() . '()';
    }
}