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

RelationsUtilityMethods   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 2
cbo 4
dl 0
loc 94
ccs 23
cts 23
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
getParentEntityDescriptor() 0 1 ?
getParentEntity() 0 1 ?
A getParentPrimaryKey() 0 6 1
A getParentRepository() 0 9 2
A setParentRepository() 0 5 1
A getParentEntityMapper() 0 5 1
A getParentTableName() 0 5 1
A getParentFields() 0 5 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
}