Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
14 / 14
DefaultCycleDetector
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
14 / 14
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 canProceed
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
10 / 10
 addVisit
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
<?php
declare(strict_types = 1);
namespace Siesta\Util;
use Siesta\Contract\CycleDetector;
/**
 * @author Gregor Müller
 */
class DefaultCycleDetector implements CycleDetector
{
    /**
     * @var array
     */
    private $visitedList;
    /**
     *
     */
    public function __construct()
    {
        $this->visitedList = [];
    }
    /**
     * @param $tableName
     * @param $visitor
     *
     * @return bool
     */
    public function canProceed($tableName, $visitor)
    {
        $entityList = ArrayUtil::getFromArray($this->visitedList, $tableName);
        if ($entityList === null) {
            $this->visitedList [$tableName] = [];
            $this->addVisit($tableName, $visitor);
            return true;
        }
        foreach ($entityList as $entity) {
            if ($visitor->arePrimaryKeyIdentical($entity)) {
                return false;
            }
        }
        $this->addVisit($tableName, $visitor);
        return true;
    }
    /**
     * @param $technicalName
     * @param $visitor
     */
    private function addVisit($technicalName, $visitor)
    {
        $this->visitedList [$technicalName] [] = $visitor;
    }
}