Completed
Push — master ( 2f124c...66b0c2 )
by Mohamed
11:36 queued 09:28
created

DefaultController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4286
cc 1
eloc 9
nc 1
nop 0
crap 2
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
    public function indexAction()
39
    {
40
        $limit = 24;
41
        $cardService = $this->get('moo_flashcard.card.repository');
42
        $cards = $cardService->fetchCards(1, $limit);
43
        $categories = $this->get('moo_flashcard.category.repository')->fetchCategories();
44
45
        return $this->render('MooFlashCardBundle:Default:index.html.twig', [
46
            'cards'      => $cards,
47
            'limit'      => $limit,
48
            '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
    public function viewAction($slug)
70
    {
71
        $cardService = $this->get('moo_flashcard.card.repository');
72
        $card = $cardService->findOneBySlugJoinedToCategory($slug);
73
        if (!$card) {
74
            throw $this->createNotFoundException('The flash card you are looking for does not exist');
75
        }
76
        $this->updateViews($card, $cardService);
77
78
        return $this->render('MooFlashCardBundle:Default:view.html.twig', [
79
            '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
    protected function updateViews($card, $cardService)
93
    {
94
        if ($card) {
95
            $viewsService = $this->get('moo_flashcard.cardview.repository');
96
            $status = $viewsService->insertView($card, $this->container->get('request')->getClientIp());
97
            if ($status) {
98
                $cardService->incrementViews($card);
99
            }
100
        }
101
102
        return $card;
103
    }
104
}
105