Completed
Push — bug/414-up-button-leaves-readp... ( 65711c...0272f7 )
by Michael
10:12
created

FinanceNodeController::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
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 6
cp 0
cc 3
eloc 5
nc 3
nop 0
crap 12
1
<?php
2
/* Copyright (C) 2017 Michael Giesler
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;
21
22
use DembeloMain\Document\User;
23
use DembeloMain\Model\FeatureToggle;
24
use DembeloMain\Model\Readpath;
25
use DembeloMain\Model\Repository\TextNodeRepositoryInterface;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating;
30
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
31
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
32
33
34
/**
35
 * Class FinanceNodeController
36
 * @Route(service="app.controller_financenode")
37
 */
38
class FinanceNodeController extends Controller
39
{
40
    /**
41
     * @var Templating
42
     */
43
    private $templating;
44
45
    /**
46
     * @var TokenStorage
47
     */
48
    private $tokenStorage;
49
50
    /**
51
     * @var Readpath
52
     */
53
    private $readpath;
54
55
    /**
56
     * @var FeatureToggle
57
     */
58
    private $featureToggle;
59
60
    /**
61
     * @var TextNodeRepositoryInterface
62
     */
63
    private $textnodeRepository;
64
65
    /**
66
     * @var AuthorizationCheckerInterface
67
     */
68
    private $authorizationChecker;
69
70
    /**
71
     * FinanceNodeController constructor.
72
     *
73
     * @param Templating $templating
74
     * @param TokenStorage $tokenStorage
75
     * @param Readpath $readpath
76
     * @param FeatureToggle $featureToggle
77
     * @param TextNodeRepositoryInterface $textNodeRepository
78
     * @param AuthorizationCheckerInterface $authorizationChecker
79
     */
80
    public function __construct(Templating $templating, TokenStorage $tokenStorage, Readpath $readpath, FeatureToggle $featureToggle, TextNodeRepositoryInterface $textNodeRepository, AuthorizationCheckerInterface $authorizationChecker)
81
    {
82
        $this->templating = $templating;
83
        $this->tokenStorage = $tokenStorage;
84
        $this->readpath = $readpath;
85
        $this->featureToggle = $featureToggle;
86
        $this->textnodeRepository = $textNodeRepository;
87
        $this->authorizationChecker = $authorizationChecker;
88
    }
89
90
    /**
91
     * @Route("/collect/{textnodeArbitraryId}", name="financenode")
92
     *
93
     * @param string $textnodeArbitraryId arbitrary ID of textnode
94
     *
95
     * @return Response
96
     */
97
    public function showAction(string $textnodeArbitraryId): Response
98
    {
99
        if ($this->featureToggle->hasFeature('login_needed') && !$this->authorizationChecker->isGranted('ROLE_USER')) {
100
            return $this->redirectToRoute('login_route');
101
        }
102
103
        $textnode = $this->textnodeRepository->findOneActiveByArbitraryId($textnodeArbitraryId);
104
105
        $user = $this->getUser();
106
107
        $this->readpath->storeReadpath($textnode, $user);
108
109
        return $this->templating->renderResponse(
110
            'DembeloMain::financenode/show.html.twig'
111
        );
112
    }
113
114
    /**
115
     * @return User|null
116
     */
117
    protected function getUser(): ?User
118
    {
119
        if (null === $token = $this->tokenStorage->getToken()) {
120
            return null;
121
        }
122
123
        if (!\is_object($user = $token->getUser())) {
124
            // e.g. anonymous authentication
125
            return null;
126
        }
127
128
        return $user;
129
    }
130
}
131