Completed
Push — development ( e2df92...98c85a )
by Claudio
02:28
created

ArticleController::one()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Article;
6
use App\Models\ArticleCategory;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
use Laravel\Lumen\Routing\Controller as BaseController;
10
11
/**
12
 * Class ArticleController
13
 * @package App\Http\Controllers
14
 */
15
class ArticleController extends BaseController
16
{
17
    /**
18
     * Render a specific view of Article set
19
     *
20
     * @param string $countryId
21
     * @param string $articleCategory
22
     * @return Response
23
     */
24
    public function many(string $countryId, string $articleCategory): Response
25
    {
26
        $category = ArticleCategory::find(strstr(($articleCategory =
27
            str_replace('.html', '', $articleCategory)), '_', true));
28
29
        $categoryPage = strstr(strrev($articleCategory), '_', true);
30
31
        return $articleCategory == 'front' ? $this->front() :
32
            $this->category($countryId, $category, $categoryPage,
33
                $categoryPage == 1 ? 0 : (10 * ($categoryPage - 1)));
34
    }
35
36
    /**
37
     * Render the Front Page of the Articles Page
38
     *
39
     * @return Response
40
     */
41
    protected function front(): Response
42
    {
43
        return response(view('habbo-web-news.articles-front', ['set' =>
44
            Article::orderBy('id', 'ASC')->limit(10)->get()]));
45
    }
46
47
    /**
48
     * Render a specific Category Articles Page
49
     *
50
     * @TODO: Proper Way to use Country ID
51
     *
52
     * @param string $countryId
53
     * @param ArticleCategory $category
54
     * @param int $categoryPage
55
     * @param int $start
56
     * @return Response
57
     */
58
    protected function category(string $countryId, ArticleCategory $category, int $categoryPage, int $start): Response
0 ignored issues
show
Unused Code introduced by
The parameter $countryId is not used and could be removed.

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

Loading history...
59
    {
60
        $articles = Article::where('id', '>=', $start)
61
            ->limit(10)->get()->filter(function ($item) use ($category) {
62
                return $category->name == 'all' || in_array($category, $item->categories);
63
            });
64
65
        if ($articles->count() == 0)
66
            return response()->json(null, 404);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
68
        return response(view('habbo-web-news.articles-category', [
69
            'category' => $category,
70
            'page' => $categoryPage,
71
            'categories' => ArticleCategory::all(),
72
            'articles' => $articles
73
        ]));
74
    }
75
76
    /**
77
     * Render a specific view of a specific Article
78
     *
79
     * @TODO: Proper Way to use Country ID
80
     *
81
     * @param string $countryId
82
     * @param string $articleName
83
     * @return Response
84
     */
85
    public function one(string $countryId, string $articleName): Response
0 ignored issues
show
Unused Code introduced by
The parameter $countryId is not used and could be removed.

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

Loading history...
86
    {
87
        if (($article = Article::find(strstr($articleName, '_', true))) == null)
88
            return response()->json(null, 404);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
89
90
        $related = ($latest = Article::all())->filter(function ($item) use ($article) {
91
            return in_array($article->categories[0], $item->categories);
92
        });
93
94
        return response(view('habbo-web-news.articles-view', [
95
            'article' => $article,
96
            'latest' => $latest->slice(0, 5),
97
            'related' => $related->slice(0, 5)
98
        ]));
99
    }
100
101
    /**
102
     * Get All Habbo Articles as XML/RSS
103
     *
104
     * @param Request $request
105
     * @return Response
106
     */
107
    public function getRss(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
108
    {
109
        return response(view('habbo-rss', [
0 ignored issues
show
Bug introduced by
The method header does only exist in Illuminate\Http\Response, but not in Laravel\Lumen\Http\ResponseFactory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
110
            'articles' => Article::limit(20)->get()
111
        ]))->header('Content-Type', 'text/xml');
112
    }
113
}
114