Completed
Push — master ( 2f2275...2895dc )
by Dan Michael O.
02:57
created

DocumentsController::getDocumentFromSomeId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace Colligator\Http\Controllers;
4
5
use Colligator\Cover;
6
use Colligator\Document;
7
use Colligator\Http\Requests\SearchDocumentsRequest;
8
use Colligator\Search\DocumentsIndex;
9
use Illuminate\Http\Request;
10
11
class DocumentsController extends Controller
12
{
13
    /**
14
     * Display a listing of the resource.
15
     *
16
     * @return Response
17
     */
18
    public function index(SearchDocumentsRequest $request, DocumentsIndex $se)
19
    {
20
        // Query ElasticSearch
21
        $response = $se->search($request);
22
23
        // Build response, include pagination data
24
        $out = [
25
            'warnings' => $request->warnings,
26
            'offset'   => $response['offset'],
27
            'total'    => intval($response['hits']['total']),
28
        ];
29
        $hits = count($response['hits']['hits']);
30
        if ($response['offset'] + $hits < $out['total']) {
31
            $out['continue'] = $response['offset'] + $hits;
32
        }
33
34
        $out['documents'] = [];
35
        foreach ($response['hits']['hits'] as $hit) {
36
            $out['documents'][] = $hit['_source'];
37
        }
38
39
        return response()->json($out);
40
    }
41
42
    /**
43
     * Show the form for creating a new resource.
44
     *
45
     * @return Response
46
     */
47
    public function create()
48
    {
49
        //
50
    }
51
52
    /**
53
     * Store a newly created resource in storage.
54
     *
55
     * @return Response
56
     */
57
    public function store()
58
    {
59
        //
60
    }
61
62
    /**
63
     * Display the specified resource.
64
     *
65
     * @param DocumentsIndex $se
66
     * @param int            $id
67
     *
68
     * @return Response
69
     */
70
    public function show(Request $request, DocumentsIndex $se, $id)
71
    {
72
        if ($request->raw) {
73
            $doc = Document::find($id);
74
        } else {
75
            $doc = $se->get($id);
76
        }
77
78
        if (is_null($doc)) {
79
            return response()->json([
80
                'error' => 'Document not found.',
81
            ]);
82
        } else {
83
            return response()->json([
84
                'document' => $doc,
85
            ]);
86
        }
87
    }
88
89
    /**
90
     * Show the form for editing the specified resource.
91
     *
92
     * @param int $id
93
     *
94
     * @return Response
95
     */
96
    public function edit($id)
97
    {
98
        //
99
    }
100
101
    /**
102
     * Update the specified resource in storage.
103
     *
104
     * @param int $id
105
     *
106
     * @return Response
107
     */
108
    public function update($id)
109
    {
110
        //
111
    }
112
113
    /**
114
     * Remove the specified resource from storage.
115
     *
116
     * @param int $id
117
     *
118
     * @return Response
119
     */
120
    public function destroy($id)
121
    {
122
        //
123
    }
124
125
    protected function getDocumentFromSomeId($document_id)
126
    {
127
        if (strlen($document_id) > 16) {
128
            return Document::with('cover')
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
129
                ->where('bibsys_id', '=', $document_id)
130
                ->firstOrFail();
131
        }
132
        return Document::with('cover')
133
            ->where('id', '=', $document_id)
134
            ->orWhere('id', '=', $document_id)
135
            ->firstOrFail();
136
    }
137
138
    /**
139
     * Show cover.
140
     *
141
     * @return Response
142
     */
143
    public function cover($document_id)
144
    {
145
        $doc = $this->getDocumentFromSomeId($document_id);
146
147
        return response()->json([
148
            'cover' => $doc->cover,
149
        ]);
150
    }
151
152
    /**
153
     * Store cover.
154
     *
155
     * @return Response
156
     */
157
    public function storeCover($document_id, Request $request, DocumentsIndex $se)
158
    {
159
        $this->validate($request, [
160
            'url' => 'required|url',
161
        ]);
162
163
        $doc = $this->getDocumentFromSomeId($document_id);
0 ignored issues
show
Unused Code introduced by
$doc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
164
165
        $doc = Document::with('cover')
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
166
            ->where('id', '=', $document_id)
167
            ->orWhere('bibsys_id', '=', $document_id)
168
            ->firstOrFail();
169
170
        try {
171
            $cover = $doc->storeCover($request->url);
172
        } catch (\ErrorException $e) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
173
            \Log::error('Failed to cache cover ' . $request->url . '. Got error: ' . $e->getMessage());
174
175
            return response()->json([
176
                'result' => 'error',
177
                'error'  => 'Failed to store the cover. Please check that the URL points to a valid image file. Details: ' . $e->getMessage(),
178
            ]);
179
        }
180
181
182
        $se->indexById($document_id);
183
184
        return response()->json([
185
            'result' => 'ok',
186
            'cover'  => $cover->toArray(),
187
        ]);
188
    }
189
190
    /**
191
     * Store description.
192
     *
193
     * @return Response
194
     */
195
    public function storeDescription($document_id, Request $request, DocumentsIndex $se)
196
    {
197
        $this->validate($request, [
198
            'text'       => 'required',
199
            'source'     => 'required',
200
            'source_url' => 'url',
201
        ]);
202
203
        $doc = Document::findOrFail($document_id);
204
        $doc->description = [
205
            'text'       => $request->text,
206
            'source'     => $request->source,
207
            'source_url' => $request->source_url,
208
        ];
209
        $doc->save();
210
211
        \Log::info('Stored new description for ' . $document_id);
212
213
        $se->indexById($document_id);
214
215
        return response()->json([
216
            'result' => 'ok',
217
        ]);
218
    }
219
}
220