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.

Elasticsearch::getResults()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\SearchIndex\SearchIndexHandlers;
4
5
use Elasticsearch\Client;
6
use InvalidArgumentException;
7
use Spatie\SearchIndex\Searchable;
8
use Spatie\SearchIndex\SearchIndexHandler;
9
use Traversable;
10
11
class Elasticsearch implements SearchIndexHandler
12
{
13
    /** @var Elasticsearch */
14
    protected $elasticsearch;
15
16
    /** @var string */
17
    protected $indexName;
18
19
    /**
20
     * @param Client $elasticsearch
21
     */
22
    public function __construct(Client $elasticsearch)
23
    {
24
        $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...
25
    }
26
27
    /**
28
     * Set the name of the index that should be used by default.
29
     *
30
     * @param $indexName
31
     *
32
     * @return $this
33
     */
34
    public function setIndexName($indexName)
35
    {
36
        $this->indexName = $indexName;
37
38
        return $this;
39
    }
40
41
    /**
42
     * Add or update the given searchable subject or array of subjects or Traversable object containing subjects.
43
     *
44
     * @param Searchable|array|Traversable $subject
45
     */
46
    public function upsertToIndex($subject, $indexName = null)
47
    {
48
        $indexName = $this->resolveIndexName($indexName);
49
50
        if ($subject instanceof Searchable) {
51
            $subject = [$subject];
52
        }
53
54
        if (is_array($subject) || $subject instanceof Traversable) {
55
            $searchableItems = collect($subject)
56
                ->each(function ($item) {
57
                    if (!$item instanceof Searchable) {
58
                        throw new InvalidArgumentException();
59
                    }
60
                })
61
                ->flatMap(function ($item) use($indexName) {
62
                    return
63
                        [
64
                            [
65
                                'index' => [
66
                                    '_id'    => $item->getSearchableId(),
67
                                    '_index' => $indexName,
68
                                    '_type'  => $item->getSearchableType(),
69
                                ],
70
                            ],
71
72
                            $item->getSearchableBody(),
73
                        ];
74
                })
75
                ->toArray();
76
77
            $payload['body'] = $searchableItems;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$payload was never initialized. Although not strictly required by PHP, it is generally a good practice to add $payload = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
78
79
            $this->elasticsearch->bulk($payload);
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...
80
81
            return;
82
        }
83
84
        throw new InvalidArgumentException('Subject must be a searchable or array of searchables');
85
    }
86
87
    /**
88
     * Remove the given subject from the search index.
89
     *
90
     * @param Searchable $subject
91
     */
92
    public function removeFromIndex(Searchable $subject, $indexName = null)
93
    {
94
        $indexName = $this->resolveIndexName($indexName);
95
96
        $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...
97
            [
98
                'index' => $indexName,
99
                'type'  => $subject->getSearchableType(),
100
                'id'    => $subject->getSearchableId(),
101
            ]
102
        );
103
    }
104
105
    /**
106
     * Remove an item from the search index by type and id.
107
     *
108
     * @param string $type
109
     * @param int    $id
110
     */
111
    public function removeFromIndexByTypeAndId($type, $id, $indexName = null)
112
    {
113
        $indexName = $this->resolveIndexName($indexName);
114
115
        $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...
116
            [
117
                'index' => $indexName,
118
                'type'  => $type,
119
                'id'    => $id,
120
            ]
121
        );
122
    }
123
124
    /**
125
     * Remove everything from the index.
126
     *
127
     * @return mixed
128
     */
129
    public function clearIndex($indexName = null)
130
    {
131
        $indexName = $this->resolveIndexName($indexName);
132
133
        $this->elasticsearch->indices()->delete(['index' => $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...
134
    }
135
136
    /**
137
     * Get the results for the given query.
138
     *
139
     * @param array $query
140
     *
141
     * @return mixed
142
     */
143
    public function getResults($query)
144
    {
145
        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...
146
    }
147
148
    /**
149
     * Get the underlying client.
150
     *
151
     * @return Elasticsearch
152
     */
153
    public function getClient()
154
    {
155
        return $this->elasticsearch;
156
    }
157
158
    protected function resolveIndexName($indexName = null)
159
    {
160
        if (!$indexName) {
161
            $indexName = $this->indexName;
162
        }
163
164
        return $indexName;
165
    }
166
}
167