BlogController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 21
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 28
rs 9.584
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\Post;
7
use App\Services\PostProcesses;
8
use Illuminate\Http\Request;
9
use Illuminate\Http\Response;
10
use Validator;
11
12
class BlogController extends Controller
13
{
14
    /**
15
     * Create a new controller instance.
16
     *
17
     * @return void
18
     */
19
    public function __construct()
20
    {
21
        //
22
    }
23
24
    /**
25
     * Display a listing of the resource.
26
     *
27
     * @param \Illuminate\Http\Request
28
     *
29
     * @return \Illuminate\Http\Response
30
     */
31
    public function index(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

31
    public function index(/** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
32
    {
33
        $publishedPosts = Post::allPublishedPosts()->get();
34
35
        return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...Http\Response::HTTP_OK) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
36
            'code'      => 200,
37
            'message'   => 'Welcome to the '.config('blog.title').' API',
38
            'data'      => [
39
                'title'         => config('blog.title'),
40
                'subtitle'      => config('blog.subtitle'),
41
                'description'   => config('blog.description'),
42
                'total_posts'   => $publishedPosts->count(),
43
                'blog_image'    => config('blog.post_image'),
44
                'latest_post'   => [
45
                    'post_id'           => $publishedPosts->last()->id,
46
                    'post_title'        => $publishedPosts->last()->title,
47
                    'post_subtitle'     => $publishedPosts->last()->subtitle,
48
                    'post_updated_at'   => $publishedPosts->last()->updated_at,
49
                    'post_published_at' => $publishedPosts->last()->published_at,
50
                    'post_subtitle'     => $publishedPosts->last()->subtitle,
51
                ],
52
                'sitemap' => config('app.url').'/sitemap.xml',
53
                'rss'     => [
54
                    'blog' => route('feeds.blog'),
55
                ],
56
57
            ],
58
        ], Response::HTTP_OK);
59
    }
60
61
    /**
62
     * Display the posts paginated.
63
     *
64
     * @param \Illuminate\Http\Request
65
     *
66
     * @return \Illuminate\Http\Response
67
     */
68
    public function posts(Request $request)
69
    {
70
        $validator = Validator::make($request->all(), [
71
            'tag' => 'string|max:255',
72
        ]);
73
74
        if ($validator->fails()) {
75
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...P_UNPROCESSABLE_ENTITY) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
76
                'errors' => $validator->errors(),
77
            ], Response::HTTP_UNPROCESSABLE_ENTITY);
78
        }
79
80
        $tag = $request->get('tag');
81
        $service = new PostProcesses($tag);
82
        $data = $service->getResponse();
83
84
        return response()->json($data['posts'], Response::HTTP_OK);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...Http\Response::HTTP_OK) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
85
    }
86
87
    /**
88
     * Display all the posts.
89
     *
90
     * @param \Illuminate\Http\Request
91
     *
92
     * @return \Illuminate\Http\Response
93
     */
94
    public function allPosts(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

94
    public function allPosts(/** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
95
    {
96
        $publishedPosts = Post::allPublishedPosts()->get();
97
98
        return response()->json($publishedPosts, Response::HTTP_OK);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...Http\Response::HTTP_OK) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
99
    }
100
101
    /**
102
     * Get the Latest Post.
103
     *
104
     * @param \Illuminate\Http\Request
105
     *
106
     * @return \Illuminate\Http\Response
107
     */
108
    public function latestPost(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

108
    public function latestPost(/** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
109
    {
110
        $post = Post::allPublishedPosts()->first();
111
112
        if (!$post) {
113
            $post = null;
114
        }
115
116
        return response()->json($post, Response::HTTP_OK);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...Http\Response::HTTP_OK) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
117
    }
118
119
    /**
120
     * Gets the posts authors.
121
     *
122
     * @param \Illuminate\Http\Request
123
     *
124
     * @return \Illuminate\Http\Response
125
     */
126
    public function getPostsAuthors(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

126
    public function getPostsAuthors(/** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
127
    {
128
        $authors = Post::activeAuthors()->get();
129
130
        if (!$authors) {
0 ignored issues
show
introduced by
$authors is of type Illuminate\Database\Eloquent\Collection, thus it always evaluated to true.
Loading history...
131
            $authors = [];
132
        }
133
134
        return response()->json($authors, Response::HTTP_OK);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...Http\Response::HTTP_OK) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
135
    }
136
137
    /**
138
     * Gets posts by author.
139
     *
140
     * @param \Illuminate\Http\Request
141
     *
142
     * @return \Illuminate\Http\Response
143
     */
144
    public function getPostsByAuthor(Request $request, $author)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

144
    public function getPostsByAuthor(/** @scrutinizer ignore-unused */ Request $request, $author)

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

Loading history...
145
    {
146
        $validator = Validator::make([
147
            'author' => $author,
148
        ], [
149
            'author' => 'string|max:255',
150
        ]);
151
152
        if ($validator->fails()) {
153
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...P_UNPROCESSABLE_ENTITY) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
154
                'errors' => $validator->errors(),
155
            ], Response::HTTP_UNPROCESSABLE_ENTITY);
156
        }
157
158
        $posts = Post::postsByAuthors($author)->get();
159
160
        if (!$posts) {
0 ignored issues
show
introduced by
$posts is of type Illuminate\Database\Eloquent\Collection, thus it always evaluated to true.
Loading history...
161
            $posts = [];
162
        }
163
164
        return response()->json($posts, Response::HTTP_OK);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...Http\Response::HTTP_OK) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
165
    }
166
}
167