Completed
Branch feature/phpstanLevel3 (de378e)
by Schlaefer
02:30
created

CurrentUserFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createLoggedIn() 0 20 1
A createVisitor() 0 11 1
A createDummy() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\User\CurrentUser;
14
15
use Cake\Controller\Controller;
16
use Cake\ORM\TableRegistry;
17
use Saito\User\Cookie\Storage;
18
use Saito\User\CurrentUser\CurrentUser;
19
use Saito\User\CurrentUser\CurrentUserInterface;
20
use Saito\User\LastRefresh\LastRefreshCookie;
21
use Saito\User\LastRefresh\LastRefreshDatabase;
22
use Saito\User\LastRefresh\LastRefreshDummy;
23
use Saito\User\ReadPostings\ReadPostingsCookie;
24
use Saito\User\ReadPostings\ReadPostingsDatabase;
25
use Saito\User\ReadPostings\ReadPostingsDummy;
26
27
/**
28
 * Creates different current-user types
29
 */
30
class CurrentUserFactory
31
{
32
    /**
33
     * Creates a logged-in user
34
     *
35
     * @param array|null $config user configuration
36
     * @return CurrentUserInterface
37
     */
38
    public static function createLoggedIn(?array $config = []): CurrentUserInterface
39
    {
40
        $CurrentUser = new CurrentUser($config);
41
42
        $CurrentUser->setLastRefresh(
43
            new LastRefreshDatabase(
44
                $CurrentUser,
45
                TableRegistry::getTableLocator()->get('Users')
0 ignored issues
show
Compatibility introduced by
\Cake\ORM\TableRegistry:...Locator()->get('Users') of type object<Cake\ORM\Table> is not a sub-type of object<App\Model\Table\UsersTable>. It seems like you assume a child class of the class Cake\ORM\Table to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
46
            )
47
        );
48
        $CurrentUser->setReadPostings(
49
            new ReadPostingsDatabase(
50
                $CurrentUser,
51
                TableRegistry::getTableLocator()->get('UserReads'),
0 ignored issues
show
Compatibility introduced by
\Cake\ORM\TableRegistry:...tor()->get('UserReads') of type object<Cake\ORM\Table> is not a sub-type of object<App\Model\Table\UserReadsTable>. It seems like you assume a child class of the class Cake\ORM\Table to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
52
                TableRegistry::getTableLocator()->get('Entries')
0 ignored issues
show
Compatibility introduced by
\Cake\ORM\TableRegistry:...cator()->get('Entries') of type object<Cake\ORM\Table> is not a sub-type of object<App\Model\Table\EntriesTable>. It seems like you assume a child class of the class Cake\ORM\Table to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
53
            )
54
        );
55
56
        return $CurrentUser;
57
    }
58
59
    /**
60
     * Creates a visitor
61
     *
62
     * @param Controller $controller CakePHP controller access request/response
63
     * @param array|null $config user configuration
64
     * @return CurrentUserInterface
65
     */
66
    public static function createVisitor(Controller $controller, ?array $config = []): CurrentUserInterface
67
    {
68
        $CurrentUser = new CurrentUser($config);
69
70
        $storage = new Storage($controller, 'lastRefresh');
71
        $CurrentUser->setLastRefresh(new LastRefreshCookie($CurrentUser, $storage));
72
        $storage = new Storage($controller, 'Saito-Read');
73
        $CurrentUser->setReadPostings(new ReadPostingsCookie($CurrentUser, $storage));
74
75
        return $CurrentUser;
76
    }
77
78
    /**
79
     * Creates user without persistence (bots, testing)
80
     *
81
     * @param array|null $config user configuration (usually empty)
82
     * @return CurrentUserInterface
83
     */
84
    public static function createDummy(?array $config = []): CurrentUserInterface
85
    {
86
        $CurrentUser = new CurrentUser($config);
87
88
        $CurrentUser->setLastRefresh(new LastRefreshDummy(new CurrentUser([]), $CurrentUser));
89
        $CurrentUser->setReadPostings(new ReadPostingsDummy());
90
91
        return $CurrentUser;
92
    }
93
}
94