Completed
Pull Request — master (#458)
by Michael
10:40 queued 05:13
created

FinanceNodeController::getUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

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