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

PolicyEntityTrait::createPolicyEntity()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 1
nop 1
crap 3
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\AccessManagement\PolicyEntity;
20
21
/**
22
 * Class PolicyEntityTrait.
23
 */
24
trait PolicyEntityTrait
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 Policy Entity instance form the data.
37
     *
38
     * @param array $data
39
     * @return PolicyEntity
40
     */
41 3
    protected function createPolicyEntity(array $data) : PolicyEntity
42
    {
43
        /** @var PolicyEntity $entity */
44 3
        $entity = $this->getNewEntityInstance(PolicyEntity::class);
45
46 3
        $entity->setPolicyId((int) $data['id_am_policy'])
47 3
            ->setResourceId(!empty($data['fk_am_resource']) ? (int) $data['fk_am_resource'] : null)
48 3
            ->setApplicationId(!empty($data['fk_application']) ? (int) $data['fk_application'] : null)
49 3
            ->setName($data['name'])
50 3
            ->setTitle($data['title'])
51 3
            ->setDescription($data['description'])
52 3
            ->setMethod($data['method'])
53 3
            ->setReadOnly((bool) $data['is_read_only'])
54 3
            ->setDateCreated(new DateTime($data['date_created'] ?? 'now'))
55 3
            ->setDateModified(new DateTime($data['date_created'] ?? 'now'));
56
57 3
        return $entity;
58
    }
59
}
60