ApiController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 43.59%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 79
ccs 17
cts 39
cp 0.4359
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getCards() 0 26 2
A getCard() 0 14 1
A getCategories() 0 18 1
1
<?php
2
3
/*
4
 * This file is part of the Moo\FlashCard package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moo\FlashCard\Controller;
13
14
use Illuminate\Http\Request;
15
use Illuminate\Routing\Controller;
16
use Moo\FlashCard\Api\SearchableCardFilter;
17
use Moo\FlashCard\Entity\Card;
18
use Moo\FlashCard\Entity\Category;
19
use Spatie\QueryBuilder\Filter;
20
use Spatie\QueryBuilder\QueryBuilder;
21
use Illuminate\Database\Eloquent\Builder;
22
23
/**
24
 * RestApiController is the default REST API controller.
25
 *
26
 * @author Mohamed Alsharaf <[email protected]>
27
 */
28
class ApiController extends Controller
29
{
30
    /**
31
     * Get collection of cards
32
     *
33
     * @param  Request                       $request
34
     * @return \Illuminate\Http\JsonResponse
35
     */
36 6
    public function getCards(Request $request)
37
    {
38
        // Parameter for limit by
39 6
        $limit = (int) $request->input('limit');
40
41 6
        $cards = QueryBuilder::for(Card::class)
42 6
            ->defaultSort('title')
43 6
            ->allowedFilters(
44 6
                Filter::exact('id'),
45 6
                'slug',
46 6
                'title',
47 6
                'category_id',
48 6
                Filter::custom('search', SearchableCardFilter::class)
49
            )
50 6
            ->allowedIncludes('category')
51 6
            ->allowedAppends('short_modified')
52 6
            ->active();
53
54 6
        if ($limit) {
55 1
            $cards = $cards->simplePaginate($limit);
56
        } else {
57 5
            $cards = $cards->get();
58
        }
59
60 6
        return response()->json($cards);
61
    }
62
63
    /**
64
     * Get details of a card
65
     *
66
     * @return \Illuminate\Http\JsonResponse
67
     */
68
    public function getCard()
69
    {
70
        $card = QueryBuilder::for(Card::class)
71
            ->defaultSort('title')
72
            ->allowedFilters(
73
                Filter::exact('id'),
74
                'slug'
75
            )
76
            ->allowedIncludes('category')
77
            ->active()
78
            ->first();
79
80
        return response()->json($card);
81
    }
82
83
    /**
84
     * Get collection of categories
85
     *
86
     * @return \Illuminate\Http\JsonResponse
87
     */
88
    public function getCategories()
89
    {
90
        $categories = QueryBuilder::for(Category::class)
91
            ->defaultSort('title')
92
            ->allowedFilters(
93
                Filter::exact('id'),
94
                Filter::custom('search', SearchableCardFilter::class)
95
            )
96
            ->withCount([
97
                'cards' => function (Builder $query) {
98
                    $query->active();
99
                },
100
            ])
101
            ->active()
102
            ->get();
103
104
        return response()->json($categories);
105
    }
106
}
107