Passed
Push — master ( 37b412...8220b9 )
by Gabor
03:10
created

UserMetaStorage::getUserMetaSetForUserId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Data\Storage\User;
15
16
use WebHemi\DateTime;
17
use WebHemi\Data\EntityInterface;
18
use WebHemi\Data\Entity\User\UserMetaEntity;
19
use WebHemi\Data\Storage\AbstractStorage;
20
21
/**
22
 * Class UserMetaStorage.
23
 */
24
class UserMetaStorage extends AbstractStorage
25
{
26
    /** @var string */
27
    protected $dataGroup = 'webhemi_user_meta';
28
    /** @var string */
29
    protected $idKey = 'id_user_meta';
30
    /** @var string */
31
    private $userId = 'fk_user';
32
    /** @var string */
33
    private $metaKey = 'meta_key';
34
    /** @var string */
35
    private $metaData = 'meta_data';
36
    /** @var string */
37
    private $dateCreated = 'date_created';
38
    /** @var string */
39
    private $dateModified = 'date_modified';
40
41
    /**
42
     * Populates an entity with storage data.
43
     *
44
     * @param EntityInterface $dataEntity
45
     * @param array           $data
46
     * @return void
47
     */
48 2
    protected function populateEntity(EntityInterface&$dataEntity, array $data) : void
49
    {
50
        /* @var UserMetaEntity $dataEntity */
51 2
        $dataEntity->setUserMetaId((int) $data[$this->idKey])
52 2
            ->setUserId((int) $data[$this->userId])
53 2
            ->setMetaKey($data[$this->metaKey])
54 2
            ->setMetaData($data[$this->metaData])
55 2
            ->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now'))
56 2
            ->setDateModified(new DateTime($data[$this->dateModified] ?? 'now'));
57 2
    }
58
59
    /**
60
     * Get data from an entity.
61
     *
62
     * @param EntityInterface $dataEntity
63
     * @return array
64
     */
65
    protected function getEntityData(EntityInterface $dataEntity) : array
66
    {
67
        /** @var UserMetaEntity $dataEntity */
68
        $dateCreated = $dataEntity->getDateCreated();
69
        $dateModified = $dataEntity->getDateModified();
70
71
        return [
72
            $this->idKey => $dataEntity->getKeyData(),
73
            $this->userId => $dataEntity->getUserId(),
74
            $this->metaKey => $dataEntity->getMetaKey(),
75
            $this->metaData => $dataEntity->getMetaData(),
76
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
77
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
78
        ];
79
    }
80
81
    /**
82
     * Returns a User Meta entity identified by (unique) ID.
83
     *
84
     * @param int $identifier
85
     * @return null|UserMetaEntity
86
     */
87 1
    public function getUserMetaById($identifier) : ? UserMetaEntity
88
    {
89
        /** @var null|UserMetaEntity $dataEntity */
90 1
        $dataEntity = $this->getDataEntity([$this->idKey => $identifier]);
91
92 1
        return $dataEntity;
93
    }
94
95
    /**
96
     * Returns a User Meta data list identified by user ID.
97
     *
98
     * @param mixed $userId
99
     * @return array
100
     */
101 1
    public function getUserMetaSetForUserId($userId) : array
102
    {
103 1
        $userMetaEntitySet = $this->getDataEntitySet([$this->userId => $userId]);
104 1
        $userMetaSet = [];
105
106
        /** @var UserMetaEntity $metaEntity */
107 1
        foreach ($userMetaEntitySet as $metaEntity) {
108 1
            $userMetaSet[$metaEntity->getMetaKey()] = $metaEntity->getMetaData();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface WebHemi\Data\EntityInterface as the method getMetaKey() does only exist in the following implementations of said interface: WebHemi\Data\Entity\User\UserMetaEntity.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface WebHemi\Data\EntityInterface as the method getMetaData() does only exist in the following implementations of said interface: WebHemi\Data\Entity\User\UserMetaEntity.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
109
        }
110
111 1
        return $userMetaSet;
112
    }
113
114
    /**
115
     * Returns a User Meta entity list identified by user ID.
116
     *
117
     * @param mixed $userId
118
     * @return UserMetaEntity[]
119
     */
120
    public function getUserMetaForUserId($userId) : array
121
    {
122
        return $this->getDataEntitySet([$this->userId => $userId]);
123
    }
124
}
125