Completed
Branch feature/phpstanLevel2 (435bc7)
by Schlaefer
02:41
created

CurrentUserFactory::createDummy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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 App\Model\Table\UserReadsTable;
16
use App\Model\Table\UsersTable;
17
use Cake\Controller\Controller;
18
use Cake\ORM\TableRegistry;
19
use Saito\User\Cookie\Storage;
20
use Saito\User\CurrentUser\CurrentUser;
21
use Saito\User\CurrentUser\CurrentUserInterface;
22
use Saito\User\LastRefresh\LastRefreshCookie;
23
use Saito\User\LastRefresh\LastRefreshDatabase;
24
use Saito\User\LastRefresh\LastRefreshDummy;
25
use Saito\User\ReadPostings\ReadPostingsCookie;
26
use Saito\User\ReadPostings\ReadPostingsDatabase;
27
use Saito\User\ReadPostings\ReadPostingsDummy;
28
29
/**
30
 * Creates different current-user types
31
 */
32
class CurrentUserFactory
33
{
34
    /**
35
     * Creates a logged-in user
36
     *
37
     * @param array|null $config user configuration
38
     * @return CurrentUserInterface
39
     */
40
    public static function createLoggedIn(?array $config = []): CurrentUserInterface
41
    {
42
        $CurrentUser = new CurrentUser($config);
43
44
        $CurrentUser->setLastRefresh(
45
            new LastRefreshDatabase(
46
                $CurrentUser,
47
                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...
48
            )
49
        );
50
        $CurrentUser->setReadPostings(
51
            new ReadPostingsDatabase(
52
                $CurrentUser,
53
                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...
54
                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...
55
            )
56
        );
57
58
        return $CurrentUser;
59
    }
60
61
    /**
62
     * Creates a visitor
63
     *
64
     * @param Controller $controller CakePHP controller access request/response
65
     * @param array|null $config user configuration
66
     * @return CurrentUserInterface
67
     */
68
    public static function createVisitor(Controller $controller, ?array $config = []): CurrentUserInterface
69
    {
70
        $CurrentUser = new CurrentUser($config);
71
72
        $storage = new Storage($controller, 'lastRefresh');
73
        $CurrentUser->setLastRefresh(new LastRefreshCookie($CurrentUser, $storage));
74
        $storage = new Storage($controller, 'Saito-Read');
75
        $CurrentUser->setReadPostings(new ReadPostingsCookie($CurrentUser, $storage));
76
77
        return $CurrentUser;
78
    }
79
80
    /**
81
     * Creates user without persistence (bots, testing)
82
     *
83
     * @param array|null $config user configuration (usually empty)
84
     * @return CurrentUserInterface
85
     */
86
    public static function createDummy(?array $config = []): CurrentUserInterface
87
    {
88
        $CurrentUser = new CurrentUser($config);
89
90
        $CurrentUser->setLastRefresh(new LastRefreshDummy(new CurrentUser([]), $CurrentUser));
91
        $CurrentUser->setReadPostings(new ReadPostingsDummy());
92
93
        return $CurrentUser;
94
    }
95
}
96