Completed
Branch feature/phpstanLevel2 (e9b6b0)
by Schlaefer
02:36
created

CurrentUserFactory::createVisitor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 11
rs 9.9
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->LastRefresh = new LastRefreshDatabase(
45
            $CurrentUser,
46
            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...
47
        );
48
        $CurrentUser->ReadEntries = new ReadPostingsDatabase(
49
            $CurrentUser,
50
            TableRegistry::get('UserReads')
0 ignored issues
show
Compatibility introduced by
\Cake\ORM\TableRegistry::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...
Deprecated Code introduced by
The method Cake\ORM\TableRegistry::get() has been deprecated with message: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Saito\User\ReadPost...$CurrentUser, $storage) of type object<Saito\User\ReadPo...ngs\ReadPostingsCookie> is incompatible with the declared type object<Saito\User\ReadPo...s\ReadPostingsDatabase> of property $ReadEntries.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Saito\User\ReadPostings\ReadPostingsDummy() of type object<Saito\User\ReadPostings\ReadPostingsDummy> is incompatible with the declared type object<Saito\User\ReadPo...s\ReadPostingsDatabase> of property $ReadEntries.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
88
        return $CurrentUser;
89
    }
90
}
91