Passed
Push — master ( 4580ba...ba1857 )
by
unknown
12:54
created

PostsController::author()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Api\V1;
4
5
use Illuminate\Http\Request;
6
7
use App\Models\Post;
8
use App\Models\Author;
9
10
use Validator;
11
12
class PostsController extends BaseController
13
{
14
    protected $model = \App\Models\Post::class;
15
16
17
    /**
18
     * Get post by ID.
19
     *
20
     * @param int $id
21
     * @return \Illuminate\Http\Response
22
     */
23
    public function show($id)
24
    {
25
        $post = Post::with('author')->findOrFail($id);
26
27
        return $this->response->array($post->toArray());
28
    }
29
30
31
32
    /**
33
     * Get post author.
34
     *
35
     * @param int $id
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function author($id)
39
    {
40
        $post = Post::findOrFail($id);
41
        if (! $post->author) {
42
            // @todo 
43
            // use Dingo response instead
44
            return response()->json([
45
                'status' => 'error', 
46
                'message' => 'Resource not found', 
47
                'errors' => 'Could not find author for post with ID: '.$id,
48
            ], 404);
49
        }
50
51
        return $this->response->array($post->author->toArray());
52
    }
53
54
55
    /**
56
     * Get post images.
57
     *
58
     * @param int $id
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function images($id)
62
    {
63
        $post = Post::with('images')->findOrFail($id);
64
65
        return $this->response->array($post->images->toArray());
66
    }
67
68
    /**
69
     * Get store validator.
70
     *
71
     * @param \Illuminate\Http\Request $request
72
     * @return \Illuminate\Validation\Validator
73
     */
74
    protected function validator(Request $request)
75
    {
76
        return Validator::make($request->all(), [
77
            'author_id' => 'required|integer|exists:authors,id',
78
            'title' => 'required|unique:posts|max:255',
79
        ]);
80
    }
81
}
82