Passed
Branch master (8cb5b2)
by Christopher
04:33 queued 12s
created

ElasticsearchRepository::bulkSave()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 1
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 3
rs 9.9
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
2
namespace Triadev\Leopard\Business\Repository;
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
4
use Illuminate\Database\Eloquent\Model;
5
use Illuminate\Support\Collection;
6
use Triadev\Leopard\Business\Helper\IsModelSearchable;
7
use Triadev\Leopard\Contract\Repository\ElasticsearchRepositoryContract;
8
use Triadev\Leopard\Facade\Leopard;
9
use Triadev\Leopard\Searchable;
10
11
class ElasticsearchRepository implements ElasticsearchRepositoryContract
0 ignored issues
show
Coding Style Documentation introduced by
Missing class doc comment
Loading history...
12
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class ElasticsearchRepository
Loading history...
13
    use IsModelSearchable;
14
    
15
    /**
16
     * Save
17
     *
18
     * @param Model|Searchable $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
19
     * @return array
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
20
     *
21
     * @throws \InvalidArgumentException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
22
     */
23 5
    public function save(Model $model): array
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
24
    {
25 5
        $this->isModelSearchable($model);
26
        
27 5
        return Leopard::indexStatement([
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
28 5
            'index' => $model->getDocumentIndex(),
1 ignored issue
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 40 spaces, but found 12.
Loading history...
29 5
            'type' => $model->getDocumentType(),
1 ignored issue
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 40 spaces, but found 12.
Loading history...
30 5
            'id' => $model->getKey(),
1 ignored issue
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 40 spaces, but found 12.
Loading history...
31 5
            'body' => $model->getDocumentData()
1 ignored issue
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 40 spaces, but found 12.
Loading history...
32
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 39 space(s), but found 8.
Loading history...
33
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end save()
Loading history...
34
    
35
    /**
36
     * Update
37
     *
38
     * @param Model|Searchable $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
39
     * @return array
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
40
     *
41
     * @throws \InvalidArgumentException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
42
     */
43 1
    public function update(Model $model): array
44
    {
45 1
        $this->isModelSearchable($model);
46
        
47 1
        return Leopard::updateStatement([
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
48 1
            'index' => $model->getDocumentIndex(),
1 ignored issue
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 41 spaces, but found 12.
Loading history...
49 1
            'type' => $model->getDocumentType(),
1 ignored issue
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 41 spaces, but found 12.
Loading history...
50 1
            'id' => $model->getKey(),
1 ignored issue
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 41 spaces, but found 12.
Loading history...
51
            'body' => [
1 ignored issue
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 41 spaces, but found 12.
Loading history...
52 1
                'doc' => $model->getDocumentData()
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 23 spaces, but found 16.
Loading history...
53
            ]
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 22 space(s), but found 12.
Loading history...
54
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 40 space(s), but found 8.
Loading history...
55
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end update()
Loading history...
56
    
57
    /**
58
     * Delete
59
     *
60
     * @param Model|Searchable $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
61
     * @return bool
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
62
     *
63
     * @throws \InvalidArgumentException
0 ignored issues
show
introduced by
Comment missing for @throws tag in function comment
Loading history...
64
     */
65 1
    public function delete(Model $model): bool
66
    {
67 1
        $this->isModelSearchable($model);
68
        
69
        $params = [
70 1
            'index' => $model->getDocumentIndex(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 19 spaces, but found 12.
Loading history...
71 1
            'type' => $model->getDocumentType(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 19 spaces, but found 12.
Loading history...
72 1
            'id' => $model->getKey(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 19 spaces, but found 12.
Loading history...
73
        ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 18 space(s), but found 8.
Loading history...
74
        
75 1
        if (Leopard::existStatement($params)) {
76 1
            Leopard::deleteStatement($params);
77
        }
78
        
79 1
        return true;
80
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end delete()
Loading history...
81
    
82
    /**
83
     * Bulk save
84
     *
85
     * @param array|Collection $models
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
86
     * @return array
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
87
     */
88 2
    public function bulkSave($models) : array
89
    {
90 2
        $params = [];
91
    
92 2
        $defaultIndex = Leopard::getEsDefaultIndex();
93
        
94 2
        foreach ($models as $model) {
95
            /** @var Model|Searchable $model */
0 ignored issues
show
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
96 2
            $this->isModelSearchable($model);
1 ignored issue
show
Bug introduced by
It seems like $model can also be of type Triadev\Leopard\Searchable; however, parameter $model of Triadev\Leopard\Business...ry::isModelSearchable() does only seem to accept Illuminate\Database\Eloquent\Model, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            $this->isModelSearchable(/** @scrutinizer ignore-type */ $model);
Loading history...
97
            
98 2
            $params['body'][] = [
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
99
                'index' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 33 spaces, but found 16.
Loading history...
100 2
                    '_index' => $model->getDocumentIndex() ?: $defaultIndex,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 28 spaces, but found 20.
Loading history...
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
101 2
                    '_type' => $model->getDocumentType(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 28 spaces, but found 20.
Loading history...
102 2
                    '_id' => $model->getKey()
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 28 spaces, but found 20.
Loading history...
103
                ]
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 27 space(s), but found 16.
Loading history...
104
            ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 32 space(s), but found 12.
Loading history...
105
            
106 2
            $params['body'][] = $model->getDocumentData();
107
        }
108
        
109 2
        return Leopard::bulkStatement($params);
110
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end bulkSave()
Loading history...
111
    
112
    /**
113
     * Bulk delete
114
     *
115
     * @param array $models
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
116
     * @return array
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
117
     */
118 1
    public function bulkDelete(array $models): array
119
    {
120 1
        $params = [];
121
    
122 1
        $defaultIndex = Leopard::getEsDefaultIndex();
123
        
124 1
        foreach ($models as $model) {
125
            /** @var Model|Searchable $model */
0 ignored issues
show
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
126 1
            $this->isModelSearchable($model);
1 ignored issue
show
Bug introduced by
It seems like $model can also be of type Triadev\Leopard\Searchable; however, parameter $model of Triadev\Leopard\Business...ry::isModelSearchable() does only seem to accept Illuminate\Database\Eloquent\Model, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
            $this->isModelSearchable(/** @scrutinizer ignore-type */ $model);
Loading history...
127
        
128 1
            $params['body'][] = [
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
129
                'delete' => [
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 33 spaces, but found 16.
Loading history...
130 1
                    '_index' => $model->getDocumentIndex() ?: $defaultIndex,
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 29 spaces, but found 20.
Loading history...
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
131 1
                    '_type' => $model->getDocumentType(),
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 29 spaces, but found 20.
Loading history...
132 1
                    '_id' => $model->getKey()
0 ignored issues
show
Coding Style introduced by
This array key does not seem to be aligned correctly; expected 29 spaces, but found 20.
Loading history...
133
                ]
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 28 space(s), but found 16.
Loading history...
134
            ];
0 ignored issues
show
Coding Style introduced by
The closing parenthesis does not seem to be aligned correctly; expected 32 space(s), but found 12.
Loading history...
135
        }
136
    
137 1
        return Leopard::bulkStatement($params);
138
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end bulkDelete()
Loading history...
139
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
140