Completed
Push — master ( 29bf34...2c2349 )
by Dan Michael O.
02:32
created

DocumentsController::cover()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
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
        $doc = $this->getDocumentFromSomeId($document_id);
160
161
        try {
162
            if (isset($request->url)) {
163
                if (empty($request->url)) {
164
                    if ($doc->cover) {
165
                        \Log::debug("[DocumentsController] Removing cover from document {$doc->id}");
166
                        $doc->cover->delete();
167
                    } else {
168
                        return response()->json([
169
                            'result' => 'error',
170
                            'error'  => 'There were no cover to remove',
171
                        ]);
172
                    }
173
                    $cover = null;
174
                } else {
175
                    $this->validate($request, [
176
                        'url' => 'required|url',
177
                    ]);
178
                    $cover = $doc->storeCover($request->url);
179
                    $cover = $cover->toArray();
180
                }
181
            } else {
182
                $data = $request->getContent();
183
                $cover = $doc->storeCoverFromBlob($data);
184
                $cover = $cover->toArray();
185
            }
186
        } 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...
187
            \Log::error('Failed to cache cover, got error: ' . $e->getMessage());
188
189
            return response()->json([
190
                'result' => 'error',
191
                'error'  => 'Failed to store the cover. Please check that the URL points to a valid image file. Details: ' . $e->getMessage(),
192
            ]);
193
        }
194
195
196
        $se->indexById($doc->id);
197
198
        return response()->json([
199
            'result' => 'ok',
200
            'cover'  => $cover,
201
        ]);
202
    }
203
204
    /**
205
     * Store description.
206
     *
207
     * @return Response
208
     */
209
    public function storeDescription($document_id, Request $request, DocumentsIndex $se)
210
    {
211
        $this->validate($request, [
212
            'text'       => 'required',
213
            'source'     => 'required',
214
            'source_url' => 'url',
215
        ]);
216
217
        $doc = $this->getDocumentFromSomeId($document_id);
218
219
        $doc->description = [
220
            'text'       => $request->text,
221
            'source'     => $request->source,
222
            'source_url' => $request->source_url,
223
        ];
224
        $doc->save();
225
226
        \Log::info('Stored new description for ' . $doc->id);
227
228
        $se->indexById($doc->id);
229
230
        return response()->json([
231
            'result' => 'ok',
232
        ]);
233
    }
234
235
    /**
236
     * Store "Cannot find cover"
237
     *
238
     * @return Response
239
     */
240
    public function cannotFindCover($document_id, Request $request, DocumentsIndex $se)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
241
    {
242
        $doc = $this->getDocumentFromSomeId($document_id);
243
244
        try {
245
            \Log::debug("[DocumentsController] Adding 'cannotFindCover' status to {$doc->id}");
246
            $doc->setCannotFindCover();
247
            $doc->save();
248
249
        } 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...
250
            \Log::error('Failed to store status, got error: ' . $e->getMessage());
251
252
            return response()->json([
253
                'result' => 'error',
254
                'error'  => 'Failed to store status. Details: ' . $e->getMessage(),
255
            ]);
256
        }
257
258
        $se->indexById($doc->id);
259
260
        return response()->json([
261
            'result' => 'ok',
262
            'cannot_find_cover' => $doc->cannot_find_cover,
263
        ]);
264
    }
265
}
266