Completed
Push — master ( 8c953e...7fbcf6 )
by Thomas
37s
created

OneToMany::addJoin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
3
namespace ORM\Relation;
4
5
use ORM\Entity;
6
use ORM\EntityFetcher;
7
use ORM\EntityManager;
8
use ORM\Exceptions\InvalidConfiguration;
9
use ORM\Relation;
10
11
class OneToMany extends Relation
12
{
13
    /**
14
     * Owner constructor.
15
     *
16
     * @param string $name
17
     * @param string $class
18
     * @param string $opponent
19
     */
20 7
    public function __construct($name, $class, $opponent)
21
    {
22 7
        $this->name = $name;
23 7
        $this->class = $class;
24 7
        $this->opponent = $opponent;
25 7
    }
26
27
    /** {@inheritdoc} */
28 8
    public function fetch(Entity $me, EntityManager $entityManager)
29
    {
30 8
        $reference = $this->getOpponent()->getReference();
31 6
        if (empty($reference)) {
32 2
            throw new InvalidConfiguration('Reference is not defined in opponent');
33
        }
34 4
        $foreignKey = $this->getForeignKey($me, array_flip($reference));
35
36
        /** @var EntityFetcher $fetcher */
37 3
        $fetcher = $entityManager->fetch($this->class);
38 3
        foreach ($foreignKey as $col => $value) {
39 3
            $fetcher->where($col, $value);
40
        }
41
42 3
        return $fetcher;
43
    }
44
45
    /** {@inheritdoc} */
46 6
    public function addJoin(EntityFetcher $fetcher, $join, $alias)
47
    {
48 6
        $expression = [];
49 6
        foreach ($this->getOpponent()->getReference() as $hisVar => $myVar) {
50 6
            $expression[] = $alias . '.' . $myVar . ' = ' . $this->name . '.' . $hisVar;
51
        }
52
53 6
        call_user_func([$fetcher, $join], $this->class, implode(' AND ', $expression), $this->name, [], true);
54 6
    }
55
}
56