Completed
Pull Request — master (#450)
by Michael
12:14 queued 06:39
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
 * Class FinanceNodeController
35
 * @Route(service="app.controller_financenode")
36
 */
37
class FinanceNodeController extends Controller
38
{
39
    /**
40
     * @var Templating
41
     */
42
    private $templating;
43
44
    /**
45
     * @var TokenStorage
46
     */
47
    private $tokenStorage;
48
49
    /**
50
     * @var Readpath
51
     */
52
    private $readpath;
53
54
    /**
55
     * @var FeatureToggle
56
     */
57
    private $featureToggle;
58
59
    /**
60
     * @var TextNodeRepositoryInterface
61
     */
62
    private $textnodeRepository;
63
64
    /**
65
     * @var AuthorizationCheckerInterface
66
     */
67
    private $authorizationChecker;
68
69
    /**
70
     * FinanceNodeController constructor.
71
     *
72
     * @param Templating                    $templating
73
     * @param TokenStorage                  $tokenStorage
74
     * @param Readpath                      $readpath
75
     * @param FeatureToggle                 $featureToggle
76
     * @param TextNodeRepositoryInterface   $textNodeRepository
77
     * @param AuthorizationCheckerInterface $authorizationChecker
78
     */
79
    public function __construct(Templating $templating, TokenStorage $tokenStorage, Readpath $readpath, FeatureToggle $featureToggle, TextNodeRepositoryInterface $textNodeRepository, AuthorizationCheckerInterface $authorizationChecker)
80
    {
81
        $this->templating = $templating;
82
        $this->tokenStorage = $tokenStorage;
83
        $this->readpath = $readpath;
84
        $this->featureToggle = $featureToggle;
85
        $this->textnodeRepository = $textNodeRepository;
86
        $this->authorizationChecker = $authorizationChecker;
87
    }
88
89
    /**
90
     * @Route("/collect/{textnodeArbitraryId}", name="financenode")
91
     *
92
     * @param string $textnodeArbitraryId arbitrary ID of textnode
93
     *
94
     * @return Response
95
     */
96
    public function showAction(string $textnodeArbitraryId): Response
97
    {
98
        if ($this->featureToggle->hasFeature('login_needed') && !$this->authorizationChecker->isGranted('ROLE_USER')) {
99
            return $this->redirectToRoute('login_route');
100
        }
101
102
        $textnode = $this->textnodeRepository->findOneActiveByArbitraryId($textnodeArbitraryId);
103
104
        $user = $this->getUser();
105
106
        $this->readpath->storeReadpath($textnode, $user);
107
108
        return $this->templating->renderResponse(
109
            'DembeloMain::financenode/show.html.twig'
110
        );
111
    }
112
113
    /**
114
     * @return User|null
115
     */
116
    protected function getUser(): ?User
117
    {
118
        if (null === $token = $this->tokenStorage->getToken()) {
119
            return null;
120
        }
121
122
        if (!\is_object($user = $token->getUser())) {
123
            // e.g. anonymous authentication
124
            return null;
125
        }
126
127
        return $user;
128
    }
129
}
130