Passed
Branch master (0828fa)
by Gabor
03:19
created

UserEntityTrait   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 2
dl 0
loc 35
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
getNewEntityInstance() 0 1 ?
A createUserEntity() 0 17 1
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\Traits;
15
16
use WebHemi\DateTime;
17
use RuntimeException;
18
use WebHemi\Data\EntityInterface;
19
use WebHemi\Data\Entity\User\UserEntity;
20
21
/**
22
 * Class UserEntityTrait.
23
 */
24
trait UserEntityTrait
25
{
26
    /**
27
     * Returns a new instance of the required entity.
28
     *
29
     * @param string $entityClassName
30
     * @throws RuntimeException
31
     * @return EntityInterface
32
     */
33
    abstract protected function getNewEntityInstance(string $entityClassName) : EntityInterface;
34
35
    /**
36
     * Creates a new User Entity instance form the data.
37
     *
38
     * @param array $data
39
     * @return UserEntity
40
     */
41 2
    protected function createUserEntity(array $data) : UserEntity
42
    {
43
        /** @var UserEntity $entity */
44 2
        $entity = $this->getNewEntityInstance(UserEntity::class);
45
46 2
        $entity->setUserId((int) $data['id_user'])
47 2
            ->setUserName($data['username'])
48 2
            ->setEmail($data['email'])
49 2
            ->setPassword($data['password'])
50 2
            ->setHash($data['hash'])
51 2
            ->setActive((bool) $data['is_active'])
52 2
            ->setEnabled((bool) $data['is_enabled'])
53 2
            ->setDateCreated(new DateTime($data['date_created'] ?? 'now'))
54 2
            ->setDateModified(new DateTime($data['date_modified'] ?? 'now'));
55
56 2
        return $entity;
57
    }
58
}
59