Completed
Push — master ( c9f804...cc8d01 )
by Sergey
15:10
created

Elasticsearchable   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 10
Bugs 0 Features 1
Metric Value
wmc 12
c 10
b 0
f 1
lcom 2
cbo 4
dl 0
loc 124
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndex() 0 4 1
A getType() 0 4 1
A getParentType() 0 4 1
A validateModelEndpoint() 0 14 3
A setInnerHits() 0 4 1
A getInnerHits() 0 4 1
A fillByResponse() 0 7 1
A fillByInnerHits() 0 7 1
A getPath() 0 4 1
A getPosition() 0 4 1
1
<?php
2
3
namespace Isswp101\Persimmon\Traits;
4
5
use Isswp101\Persimmon\Elasticsearch\DocumentPath;
6
use Isswp101\Persimmon\Elasticsearch\InnerHits;
7
use Isswp101\Persimmon\Elasticsearch\Response;
8
use Isswp101\Persimmon\Exceptions\InvalidModelEndpointException;
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 InvalidModelEndpointException
68
     */
69
    final protected function validateModelEndpoint()
70
    {
71
        if (!$this->getIndex()) {
72
            throw new InvalidModelEndpointException(sprintf(
73
                'Please specify the index for your Elasticsearch model %s', static::class
74
            ));
75
        }
76
77
        if (!$this->getType()) {
78
            throw new InvalidModelEndpointException(sprintf(
79
                'Please specify the type for your Elasticsearch model %s', static::class
80
            ));
81
        }
82
    }
83
84
    /**
85
     * @param InnerHits $innerHits
86
     */
87
    protected function setInnerHits(InnerHits $innerHits)
88
    {
89
        $this->_innerHits = $innerHits;
90
    }
91
92
    /**
93
     * @return InnerHits
94
     */
95
    protected function getInnerHits()
96
    {
97
        return $this->_innerHits;
98
    }
99
100
    /**
101
     * @param array $response
102
     * @return $this
103
     */
104
    public function fillByResponse(array $response)
105
    {
106
        $res = new Response($response);
107
        $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...
108
        $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...
109
        return $this;
110
    }
111
112
    /**
113
     * @param array $response
114
     * @return $this
115
     */
116
    public function fillByInnerHits(array $response)
117
    {
118
        $innerHits = new InnerHits($response);
119
        $this->setInnerHits($innerHits);
120
        $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...
121
        return $this;
122
    }
123
124
    public function getPath()
125
    {
126
        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...
127
    }
128
129
    public function getPosition()
130
    {
131
        return $this->_position;
132
    }
133
}