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') |
|
|
|
|
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
$CurrentUser->setReadPostings( |
49
|
|
|
new ReadPostingsDatabase( |
50
|
|
|
$CurrentUser, |
51
|
|
|
TableRegistry::getTableLocator()->get('UserReads'), |
|
|
|
|
52
|
|
|
TableRegistry::getTableLocator()->get('Entries') |
|
|
|
|
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
|
|
|
|
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.