Passed
Push — master ( 64ab61...b96172 )
by Gabor
09:36
created

UserToGroupCoupler::getNewEntityInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 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
namespace WebHemi\Data\Coupler;
13
14
use RuntimeException;
15
use WebHemi\Adapter\Data\DataAdapterInterface;
16
use WebHemi\Data\Coupler\Traits\UserEntityTrait;
17
use WebHemi\Data\Coupler\Traits\UserGroupEntityTrait;
18
use WebHemi\Data\Entity\DataEntityInterface;
19
use WebHemi\Data\Entity\User\UserEntity;
20
use WebHemi\Data\Entity\User\UserGroupEntity;
21
22
/**
23
 * Class UserToPolicyCoupler.
24
 */
25
class UserToGroupCoupler extends AbstractDataCoupler
26
{
27
    /** @var string */
28
    protected $connectorIdKey = 'id_user_to_user_group';
29
    /** @var string */
30
    protected $connectorDataGroup = 'webhemi_user_to_user_group';
31
    /** @var array */
32
    protected $dependentDataGroups = [
33
        UserEntity::class => [
34
            'source_key' => 'fk_user',
35
            'connector_key' => 'fk_user_group',
36
            'depending_group' => 'webhemi_user_group',
37
            'depending_id_key' => 'id_user_group',
38
        ],
39
        UserGroupEntity::class => [
40
            'source_key' => 'fk_user_group',
41
            'connector_key' => 'fk_user',
42
            'depending_group' => 'webhemi_user',
43
            'depending_id_key' => 'id_user',
44
        ]
45
    ];
46
47
    use UserEntityTrait;
48
    use UserGroupEntityTrait;
49
50
    /**
51
     * Returns a new instance of the required entity.
52
     *
53
     * @param string $entityClassName
54
     * @throws RuntimeException
55
     * @return DataEntityInterface
56
     */
57 View Code Duplication
    protected function getNewEntityInstance($entityClassName)
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...
58
    {
59
        if (!isset($this->dataEntityPrototypes[$entityClassName])) {
60
            throw new RuntimeException(sprintf('Class %s is not defined in this Coupler.', $entityClassName));
61
        }
62
63
        return clone $this->dataEntityPrototypes[$entityClassName];
64
    }
65
66
    /**
67
     * Gets all the entities those are depending from the given entity.
68
     *
69
     * @param DataEntityInterface $entity
70
     * @throws RuntimeException
71
     * @return array<DataEntityInterface>
72
     */
73
    public function getEntityDependencies(DataEntityInterface $entity)
74
    {
75
        $entityClass = get_class($entity);
76
        if (!isset($this->dataEntityPrototypes[$entityClass])) {
77
            throw new RuntimeException(
78
                sprintf('Cannot use this coupler class to find dependencies for %s.', $entityClass)
79
            );
80
        }
81
82
        $entityList = [];
83
        $dataList = $this->getEntityDataSet($entity);
84
85
        foreach ($dataList as $entityData) {
86
            $entityList[] = $entity instanceof UserEntity
87
                ? $this->createUserGroupEntity($entityData)
88
                : $this->createUserEntity($entityData);
89
        }
90
91
        return $entityList;
92
    }
93
}
94