Completed
Push — master ( be83ab...fe4249 )
by Sergey
44:41 queued 29:39
created

HasManyRelationship::save()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 4
nop 1
1
<?php
2
3
namespace Isswp101\Persimmon\Relationship;
4
5
use Isswp101\Persimmon\ElasticsearchModel;
6
use Isswp101\Persimmon\QueryBuilder\Filters\ParentFilter;
7
use Isswp101\Persimmon\QueryBuilder\QueryBuilder;
8
9
class HasManyRelationship
10
{
11
    /**
12
     * @var ElasticsearchModel
13
     */
14
    protected $parent;
15
16
    /**
17
     * @var ElasticsearchModel
18
     */
19
    protected $childClassName;
20
21
    function __construct(ElasticsearchModel $parent, $childClassName)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
    {
23
        $this->parent = $parent;
24
        $this->childClassName = $childClassName;
25
    }
26
27
    /**
28
     * Find all children.
29
     *
30
     * @return ElasticsearchModel[]
31
     */
32
    public function get()
33
    {
34
        $child = $this->childClassName;
35
        $query = new QueryBuilder();
36
        $query->filter(new ParentFilter($this->parent->getId()));
37
        return $child::search($query);
38
    }
39
40
    /**
41
     * Find document by id.
42
     *
43
     * @param mixed $id
44
     * @return ElasticsearchModel
45
     */
46
    public function find($id)
47
    {
48
        $child = $this->childClassName;
49
        $model = $child::findWithParentId($id, $this->parent->getId());
50
        if ($model) {
51
            $model->setParent($this->parent);
52
        }
53
        return $model;
54
    }
55
56
    /**
57
     * Save children.
58
     *
59
     * @param ElasticsearchModel|ElasticsearchModel[] $child
60
     */
61
    public function save($child)
62
    {
63
        /** @var ElasticsearchModel[] $children */
64
        $children = !is_array($child) ? [$child] : $child;
65
        // @TODO: use bulk if count($children) > 1
1 ignored issue
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
        foreach ($children as $child) {
67
            $child->setParent($this->parent);
68
            $child->save();
69
        }
70
    }
71
}