DefaultPresenter::actionDefault()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace UniMan\Presenters;
4
5
use UniMan\Core\LoginForm;
6
use Nette\Application\Responses\TextResponse;
7
8
class DefaultPresenter extends AbstractBasePresenter
9
{
10
    public function actionDefault($driver = null)
11
    {
12
        $actualDriver = $driver ?: current(array_keys($this->driverStorage->getDrivers()));
13
        $this->driver = $this->driverStorage->getDriver($actualDriver);
14
        $this->template->driver = $actualDriver;
15
    }
16
17
    public function actionLogout($driver = null)
18
    {
19
        $section = $this->getSession('uniman');
20
        if ($driver) {
21
            unset($section->{$driver});
22
        } else {
23
            $section->remove();
0 ignored issues
show
Bug introduced by
The method remove does only exist in Nette\Http\SessionSection, but not in Nette\Http\Session.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
24
        }
25
        $this->redirect('Default:default', $driver);
26
    }
27
28
    public function actionFile($file)
29
    {
30
        $httpResponse = $this->getHttpResponse();
31
        $httpResponse->setHeader('Pragma', null);
32
        $httpResponse->setExpiration('+10 minutes');
33
        $contentTypes = [
34
            'css' => 'text/css',
35
            'js' => 'application/javascript',
36
            'jpg' => 'image/jpeg',
37
            'jpeg' => 'image/jpeg',
38
            'png' => 'image/png',
39
            'ico' => 'image/x-icon',
40
            'eot' => 'application/vnd.ms-fontobject',
41
            'woff' => 'application/font-woff',
42
            'woff2' => 'application/font-woff2',
43
            'ttf' => 'application/x-font-truetype',
44
            'svg' => 'image/svg+xml',
45
            'otf' => 'application/x-font-opentype',
46
        ];
47
        $path = __DIR__ . '/../../www/' . $file;
48
        $extension = pathinfo($path, PATHINFO_EXTENSION);
49
        $contentType = isset($contentTypes[$extension]) ? $contentTypes[$extension] : 'text/plain';
50
        $httpResponse->setContentType($contentType);
51
        $this->sendResponse(new TextResponse(file_get_contents($path)));
52
    }
53
54
    public function renderCheck()
55
    {
56
        $drivers = $this->driverStorage->getDrivers();
57
        $actualDriver = current(array_keys($drivers));
58
        $this->driver = $this->driverStorage->getDriver($actualDriver);
59
        $this->template->driver = $actualDriver;
60
        $this->template->drivers = $drivers;
61
    }
62
63
    protected function createComponentLoginForm()
64
    {
65
        return new LoginForm($this->translator, $this->driverStorage, $this->credentialsStorage, $this->driver->type(), $this->locale);
66
    }
67
}
68