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%
24 / 24
SingletonPlugin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
11
100.00% covered (success)
100.00%
24 / 24
 getUseClassNameList
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
7 / 7
 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
 generateSingleton
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
10 / 10
 getInstantiationClass
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;
use Siesta\Model\ServiceClass;
/**
 * @author Gregor Müller
 */
class SingletonPlugin extends BasePlugin
{
    const METHOD_SINGLETON = "getInstance";
    /**
     * @var ServiceClass
     */
    protected $serviceClass;
    /**
     * @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();
        }
        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->generateSingleton();
    }
    /**
     *
     */
    protected function generateSingleton()
    {
        $instantiationClass = $this->getInstantiationClass();
        $this->codeGenerator->addStaticProtectedMember("instance", $instantiationClass);
        $method = $this->codeGenerator->newPublicStaticMethod(self::METHOD_SINGLETON);
        $method->setReturnType($instantiationClass);
        $method->addIfStart('self::$instance === null');
        $method->addLine('self::$instance = new ' . $instantiationClass . '();');
        $method->addIfEnd();
        $method->addLine('return self::$instance;');
        $method->end();
    }
    /**
     * @return null|string
     */
    protected function getInstantiationClass()
    {
        $serviceClass = $this->entity->getServiceClass();
        if ($serviceClass !== null && $serviceClass->getClassShortName() !== null) {
            return $serviceClass->getClassShortName();
        }
        return $this->entity->getServiceClassShortName();
    }
}