Passed
Push — master ( e20bb5...b3a0d1 )
by Lars
04:05
created

BlogController::editBlog()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 8
nop 1
dl 0
loc 26
ccs 0
cts 15
cp 0
crap 20
rs 9.7998
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 Illuminate\Http\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 getBlogByHash($blogHash)
22
    {
23
        $blog = new Blog();
24
        $blogResult = $blog->where("hash", $blogHash)->first();
25
26
        if ($blogResult != null) {
27
            return FormatHelper::formatData($blogResult);
28
        } else {
29
            return FormatHelper::formatData(array("errorCode" => "blog-not-found"), false, 404);
30
        }
31
    }
32
33
    public function getArticle($articleHash)
34
    {
35
        $article = new BlogArticle();
36
        $articleResult = $article->where("hash", $articleHash)->first();
37
38
        if ($articleResult != null) {
39
            return FormatHelper::formatData($articleResult);
40
        } else {
41
            return FormatHelper::formatData(array("errorCode" => "article-not-found"), false, 404);
42
        }
43
    }
44
45
    public function addBlog(Request $request)
46
    {
47
        $blog = new Blog();
48
49
        $name = $request->input("name");
50
        $description = $request->input("description");
51
        $url = $request->input("url");
52
53
        $blogHash = md5(time());
54
        $dataArray = array(
55
            "hash" => $blogHash,
56
            "name" => $name,
57
            "description" => $description,
58
            "url" => $url
59
        );
60
        $blog->create($dataArray);
61
        return $dataArray;
62
    }
63
64
    public function editBlog(Request $request)
65
    {
66
        $blog = new Blog();
67
68
        $blogHash = $request->input("hash");
69
        $name = $request->input("name");
70
        $description = $request->input("description");
71
        $url = $request->input("url");
72
73
        $dataArray = array();
74
75
        if ($name != null) {
76
            $dataArray["name"] = $name;
77
        }
78
79
        if ($description != null) {
80
            $dataArray["description"] = $description;
81
        }
82
83
        if ($url != null) {
84
            $dataArray["url"] = $url;
85
        }
86
87
        $blog->where("hash", $blogHash)->update($dataArray);
88
89
        return $dataArray;
90
    }
91
92
    public function deleteBlog($blogHash)
93
    {
94
        $blog = new Blog();
95
        $blogResult = $blog->where("hash", $blogHash)->first();
96
        if ($blogResult != null) {
97
            $blogResult->delete();
98
            return FormatHelper::formatData(array(), true);
99
        } else {
100
            return FormatHelper::formatData(array("errorCode" => "blog-not-found"), false, 404);
101
        }
102
    }
103
}