Passed
Push — main ( 25353d...351785 )
by Sammy
01:44
created

Reception   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A identify() 0 22 5
A welcome() 0 24 5
A checkout() 0 4 1
A checkin() 0 4 1
A requires_operator() 0 3 1
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\{OperatorInterface,AccessRefusedException};
7
use HexMakina\LeMarchand\LeMarchand;
8
9
class Reception extends Kadro
10
{
11
    public function requires_operator(): bool
12
    {
13
        return false;
14
    }
15
16
    public function welcome(OperatorInterface $operator)
17
    {
18
19
        if ($this->router()->name() === 'identify') {
20
            $this->identify($operator);
21
        }
22
23
        $target_controller = $this->router()->targetController();
24
        $target_controller = $this->get('Controllers\\'.$target_controller);
25
26
27
        if ($target_controller->requires_operator()) {
28
            if (is_null($operator = get_class($operator)::exists($this->get('StateAgent')->operatorId()))) {
29
                $this->router()->hop('checkin');
30
            }
31
32
            if (!$operator->is_active()) {
33
                $this->checkout();
34
                throw new AccessRefusedException();
35
            }
36
            LeMarchand::box()->put('OperatorInterface', $operator);
0 ignored issues
show
Bug introduced by
The method put() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as HexMakina\LeMarchand\LeMarchand. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
            LeMarchand::box()->/** @scrutinizer ignore-call */ put('OperatorInterface', $operator);
Loading history...
37
        }
38
39
        return $operator;
40
    }
41
42
    public function checkin()
43
    {
44
        $this->display('checkin', 'standalone');
45
        $this->logger()->cleanUserReport();
46
    }
47
48
    public function checkout()
49
    {
50
        $this->get('StateAgent')->destroy();
51
        $this->router()->hop('checkin');
52
    }
53
54
    public function identify($op)
55
    {
56
        try {
57
            $username = $this->router()->submitted('username');
58
            $password = $this->router()->submitted('password');
59
60
            $operator = get_class($op)::exists(['username' => $username]);
61
62
            if (is_null($operator) || !$operator->is_active()) {
63
                throw new \Exception('ERR_DISABLED');
64
            }
65
66
            if (!$operator->password_verify($password)) {
67
                throw new \Exception('ERR_WRONG_LOGIN_OR_PASSWORD');
68
            }
69
70
            $this->get('StateAgent')->operatorId($operator->get_id());
71
            $this->logger()->nice($this->l('PAGE_CHECKIN_WELCOME', [$operator->name()]));
72
            $this->router()->hop();
73
        } catch (\Exception $e) {
74
            $this->logger()->warning($this->l('KADRO_operator_' . $e->getMessage()));
75
            $this->router()->hop('checkin');
76
        }
77
    }
78
}
79