Passed
Push — master ( f195a6...b8472b )
by Michael
08:31
created

DefaultController::getUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
crap 12
1
<?php
2
/* Copyright (C) 2015 Michael Giesler, Stephan Kreutzer
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace DembeloMain\Controller\Dashboard;
21
22
use DembeloMain\Document\User;
23
use DembeloMain\Model\FavoriteManager;
24
use DembeloMain\Model\Repository\TopicRepositoryInterface;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
26
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating;
29
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
30
31
/**
32
 * Class DefaultController
33
 * @Route(service="app.controller_dashboard")
34
 */
35
class DefaultController extends Controller
36
{
37
    /**
38
     * @var TopicRepositoryInterface
39
     */
40
    private $topicRepository;
41
42
    /**
43
     * @var FavoriteManager
44
     */
45
    private $favoriteManager;
46
47
    /**
48
     * @var Templating
49
     */
50
    private $templating;
51
52
    /**
53
     * @var TokenStorage
54
     */
55
    private $tokenStorage;
56
57
    /**
58
     * DefaultController constructor.
59
     * @param TopicRepositoryInterface $topicRepository
60
     * @param FavoriteManager          $favoriteManager
61
     * @param Templating               $templating
62
     * @param TokenStorage             $tokenStorage
63
     */
64 1
    public function __construct(TopicRepositoryInterface $topicRepository, FavoriteManager $favoriteManager, Templating $templating, TokenStorage $tokenStorage)
65
    {
66 1
        $this->topicRepository = $topicRepository;
67 1
        $this->favoriteManager = $favoriteManager;
68 1
        $this->templating = $templating;
69 1
        $this->tokenStorage = $tokenStorage;
70 1
    }
71
72
    /**
73
     * @Route("/", name="mainpage")
74
     *
75
     * @return Response
76
     */
77 1
    public function indexAction(): Response
78
    {
79 1
        $topics = $this->topicRepository->findBy([], ['sortKey' => 'ASC'], 8);
80 1
        $favorites = [];
81
82 1
        foreach ($topics as $topic) {
83
            $favoriteId = $this->favoriteManager->getFavorite($topic, $this->getUser());
84
            if (null === $favoriteId) {
85
                $favorites[$topic->getId()] = false;
86
            } else {
87
                $favorites[$topic->getId()] = $favoriteId;
88
            }
89
        }
90
91 1
        return $this->templating->renderResponse(
92 1
            'DembeloMain::dashboard/index.html.twig',
93
            [
94 1
                'topics' => $topics,
95 1
                'favorites' => $favorites,
96
            ]
97
        );
98
    }
99
100
    /**
101
     * @return User|null
102
     */
103
    protected function getUser(): ?User
104
    {
105
        if (null === $token = $this->tokenStorage->getToken()) {
106
            return null;
107
        }
108
109
        if (!\is_object($user = $token->getUser())) {
110
            // e.g. anonymous authentication
111
            return null;
112
        }
113
114
        return $user;
115
    }
116
}
117