GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#21)
by
unknown
02:22
created

Elasticsearch::insertManyToIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 9.2
cc 3
eloc 12
nc 3
nop 1
1
<?php
2
3
namespace Spatie\SearchIndex\SearchIndexHandlers;
4
5
use Elasticsearch\Client;
6
use Spatie\SearchIndex\Searchable;
7
use Spatie\SearchIndex\SearchIndexHandler;
8
9
class Elasticsearch implements SearchIndexHandler
10
{
11
    /**
12
     * @var Elasticsearch
13
     */
14
    protected $elasticsearch;
15
16
    /**
17
     * @var string
18
     */
19
    protected $indexName;
20
21
    /**
22
     * @param Client $elasticsearch
23
     */
24
    public function __construct(Client $elasticsearch)
25
    {
26
        $this->elasticsearch = $elasticsearch;
0 ignored issues
show
Documentation Bug introduced by
It seems like $elasticsearch of type object<Elasticsearch\Client> is incompatible with the declared type object<Spatie\SearchInde...Handlers\Elasticsearch> of property $elasticsearch.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
    }
28
29
    /**
30
     * Set the name of the index that should be used by default.
31
     *
32
     * @param $indexName
33
     *
34
     * @return $this
35
     */
36
    public function setIndexName($indexName)
37
    {
38
        $this->indexName = $indexName;
39
40
        return $this;
41
    }
42
43
    /**
44
     * Add or update the given searchable subject to the index.
45
     *
46
     * @param Searchable $subject
47
     */
48
    public function upsertToIndex(Searchable $subject)
49
    {
50
        $this->elasticsearch->index(
0 ignored issues
show
Bug introduced by
The method index() does not seem to exist on object<Spatie\SearchInde...Handlers\Elasticsearch>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
            [
52
                'index' => $this->indexName,
53
                'type' => $subject->getSearchableType(),
54
                'id' => $subject->getSearchableId(),
55
                'body' => $subject->getSearchableBody(),
56
            ]
57
        );
58
    }
59
60
    /**
61
     * Add multiple searchable subjects to the index.
62
     *
63
     * @param array|Collection $subjects
64
     */
65
    public function insertManyToIndex($subjects = [])
66
    {
67
        $params = [];
68
69
        foreach ($subjects as $subject) {
70
            if (! $subject instanceof Searchable) {
71
                throw new \InvalidArgumentException;
72
            }
73
74
            $params['body'][] = [
75
                "index" => [
76
                    '_id' => $subject->getSearchableId(),
77
                    '_index' => $this->indexName,
78
                    '_type' => $subject->getSearchableType(),
79
                ]
80
            ];
81
82
            $params['body'][] = $subject->getSearchableBody();
83
        }
84
85
        $this->elasticsearch->bulk($params);
0 ignored issues
show
Bug introduced by
The method bulk() does not seem to exist on object<Spatie\SearchInde...Handlers\Elasticsearch>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
    }
87
88
    /**
89
     * Remove the given subject from the search index.
90
     *
91
     * @param Searchable $subject
92
     */
93
    public function removeFromIndex(Searchable $subject)
94
    {
95
        $this->elasticsearch->delete(
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Spatie\SearchInde...Handlers\Elasticsearch>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
            [
97
                'index' => $this->indexName,
98
                'type' => $subject->getSearchableType(),
99
                'id' => $subject->getSearchableId(),
100
            ]
101
        );
102
    }
103
104
    /**
105
     * Remove an item from the search index by type and id.
106
     *
107
     * @param string $type
108
     * @param int    $id
109
     */
110
    public function removeFromIndexByTypeAndId($type, $id)
111
    {
112
        $this->elasticsearch->delete(
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Spatie\SearchInde...Handlers\Elasticsearch>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
            [
114
                'index' => $this->indexName,
115
                'type' => $type,
116
                'id' => $id,
117
            ]
118
        );
119
    }
120
121
    /**
122
     * Remove everything from the index.
123
     *
124
     * @return mixed
125
     */
126
    public function clearIndex()
127
    {
128
        $this->elasticsearch->indices()->delete(['index' => $this->indexName]);
0 ignored issues
show
Bug introduced by
The method indices() does not seem to exist on object<Spatie\SearchInde...Handlers\Elasticsearch>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
    }
130
131
    /**
132
     * Get the results for the given query.
133
     *
134
     * @param array $query
135
     *
136
     * @return mixed
137
     */
138
    public function getResults($query)
139
    {
140
        return $this->elasticsearch->search($query);
0 ignored issues
show
Bug introduced by
The method search() does not seem to exist on object<Spatie\SearchInde...Handlers\Elasticsearch>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
    }
142
143
    /**
144
     * Get the underlying client.
145
     *
146
     * @return Elasticsearch
147
     */
148
    public function getClient()
149
    {
150
        return $this->elasticsearch;
151
    }
152
}
153