WebController::pageAction()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 28
Code Lines 21

Duplication

Lines 3
Ratio 10.71 %

Importance

Changes 0
Metric Value
dl 3
loc 28
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 7
nop 2
1
<?php
2
3
/**
4
 * @author    Markus Tacker <[email protected]>
5
 * @copyright 2013-2016 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/
6
 */
7
8
namespace BCRM\WebBundle\Controller;
9
10
use BCRM\BackendBundle\Entity\Event\Event;
11
use BCRM\BackendBundle\Entity\Event\EventRepository;
12
use BCRM\BackendBundle\Exception\FileNotFoundException;
13
use BCRM\WebBundle\Content\ContentReader;
14
use Carbon\Carbon;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
16
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
17
use Symfony\Component\Form\FormFactoryInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21
use Symfony\Component\Routing\RouterInterface;
22
23
/**
24
 * Renders the page.
25
 */
26
class WebController
27
{
28
    /**
29
     * @var ContentReader
30
     */
31
    private $reader;
32
33
    /**
34
     * @var \Symfony\Component\Form\FormFactoryInterface
35
     */
36
    private $formFactory;
37
38
    /**
39
     * @var \Symfony\Component\Routing\RouterInterface
40
     */
41
    private $router;
42
43
    /**
44
     * @var \BCRM\BackendBundle\Entity\Event\EventRepository
45
     */
46
    private $eventRepo;
47
48
    /**
49
     * @var \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface
50
     */
51
    private $renderer;
52
53
    /**
54
     * @var int Unix timestamp of assets modification
55
     */
56
    private $assetsVersion;
57
58
    public function __construct(
59
        ContentReader $reader,
60
        FormFactoryInterface $formFactory,
61
        RouterInterface $router,
62
        EventRepository $eventRepo,
63
        EngineInterface $renderer,
64
        $assetsVersion
65
    )
66
    {
67
        $this->reader        = $reader;
68
        $this->formFactory   = $formFactory;
69
        $this->router        = $router;
70
        $this->eventRepo     = $eventRepo;
71
        $this->renderer      = $renderer;
72
        $this->assetsVersion = $assetsVersion;
73
    }
74
75
    /**
76
     * Render the index page.
77
     *
78
     * @param Request $request
79
     *
80
     * @return Response
81
     */
82
    public function indexAction(Request $request)
83
    {
84
        $response = $this->pageAction($request, 'Index');
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->pageAction($request, 'Index'); of type Symfony\Component\HttpFoundation\Response|array adds the type array to the return on line 86 which is incompatible with the return type documented by BCRM\WebBundle\Controlle...Controller::indexAction of type Symfony\Component\HttpFoundation\Response.
Loading history...
85
        if ($response->isNotModified($request)) {
86
            return $response;
87
        }
88
        $nextEvent = $this->eventRepo->getNextEvent();
89
90
        $data = array(
91
            'page'     => $this->reader->getPage('Index.md'),
92
            'path'     => 'Index',
93
            'sponsors' => $this->reader->getPage('Sponsoren/Index.md')
94
        );
95
        if ($nextEvent->isDefined()) {
96
            /* @var Event $event */
97
            $event = $nextEvent->get();
98
            if (Carbon::createFromTimestamp($event->getRegistrationEnd()->getTimestamp())->isFuture()) {
99
                $data ['nextEvent'] = $nextEvent;
100
            }
101
        }
102
        return $this->renderer->renderResponse('BCRMWebBundle:Web:index.html.twig', $data, $response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->pageAction($request, 'Index') on line 84 can also be of type array; however, Symfony\Bundle\Framework...rface::renderResponse() does only seem to accept null|object<Symfony\Comp...ttpFoundation\Response>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
103
    }
104
105
    /**
106
     * Render a content page.
107
     *
108
     * @param Request $request
109
     * @param string  $path
110
     *
111
     * @return Response|array
112
     * @throws NotFoundHttpException
113
     */
114
    public function pageAction(Request $request, $path)
115
    {
116
        try {
117
            $pageInfo = $this->reader->getInfo($path . '.md');
118
        } catch (FileNotFoundException $e) {
119
            throw new NotFoundHttpException();
120
        }
121
        $response = new Response();
122
        $response->setETag($pageInfo->getEtag());
123
        $lastModified = $pageInfo->getLastModified();
124 View Code Duplication
        if ($this->assetsVersion - $pageInfo->getLastModified()->getTimestamp() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
            $lastModified = new \DateTime('@' . $this->assetsVersion);
126
        }
127
        $response->setLastModified($lastModified);
128
        $response->setPublic();
129
        if ($response->isNotModified($request)) {
130
            return $response;
131
        }
132
133
        $p = $this->reader->getPage($path . '.md');
134
        if ($p->isHidden()) throw new NotFoundHttpException();
135
136
        return $this->renderer->renderResponse('BCRMWebBundle:Web:page.html.twig', array(
137
            'page'     => $p,
138
            'path'     => $path,
139
            'sponsors' => $this->reader->getPage('Sponsoren/Index.md')
140
        ), $response);
141
    }
142
143
    /**
144
     * Render a content page without the body. Used for ajax calls.
145
     *
146
     * @param Request $request
147
     * @param         $path
148
     *
149
     * @return Response
150
     */
151
    public function contentAction(Request $request, $path)
152
    {
153
        $pageInfo = $this->reader->getInfo($path . '.md');
154
        $response = new Response();
155
        $response->setETag($pageInfo->getEtag());
156
        $lastModified = $pageInfo->getLastModified();
157 View Code Duplication
        if ($this->assetsVersion - $pageInfo->getLastModified()->getTimestamp() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
            $lastModified = new \DateTime('@' . $this->assetsVersion);
159
        }
160
        $response->setLastModified($lastModified);
161
        $response->setPublic();
162
        if ($response->isNotModified($request)) {
163
            return $response;
164
        }
165
166
        $p = $this->reader->getPage($path . '.md');
167
        $response->setContent($p->getContent());
168
        return $response;
169
    }
170
}
171