|
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\Categories; |
|
18
|
|
|
use Saito\User\Cookie\Storage; |
|
19
|
|
|
use Saito\User\CurrentUser\CurrentUser; |
|
20
|
|
|
use Saito\User\CurrentUser\CurrentUserInterface; |
|
21
|
|
|
use Saito\User\LastRefresh\LastRefreshCookie; |
|
22
|
|
|
use Saito\User\LastRefresh\LastRefreshDatabase; |
|
23
|
|
|
use Saito\User\LastRefresh\LastRefreshDummy; |
|
24
|
|
|
use Saito\User\ReadPostings\ReadPostingsCookie; |
|
25
|
|
|
use Saito\User\ReadPostings\ReadPostingsDatabase; |
|
26
|
|
|
use Saito\User\ReadPostings\ReadPostingsDummy; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Creates different current-user types |
|
30
|
|
|
*/ |
|
31
|
|
|
class CurrentUserFactory |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Creates a logged-in user |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $config user configuration |
|
37
|
|
|
* @return CurrentUserInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function createLoggedIn(array $config = []): CurrentUserInterface |
|
40
|
|
|
{ |
|
41
|
|
|
$CurrentUser = new CurrentUser($config); |
|
42
|
|
|
|
|
43
|
|
|
$CurrentUser->setCategories(new Categories($CurrentUser)); |
|
44
|
|
|
$CurrentUser->setLastRefresh( |
|
45
|
|
|
new LastRefreshDatabase( |
|
46
|
|
|
$CurrentUser, |
|
47
|
|
|
TableRegistry::getTableLocator()->get('Users') |
|
|
|
|
|
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
$CurrentUser->setReadPostings( |
|
51
|
|
|
new ReadPostingsDatabase( |
|
52
|
|
|
$CurrentUser, |
|
53
|
|
|
TableRegistry::getTableLocator()->get('UserReads'), |
|
|
|
|
|
|
54
|
|
|
TableRegistry::getTableLocator()->get('Entries') |
|
|
|
|
|
|
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
|
|
|
$config['user_type'] = 'anon'; |
|
71
|
|
|
$CurrentUser = new CurrentUser($config); |
|
72
|
|
|
|
|
73
|
|
|
$CurrentUser->setCategories(new Categories($CurrentUser)); |
|
74
|
|
|
$storage = new Storage($controller, 'lastRefresh'); |
|
75
|
|
|
$CurrentUser->setLastRefresh(new LastRefreshCookie($CurrentUser, $storage)); |
|
76
|
|
|
$storage = new Storage($controller, 'Saito-Read'); |
|
77
|
|
|
$CurrentUser->setReadPostings(new ReadPostingsCookie($CurrentUser, $storage)); |
|
78
|
|
|
|
|
79
|
|
|
return $CurrentUser; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Creates user without persistence (bots, testing) |
|
84
|
|
|
* |
|
85
|
|
|
* @param array|null $config user configuration (usually empty) |
|
86
|
|
|
* @return CurrentUserInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function createDummy(?array $config = []): CurrentUserInterface |
|
89
|
|
|
{ |
|
90
|
|
|
$config['user_type'] = 'anon'; |
|
91
|
|
|
$CurrentUser = new CurrentUser($config); |
|
92
|
|
|
|
|
93
|
|
|
$CurrentUser->setCategories(new Categories($CurrentUser)); |
|
94
|
|
|
$CurrentUser->setLastRefresh(new LastRefreshDummy(new CurrentUser([]), $CurrentUser)); |
|
95
|
|
|
$CurrentUser->setReadPostings(new ReadPostingsDummy()); |
|
96
|
|
|
|
|
97
|
|
|
return $CurrentUser; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
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.