Completed
Pull Request — master (#13)
by
unknown
02:32
created

InMemoryUserRepository   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 123
Duplicated Lines 14.63 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 19
Bugs 2 Features 11
Metric Value
wmc 18
c 19
b 2
f 11
lcom 1
cbo 3
dl 18
loc 123
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 1
A findByUsername() 0 8 3
A find() 0 8 3
A supportsClass() 0 4 1
A findByPasswordResetToken() 0 4 1
A getSupportedClass() 0 4 1
A add() 0 4 1
A save() 0 3 1
A update() 9 9 3
A delete() 9 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\User;
4
5
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken;
6
7
/**
8
 * Class InMemoryUserRepository
9
 */
10
class InMemoryUserRepository implements UserRepository, PasswordResetRepository
11
{
12
    /** @var array */
13
    private $users = [];
14
15
    /**
16
     * InMemoryUserRepository constructor.
17
     */
18
    public function __construct()
19
    {
20
        $user = new User(
21
            'wouter',
22
            'test',
23
            'Wouter Sioen',
24
            '[email protected]',
25
            1
26
        );
27
28
        $this->users[] = $user;
29
30
        $passwordResetUser = new User(
31
            'reset',
32
            'reset',
33
            'reset',
34
            '[email protected]',
35
            2,
36
            PasswordResetToken::generate()
37
        );
38
        
39
        $this->users[] = $passwordResetUser;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function findByUsername($username)
46
    {
47
        foreach ($this->users as $user) {
48
            if ($user->getUsername() === $username) {
49
                return $user;
50
            }
51
        }
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function find($id)
58
    {
59
        foreach ($this->users as $user) {
60
            if ($user->getId() === $id) {
61
                return $user;
62
            }
63
        }
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function supportsClass($class)
70
    {
71
        return $class === User::class;
72
    }
73
74
    /**
75
     * @param string $token
76
     *
77
     * @return UserInterface|null
78
     */
79
    public function findByPasswordResetToken($token)
80
    {
81
        return $this->findByUsername('reset');
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getSupportedClass()
88
    {
89
        return User::class;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function add(UserInterface $user)
96
    {
97
        $this->users[] = $user;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function save(UserInterface $user)
104
    {
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 View Code Duplication
    public function update(UserInterface $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        foreach ($this->users as $key => $row) {
113
            if ($row->getId() === $user->getId()) {
114
                $this->users[$key] = $user;
115
                break;
116
            }
117
        }
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 View Code Duplication
    public function delete(UserInterface $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        foreach ($this->users as $key => $row) {
126
            if ($row->getUserName() === $user->getUserName()) {
127
                unset($this->users[$key]);
128
                break;
129
            }
130
        }
131
    }
132
}
133