Completed
Push — master ( 3c671b...7b9b10 )
by Mohamed
07:35
created

DefaultController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4286
cc 1
eloc 9
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Moo\FlashCardBundle 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\FlashCardBundle\Controller;
13
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
16
17
/**
18
 * DefaultController is the default frontend controller (homepage, & card details view).
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 */
22
class DefaultController extends Controller
23
{
24
    /**
25
     * <p>Shows preview of the first n cards, with the options to load more cards (the next n cards).</p>
26
     * <p>Each card is displayed as a thumbnail. Clicking the thumbnail, will expand the card to show all of it
27
     * details.</p>
28
     * <p>There is also, a search text field at the top right side to search the cards.</p>
29
     *
30
     * @ApiDoc(
31
     *  section="Public pages",
32
     *  resource=true,
33
     *  description="The home page."
34
     * )
35
     *
36
     * @return \Symfony\Component\HttpFoundation\Response A Response instance
37
     */
38 1
    public function indexAction()
39
    {
40 1
        $limit = 24;
41 1
        $cardService = $this->get('moo_flashcard.card.repository');
42 1
        $cards = $cardService->fetchCards(1, $limit);
43 1
        $categories = $this->get('moo_flashcard.category.repository')->fetchCategories();
44
45 1
        return $this->render('MooFlashCardBundle:Default:index.html.twig', [
46 1
            'cards'      => $cards,
47 1
            'limit'      => $limit,
48 1
            'categories' => $categories,
49
        ]);
50
    }
51
52
    /**
53
     * Shows the details of a card.
54
     *
55
     * @ApiDoc(
56
     *  section="Public pages",
57
     *  resource=true,
58
     *  description="Displays a card details.",
59
     *   statusCodes={
60
     *      200="Returned when successful",
61
     *      404="Returned when the card is not found"
62
     *   }
63
     * )
64
     *
65
     * @param string $slug The slug value of a card.
66
     *
67
     * @return \Symfony\Component\HttpFoundation\Response A Response instance
68
     */
69 2
    public function viewAction($slug)
70
    {
71 2
        $cardService = $this->get('moo_flashcard.card.repository');
72 2
        $card = $cardService->findOneBySlugJoinedToCategory($slug);
73 2
        if (!$card) {
74 1
            throw $this->createNotFoundException('The flash card you are looking for does not exist');
75
        }
76 1
        $this->updateViews($card, $cardService);
77
78 1
        return $this->render('MooFlashCardBundle:Default:view.html.twig', [
79 1
            'card'  => $card,
80
            'popup' => null,
81
        ]);
82
    }
83
84
    /**
85
     * Update flashcard view counter
86
     *
87
     * @param \Moo\FlashCardBundle\Entity\Card     $card
88
     * @param \Moo\FlashCardBundle\Repository\Card $cardService
89
     *
90
     * @return \Moo\FlashCardBundle\Entity\Card
91
     */
92 1
    protected function updateViews($card, $cardService)
93
    {
94 1
        if ($card) {
95 1
            $viewsService = $this->get('moo_flashcard.cardview.repository');
96 1
            $status = $viewsService->insertView($card, $this->container->get('request')->getClientIp());
97 1
            if ($status) {
98 1
                $cardService->incrementViews($card);
99
            }
100
        }
101
102 1
        return $card;
103
    }
104
}
105