Completed
Push — master ( fd5325...d7e193 )
by Schlaefer
05:54 queued 03:00
created

CurrentUserFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createLoggedIn() 0 21 1
A createVisitor() 0 13 1
A createDummy() 0 11 1
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')
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
        $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