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->LastRefresh = new LastRefreshDatabase( |
45
|
|
|
$CurrentUser, |
46
|
|
|
TableRegistry::getTableLocator()->get('Users') |
|
|
|
|
47
|
|
|
); |
48
|
|
|
$CurrentUser->ReadEntries = new ReadPostingsDatabase( |
49
|
|
|
$CurrentUser, |
50
|
|
|
TableRegistry::get('UserReads') |
|
|
|
|
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
return $CurrentUser; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Creates a visitor |
58
|
|
|
* |
59
|
|
|
* @param Controller $controller CakePHP controller access request/response |
60
|
|
|
* @param array|null $config user configuration |
61
|
|
|
* @return CurrentUserInterface |
62
|
|
|
*/ |
63
|
|
|
public static function createVisitor(Controller $controller, ?array $config = []): CurrentUserInterface |
64
|
|
|
{ |
65
|
|
|
$CurrentUser = new CurrentUser($config); |
66
|
|
|
|
67
|
|
|
$storage = new Storage($controller, 'lastRefresh'); |
68
|
|
|
$CurrentUser->LastRefresh = new LastRefreshCookie($CurrentUser, $storage); |
69
|
|
|
$storage = new Storage($controller, 'Saito-Read'); |
70
|
|
|
$CurrentUser->ReadEntries = new ReadPostingsCookie($CurrentUser, $storage); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return $CurrentUser; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Creates user without persistence (bots, testing) |
77
|
|
|
* |
78
|
|
|
* @param array|null $config user configuration (usually empty) |
79
|
|
|
* @return CurrentUserInterface |
80
|
|
|
*/ |
81
|
|
|
public static function createDummy(?array $config = []): CurrentUserInterface |
82
|
|
|
{ |
83
|
|
|
$CurrentUser = new CurrentUser($config); |
84
|
|
|
|
85
|
|
|
$CurrentUser->LastRefresh = new LastRefreshDummy(new CurrentUser([]), $CurrentUser); |
86
|
|
|
$CurrentUser->ReadEntries = new ReadPostingsDummy(); |
|
|
|
|
87
|
|
|
|
88
|
|
|
return $CurrentUser; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.