ArticleController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 71
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getArticlesByBlog() 0 6 1
A addArticle() 0 19 1
A deleteArticle() 0 12 2
A editArticle() 0 26 4
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\BlogArticle;
6
use App\Comments;
7
use App\Helper\FormatHelper;
8
use Illuminate\Http\Request;
9
10
/**
11
 * Class ArticleController
12
 * @package App\Http\Controllers
13
 */
14
class ArticleController extends Controller
15
{
16
    public function getArticlesByBlog($blogHash)
17
    {
18
        $blogArticle = new BlogArticle();
19
        $articleResult = $blogArticle->where("blogHash", $blogHash)->orderBy("created_at")->get();
20
21
        return FormatHelper::formatData($articleResult);
22
    }
23
24
    public function addArticle(Request $request)
25
    {
26
        $article = new BlogArticle();
27
28
        $blogHash = $request->input("blogHash");
29
        $title = $request->input("title");
30
        $author = $request->input("author");
31
        $url = $request->input("url");
32
33
        $articleHash = md5(time());
34
        $dataArray = array(
35
            "hash" => $articleHash,
36
            "blogHash" => $blogHash,
37
            "title" => $title,
38
            "author" => $author,
39
            "url" => $url
40
        );
41
        $article->create($dataArray);
42
        return $dataArray;
43
    }
44
45
    public function editArticle(Request $request)
46
    {
47
        $article = new BlogArticle();
48
49
        $articleHash = $request->input("blogHash");
50
        $title = $request->input("title");
51
        $author = $request->input("author");
52
        $url = $request->input("url");
53
54
        $dataArray = array();
55
56
        if ($title != null) {
57
            $dataArray["title"] = $title;
58
        }
59
60
        if ($author != null) {
61
            $dataArray["author"] = $author;
62
        }
63
64
        if ($url != null) {
65
            $dataArray["url"] = $url;
66
        }
67
68
        $article->where("hash", $articleHash)->update($dataArray);
69
70
        return $dataArray;
71
    }
72
73
    public function deleteArticle($articleHash)
74
    {
75
        $comment = new Comments();
76
77
        $article = new BlogArticle();
78
        $articleResult = $article->where("hash", $articleHash)->first();
79
        if ($articleResult != null) {
80
            $comment->where("articleHash", $articleHash)->delete();
81
            $articleResult->delete();
82
            return FormatHelper::formatData(array(), true);
83
        } else {
84
            return FormatHelper::formatData(array("errorCode" => "article-not-found"), false, 404);
85
        }
86
    }
87
}