Completed
Push — master ( 8e4f56...b416d7 )
by Filipe
02:28
created

RelationsUtilityMethods::setParentRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of slick/orm package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Orm\Mapper\Relation;
11
12
use Slick\Orm\Descriptor\EntityDescriptorInterface;
13
use Slick\Orm\Descriptor\Field\FieldDescriptor;
14
use Slick\Orm\Descriptor\Field\FieldsCollection;
15
use Slick\Orm\Orm;
16
use Slick\Orm\RepositoryInterface;
17
18
/**
19
 * Useful methods for relations
20
 *
21
 * @package Slick\Orm\Mapper\Relation
22
 * @author  Filipe Silva <[email protected]>
23
 */
24
trait RelationsUtilityMethods
25
{
26
27
    /**
28
     * @var RepositoryInterface
29
     */
30
    protected $parentRepository;
31
32
    /**
33
     * Gets the parent or related entity descriptor
34
     *
35
     * @return EntityDescriptorInterface
36
     */
37
    abstract public function getParentEntityDescriptor();
38
39
    /**
40
     * Gets parent entity class name
41
     *
42
     * @return string
43
     */
44
    abstract public function getParentEntity();
45
46
    /**
47
     * Gets the parent entity primary key field name
48
     *
49
     * @return string
50
     */
51 8
    public function getParentPrimaryKey()
52
    {
53 8
        return $this->getParentEntityDescriptor()
54 8
            ->getPrimaryKey()
55 8
            ->getField();
56
    }
57
58
    /**
59
     * Gets parent entity repository
60
     *
61
     * @return \Slick\Orm\Repository\EntityRepository
62
     */
63 12
    public function getParentRepository()
64
    {
65 12
        if (null == $this->parentRepository) {
66 10
            $this->setParentRepository(
67 10
                Orm::getRepository($this->getParentEntity())
68 10
            );
69 10
        }
70 12
        return $this->parentRepository;
71
    }
72
73
    /**
74
     * Sets parent entity repository
75
     * 
76
     * @param RepositoryInterface $repository
77
     * @return $this
78
     */
79 12
    public function setParentRepository(RepositoryInterface $repository)
80
    {
81 12
        $this->parentRepository = $repository;
82 12
        return $this;
83
    }
84
85
    /**
86
     * Gets the entity mapper of parent entity repository
87
     *
88
     * @return \Slick\Orm\EntityMapperInterface
89
     */
90 8
    public function getParentEntityMapper()
91
    {
92 8
        return $this->getParentRepository()
93 8
            ->getEntityMapper();
94
    }
95
96
    /**
97
     * Gets parent entity table name
98
     *
99
     * @return string
100
     */
101 14
    public function getParentTableName()
102
    {
103 14
        return $this->getParentEntityDescriptor()
104 14
            ->getTableName();
105
    }
106
107
    /**
108
     * Get parent entity fields collection
109
     *
110
     * @return FieldsCollection|FieldDescriptor[]
111
     */
112 4
    public function getParentFields()
113
    {
114 4
        return $this->getParentEntityDescriptor()
115 4
            ->getFields();
116
    }
117
}