Completed
Pull Request — master (#7)
by Lars
03:19
created

BlogController::editBlog()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 8
nop 2
dl 0
loc 25
ccs 0
cts 14
cp 0
crap 20
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Blog;
6
use App\BlogArticle;
7
use App\Helper\FormatHelper;
8
use Request;
9
10
/**
11
 * Class BlogController
12
 * @package App\Http\Controllers
13
 */
14
class BlogController extends Controller
15
{
16
    public function index()
17
    {
18
        return FormatHelper::formatData(Blog::all());
19
    }
20
21
    public function getBlogWithArticles($blogHash)
22
    {
23
        $blog = new Blog();
24
        $blogResult = $blog->where("hash", $blogHash)->first();
25
26
        if ($blogResult != null) {
27
            $blogArticle = new BlogArticle();
28
            $blogResult["articles"] = $blogArticle->where("blogHash", $blogHash)->orderBy("created_at")->get();
29
30
            return FormatHelper::formatData($blogResult);
31
        } else {
32
            return FormatHelper::formatData(array("errorCode" => "not-found"), false, 404);
33
        }
34
    }
35
36
    public function getArticle($articleHash)
37
    {
38
        $article = new BlogArticle();
39
        $articleResult = $article->where("hash", $articleHash)->first();
40
41
        if ($articleResult != null) {
42
            return FormatHelper::formatData($articleResult);
43
        } else {
44
            return FormatHelper::formatData(array("errorCode" => "not-found"), false, 404);
45
        }
46
    }
47
48
    public function addBlog(Request $request)
49
    {
50
        $blog = new Blog();
51
52
        $name = $request->input("name");
0 ignored issues
show
Bug introduced by
The method input() does not exist on Illuminate\Support\Facades\Request. ( Ignorable by Annotation )

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

52
        /** @scrutinizer ignore-call */ 
53
        $name = $request->input("name");

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        $description = $request->input("description");
54
        $url = $request->input("url");
55
56
        $blogHash = md5(time());
57
        $dataArray = array(
58
            "hash" => $blogHash,
59
            "name" => $name,
60
            "description" => $description,
61
            "url" => $url
62
        );
63
        $blog->create($dataArray);
64
    }
65
66
    public function editBlog(Request $request, $blogHash)
67
    {
68
        $blog = new Blog();
69
70
        $name = $request->input("name");
71
        $description = $request->input("description");
72
        $url = $request->input("url");
73
74
        $dataArray = array();
75
76
        if ($name != null) {
77
            $dataArray["name"] = $name;
78
        }
79
80
        if ($description != null) {
81
            $dataArray["description"] = $description;
82
        }
83
84
        if ($url != null) {
85
            $dataArray["url"] = $url;
86
        }
87
88
        $blog->where("hash", $blogHash)->update($dataArray);
89
90
        return $dataArray;
91
    }
92
93
    public function deleteBlog($blogHash)
94
    {
95
        $blog = new Blog();
96
        $blogResult = $blog->where("hash", $blogHash)->first();
97
        if ($blogResult != null) {
98
            $blogResult->delete();
99
            return FormatHelper::formatData(array("errorCode" => "blog-deleted"), true);
100
        } else {
101
            return FormatHelper::formatData(array("errorCode" => "not-found"), false, 404);
102
        }
103
    }
104
}