DefaultController::getUser()   A
last analyzed

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\Component\HttpFoundation\Response;
27
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating;
28
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
29
30
/**
31
 * Class DefaultController
32
 * @Route(service="app.controller_dashboard")
33
 */
34
class DefaultController
35
{
36
    /**
37
     * @var TopicRepositoryInterface
38
     */
39
    private $topicRepository;
40
41
    /**
42
     * @var FavoriteManager
43
     */
44
    private $favoriteManager;
45
46
    /**
47
     * @var Templating
48
     */
49
    private $templating;
50
51
    /**
52
     * @var TokenStorage
53
     */
54
    private $tokenStorage;
55
56
    /**
57
     * DefaultController constructor.
58
     * @param TopicRepositoryInterface $topicRepository
59
     * @param FavoriteManager          $favoriteManager
60
     * @param Templating               $templating
61
     * @param TokenStorage             $tokenStorage
62
     */
63
    public function __construct(TopicRepositoryInterface $topicRepository, FavoriteManager $favoriteManager, Templating $templating, TokenStorage $tokenStorage)
64
    {
65
        $this->topicRepository = $topicRepository;
66
        $this->favoriteManager = $favoriteManager;
67
        $this->templating = $templating;
68
        $this->tokenStorage = $tokenStorage;
69
    }
70
71
    /**
72
     * @Route("/", name="mainpage")
73
     *
74
     * @return Response
75
     */
76
    public function indexAction(): Response
77
    {
78
        $topics = $this->topicRepository->findBy([], ['sortKey' => 'ASC'], 8);
79
        $favorites = [];
80
81
        foreach ($topics as $topic) {
82
            $favoriteId = $this->favoriteManager->getFavorite($topic, $this->getUser());
83
            if (null === $favoriteId) {
84
                $favorites[$topic->getId()] = false;
85
            } else {
86
                $favorites[$topic->getId()] = $favoriteId;
87
            }
88
        }
89
90
        return $this->templating->renderResponse(
91
            'DembeloMain::dashboard/index.html.twig',
92
            [
93
                'topics' => $topics,
94
                'favorites' => $favorites,
95
            ]
96
        );
97
    }
98
99
    /**
100
     * @return User|null
101
     */
102
    protected function getUser(): ?User
103
    {
104
        if (null === $token = $this->tokenStorage->getToken()) {
105
            return null;
106
        }
107
108
        if (!\is_object($user = $token->getUser())) {
109
            // e.g. anonymous authentication
110
            return null;
111
        }
112
113
        return $user;
114
    }
115
}
116