Completed
Push — master ( f63c2e...3d45a5 )
by Sergey
11:19
created

Elasticsearchable::fillByResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
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 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Isswp101\Persimmon\Traits;
4
5
use Exception;
6
use Isswp101\Persimmon\Elasticsearch\DocumentPath;
7
use Isswp101\Persimmon\Elasticsearch\InnerHits;
8
use Isswp101\Persimmon\Elasticsearch\Response;
9
10
trait Elasticsearchable
11
{
12
    /**
13
     * @var string
14
     */
15
    protected static $index;
16
17
    /**
18
     * @var string
19
     */
20
    protected static $type;
21
22
    /**
23
     * @var string
24
     */
25
    protected static $parentType;
26
27
    /**
28
     * @var InnerHits
29
     */
30
    public $_innerHits;
31
32
    /**
33
     * @var float
34
     */
35
    public $_score;
36
37
    /**
38
     * @var int
39
     */
40
    public $_position;
41
42
    /**
43
     * @return string
44
     */
45
    final public static function getIndex()
46
    {
47
        return static::$index;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    final public static function getType()
54
    {
55
        return static::$type;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    final public static function getParentType()
62
    {
63
        return static::$parentType;
64
    }
65
66
    /**
67
     * @throws \Exception
68
     */
69
    final protected function validateIndexAndType()
70
    {
71
        if (!$this->getIndex()) {
72
            throw new Exception('Please specify the index for your Elasticsearch model');
73
        }
74
75
        if (!$this->getType()) {
76
            throw new Exception('Please specify the type for your Elasticsearch model');
77
        }
78
    }
79
80
    /**
81
     * @param InnerHits $innerHits
82
     */
83
    protected function setInnerHits(InnerHits $innerHits)
84
    {
85
        $this->_innerHits = $innerHits;
86
    }
87
88
    /**
89
     * @return InnerHits
90
     */
91
    protected function getInnerHits()
92
    {
93
        return $this->_innerHits;
94
    }
95
96
    /**
97
     * @param array $response
98
     * @return $this
99
     */
100
    public function fillByResponse(array $response)
101
    {
102
        $res = new Response($response);
103
        $this->fill($res->getSource());
0 ignored issues
show
Bug introduced by
It seems like fill() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
104
        $this->setId($res->getId());
0 ignored issues
show
Bug introduced by
It seems like setId() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
105
        return $this;
106
    }
107
108
    /**
109
     * @param array $response
110
     * @return $this
111
     */
112
    public function fillByInnerHits(array $response)
113
    {
114
        $innerHits = new InnerHits($response);
115
        $this->setInnerHits($innerHits);
116
        $this->setParentId($innerHits->getParentId($this->getParentType()));
0 ignored issues
show
Bug introduced by
It seems like setParentId() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
117
        return $this;
118
    }
119
120
    public function getPath()
121
    {
122
        return new DocumentPath($this->getIndex(), $this->getType(), $this->getId());
0 ignored issues
show
Bug introduced by
It seems like getId() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
123
    }
124
}