Completed
Push — master ( 6a0ab6...96f47f )
by Mārtiņš
02:11
created

EmailIdentity::createIdentity()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 17
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * SQL logic for authentication attemps using username/password
7
 */
8
9
use Palladium\Component\SqlMapper;
10
use Palladium\Entity as Entity;
11
use Palladium\Contract\CanPersistIdentity;
12
13
class EmailIdentity extends SqlMapper implements CanPersistIdentity
14
{
15
16
    /**
17
     * @param Entity\Identity $entity
18
     */
19
    public function exists(Entity\Identity $entity)
20
    {
21
        $table = $this->config['accounts']['identities'];
22
23
        $sql = "SELECT 1
24
                  FROM {$table}
25
                 WHERE type = :type
26
                   AND fingerprint = :fingerprint
27
                   AND identifier = :identifier
28
                   AND (expires_on IS NULL OR expires_on > :now)";
29
30
        $statement = $this->connection->prepare($sql);
31
32
        $statement->bindValue(':type', Entity\Identity::TYPE_PASSWORD);
33
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Palladium\Entity\Identity as the method getFingerprint() does only exist in the following sub-classes of Palladium\Entity\Identity: Palladium\Entity\CookieIdentity, Palladium\Entity\EmailIdentity. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
34
        $statement->bindValue(':identifier', $entity->getIdentifier());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Palladium\Entity\Identity as the method getIdentifier() does only exist in the following sub-classes of Palladium\Entity\Identity: Palladium\Entity\EmailIdentity. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
35
        $statement->bindValue(':now', time());
36
37
        $statement->execute();
38
        $data = $statement->fetch();
39
40
        return empty($data) === false;
41
    }
42
43
44
    /**
45
     * @param Entity\Identity $entity
46
     */
47
    public function fetch(Entity\Identity $entity)
48
    {
49
        $table = $this->config['accounts']['identities'];
50
51
        $sql = "SELECT identity_id      AS id,
52
                       user_id          AS userId,
53
                       hash             AS hash,
54
                       status           AS status,
55
                       token            AS token,
56
                       token_action     AS tokenAction,
57
                       token_expires_on AS tokenEndOfLife
58
                  FROM $table
59
                 WHERE type = :type
60
                   AND fingerprint = :fingerprint
61
                   AND identifier = :identifier";
62
63
        $statement = $this->connection->prepare($sql);
64
65
        $statement->bindValue(':type', $entity->getType());
66
        $statement->bindValue(':identifier', $entity->getIdentifier());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Palladium\Entity\Identity as the method getIdentifier() does only exist in the following sub-classes of Palladium\Entity\Identity: Palladium\Entity\EmailIdentity. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
67
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Palladium\Entity\Identity as the method getFingerprint() does only exist in the following sub-classes of Palladium\Entity\Identity: Palladium\Entity\CookieIdentity, Palladium\Entity\EmailIdentity. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
68
69
        $statement->execute();
70
71
        $data = $statement->fetch();
72
73
        if ($data) {
74
            $this->applyValues($entity, $data);
75
        }
76
    }
77
78
79
    /**
80
     * @param Entity\Identity $entity
81
     */
82
    public function store(Entity\Identity $entity)
83
    {
84
        if ($entity->getId() === null) {
85
            $this->createIdentity($entity);
86
            return;
87
        }
88
89
        $this->updateIdentity($entity);
90
    }
91
92
93
    private function createIdentity(Entity\Identity $entity)
94
    {
95
        $table = $this->config['accounts']['identities'];
96
97
        $sql = "INSERT INTO {$table}
98
                       (type, status, identifier, fingerprint, hash, created_on, token, token_action, token_expires_on )
99
                VALUES (:type, :status, :identifier, :fingerprint, :hash, :created, :token, :action, :token_eol)";
100
101
        $statement = $this->connection->prepare($sql);
102
103
        $statement->bindValue(':type', Entity\Identity::TYPE_PASSWORD);
104
        $statement->bindValue(':status', Entity\Identity::STATUS_NEW);
105
        $statement->bindValue(':identifier', $entity->getIdentifier());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Palladium\Entity\Identity as the method getIdentifier() does only exist in the following sub-classes of Palladium\Entity\Identity: Palladium\Entity\EmailIdentity. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
106
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Palladium\Entity\Identity as the method getFingerprint() does only exist in the following sub-classes of Palladium\Entity\Identity: Palladium\Entity\CookieIdentity, Palladium\Entity\EmailIdentity. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
107
        $statement->bindValue(':hash', $entity->getHash());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Palladium\Entity\Identity as the method getHash() does only exist in the following sub-classes of Palladium\Entity\Identity: Palladium\Entity\CookieIdentity, Palladium\Entity\EmailIdentity. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
108
        $statement->bindValue(':token', $entity->getToken());
109
        $statement->bindValue(':action', $entity->getTokenAction());
110
        $statement->bindValue(':token_eol', $entity->getTokenEndOfLife());
111
        $statement->bindValue(':created', time());
112
113
114
        $statement->execute();
115
116
        $entity->setId($this->connection->lastInsertId());
117
    }
118
119
120 View Code Duplication
    private function updateIdentity(Entity\Identity $entity)
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...
121
    {
122
        $table = $this->config['accounts']['identities'];
123
124
        $sql = "UPDATE {$table}
125
                   SET hash = :hash,
126
                       status = :status,
127
                       expires_on = :expires,
128
                       token = :token,
129
                       token_action = :action,
130
                       token_expires_on = :token_eol
131
                 WHERE identity_id = :id";
132
133
         $statement = $this->connection->prepare($sql);
134
135
         $statement->bindValue(':id', $entity->getId());
136
         $statement->bindValue(':hash', $entity->getHash());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Palladium\Entity\Identity as the method getHash() does only exist in the following sub-classes of Palladium\Entity\Identity: Palladium\Entity\CookieIdentity, Palladium\Entity\EmailIdentity. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
137
         $statement->bindValue(':status', $entity->getStatus());
138
         $statement->bindValue(':expires', $entity->getExpiresOn());
139
         $statement->bindValue(':token', $entity->getToken());
140
         $statement->bindValue(':action', $entity->getTokenAction());
141
         $statement->bindValue(':token_eol', $entity->getTokenEndOfLife());
142
143
         $statement->execute();
144
    }
145
}
146