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

HasMany::getForeignKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 9
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 0
crap 2
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\Common\Utils\Text;
13
use Slick\Orm\Entity\EntityCollection;
14
use Slick\Orm\EntityInterface;
15
use Slick\Orm\Mapper\RelationInterface;
16
17
/**
18
 * HasMany
19
 *
20
 * @package Slick\Orm\Mapper\Relation
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
class HasMany extends AbstractRelation implements RelationInterface
24
{
25
    /**
26
     * Relations utility methods
27
     */
28
    use RelationsUtilityMethods;
29
30
    /**
31
     * @readwrite
32
     * @var integer
33
     */
34
    protected $limit;
35
36
    /**
37
     * @readwrite
38
     * @var string
39
     */
40
    protected $order;
41
42
    /**
43
     * @readwrite
44
     * @var mixed
45
     */
46
    protected $conditions;
47
48
    /**
49
     * BelongsTo relation
50
     *
51
     * @param array|object $options The parameters from annotation
52
     */
53 18
    public function __construct($options)
54
    {
55
        /** @var \Slick\Orm\Annotations\BelongsTo $annotation */
56 18
        $annotation = $options['annotation'];
57 18
        unset($options['annotation']);
58 18
        $options['foreignKey'] = $annotation->getParameter('foreignKey');
59 18
        $options['parentEntity'] = $annotation->getValue();
60 18
        $options['limit'] = $annotation->getParameter('limit');
61 18
        $options['order'] = $annotation->getParameter('order');
62 18
        $options['conditions'] = $annotation->getParameter('conditions');
63
64 18
        parent::__construct($options);
65 18
    }
66
67
    /**
68
     * Loads the entity or entity collection for this relation
69
     *
70
     * @param EntityInterface $entity
71
     *
72
     * @return null|EntityInterface|EntityCollection|EntityInterface[]
73
     */
74 2
    public function load(EntityInterface $entity)
75
    {
76 2
        $repository = $this->getParentRepository();
77 2
        $collection = $repository->find()
78 2
            ->where($this->getConditions($entity))
0 ignored issues
show
Documentation introduced by
$this->getConditions($entity) is of type array<string|integer,array<string|integer,*>>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79 2
            ->limit($this->limit)
80 2
            ->all();
81 2
        return $collection;
82
    }
83
84
    /**
85
     * Gets the relation conditions
86
     *
87
     * @param EntityInterface $entity
88
     * @return array
89
     */
90 2
    protected function getConditions(EntityInterface $entity)
91
    {
92 2
        $field = "{$this->getParentTableName()}.{$this->getForeignKey()}";
93 2
        $property = $this->getPropertyName();
94
        $conditions = [
95 2
            "{$field} = :{$property}" => [
96 2
                ":{$property}" => $entity->getId()
97 2
            ]
98 2
        ];
99 2
        return $conditions;
100
    }
101
102
    /**
103
     * Gets the foreign key field name
104
     *
105
     * @return string
106
     */
107 4 View Code Duplication
    public function getForeignKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109 4
        if (is_null($this->foreignKey)) {
110 4
            $name = $this->getEntityDescriptor()->getTableName();
111 4
            $name = Text::singular(strtolower($name));
112 4
            $this->foreignKey = "{$name}_id";
113 4
        }
114 4
        return $this->foreignKey;
115
    }
116
}