Cancelled
Branch main (caeba9)
by Sammy
12:38 queued 10:31
created

Reception::requires_operator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace HexMakina\kadro\Controllers;
4
5
use HexMakina\kadro\Auth\{Operator,Permission,ACL};
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HexMakina\kadro\Controllers\Operator. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use HexMakina\kadro\Auth\AccessRefusedException;
7
use HexMakina\BlackBox\Auth\OperatorInterface;
8
// use HexMakina\LeMarchand\LeMarchand;
9
10
class Reception extends Kadro
11
{
12
    public function requiresOperator(): bool
13
    {
14
        return false;
15
    }
16
17
    public function welcome(OperatorInterface $operator)
18
    {
19
        $router = $this->get('HexMakina\BlackBox\RouterInterface');
20
        $router->match(); // throws RouterException if no match
21
22
        if ($router->name() === 'identify') {
23
            $this->identify($operator);
24
        }
25
26
        // MVC Cascade
27
        $target_controller = $this->get('Controllers\\'.$router->targetController());
28
29
        if ($target_controller->requiresOperator()) {
30
            if(is_null($operator_id = $this->get('HexMakina\BlackBox\StateAgentInterface')->operatorId()))
31
                $this->checkin();
32
33
            if(is_null($operator = get_class($operator)::exists($operator_id)) || !$operator->isActive()){
34
              $this->checkout();
35
            }
36
        }
37
38
        return $operator;
39
    }
40
41
    public function checkin()
42
    {
43
        $this->display('checkin', 'standalone');
44
        $this->get('HexMakina\BlackBox\StateAgentInterface')->resetMessages();
45
    }
46
47
    public function checkout()
48
    {
49
        $this->get('HexMakina\BlackBox\StateAgentInterface')->destroy();
50
        $this->router()->hop('checkin');
51
    }
52
53
    public function identify($op)
54
    {
55
        try {
56
            $username = $this->router()->submitted('username');
57
            $password = $this->router()->submitted('password');
58
59
            $operator = get_class($op)::exists(['username' => $username]);
60
61
            if (is_null($operator) || !$operator->isActive()) {
62
                throw new \Exception('ERR_DISABLED');
63
            }
64
65
            if (!$operator->passwordVerify($password)) {
66
                throw new \Exception('ERR_WRONG_LOGIN_OR_PASSWORD');
67
            }
68
69
            $this->get('HexMakina\BlackBox\StateAgentInterface')->operatorId($operator->getId());
70
            $this->logger()->notice($this->l('PAGE_CHECKIN_WELCOME', [$operator->name()]));
71
            $this->router()->hop();
72
        } catch (\Exception $e) {
73
            $this->logger()->warning($this->l('KADRO_operator_' . $e->getMessage()));
74
            $this->router()->hop('checkin');
75
        }
76
    }
77
}
78