Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
31 / 31
CollectionAccessPlugin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
31 / 31
 getSelectByReferenceName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getDeleteByReferenceName
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getUseClassNameList
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 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
3
100.00% covered (success)
100.00%
7 / 7
 generateSelectByReference
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
11 / 11
 generateDeleteByReference
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
10 / 10
<?php
declare(strict_types = 1);
namespace Siesta\GeneratorPlugin\ServiceClass;
use Siesta\CodeGenerator\CodeGenerator;
use Siesta\Database\StoredProcedureNaming;
use Siesta\GeneratorPlugin\BasePlugin;
use Siesta\Model\Entity;
use Siesta\Model\Reference;
/**
 * @author Gregor Müller
 */
class CollectionAccessPlugin extends BasePlugin
{
    const METHOD_NAME_SELECT_BY_REFERENCE = "getEntityBy%sReference";
    const METHOD_NAME_DELETE_BY_REFERENCE = "deleteEntityBy%sReference";
    /**
     * @param Reference $reference
     *
     * @return string
     */
    public static function getSelectByReferenceName(Reference $reference) : string
    {
        return sprintf(self::METHOD_NAME_SELECT_BY_REFERENCE, $reference->getMethodName());
    }
    /**
     * @param Reference $reference
     *
     * @return string
     */
    public static function getDeleteByReferenceName(Reference $reference) : string
    {
        return sprintf(self::METHOD_NAME_DELETE_BY_REFERENCE, $reference->getMethodName());
    }
    /**
     * @param Entity $entity
     *
     * @return string[]
     */
    public function getUseClassNameList(Entity $entity) : array
    {
        return [];
    }
    /**
     * @return string[]
     */
    public function getDependantPluginList() : array
    {
        return [];
    }
    /**
     * @param Entity $entity
     * @param CodeGenerator $codeGenerator
     */
    public function generate(Entity $entity, CodeGenerator $codeGenerator)
    {
        $this->setup($entity, $codeGenerator);
        foreach ($this->entity->getReferenceList() as $reference) {
            if (!$reference->doesCollectionRefersTo()) {
                continue;
            }
            $this->generateSelectByReference($reference);
            $this->generateDeleteByReference($reference);
        }
    }
    /**
     * @param Reference $reference
     */
    protected function generateSelectByReference(Reference $reference)
    {
        $methodName = self::getSelectByReferenceName($reference);
        $mappingList = $reference->getReferenceMappingList();
        $method = $this->codeGenerator->newPublicMethod($methodName);
        $method->addReferenceMappingListParameter($mappingList);
        $method->addConnectionNameParameter();
        $method->addQuoteReferenceMappingList($mappingList, true);
        // stored procedure invocation
        $spName = StoredProcedureNaming::getSelectByReferenceName($this->entity, $reference);
        $signature = $method->getSPInvocationSignatureFromReferenceMapping($mappingList);
        $method->addLine('return $this->executeStoredProcedure("CALL ' . $spName . ' (' . $signature . ')", $connectionName);');
        $method->end();
    }
    /**
     * @param Reference $reference
     */
    protected function generateDeleteByReference(Reference $reference)
    {
        $methodName = self::getDeleteByReferenceName($reference);
        $mappingList = $reference->getReferenceMappingList();
        $method = $this->codeGenerator->newPublicMethod($methodName);
        $method->addReferenceMappingListParameter($mappingList);
        $method->addConnectionNameParameter();
        $method->addQuoteReferenceMappingList($mappingList, true);
        // stored procedure invocation
        $spName = StoredProcedureNaming::getDeleteByReferenceName($this->entity, $reference);
        $method->addExecuteStoredProcedureWithMappingList($spName, $mappingList, false);
        $method->end();
    }
}