Passed
Push — master ( 02dfef...9d3b05 )
by Loban
09:09
created

ActivityLogDecorator::getEntityAttributeLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * @link https://github.com/lav45/yii2-activity-logger
4
 * @copyright Copyright (c) 2017 LAV45
5
 * @author Aleksey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\activityLogger\module\models;
10
11
use Yii;
12
13
class ActivityLogDecorator
14
{
15
    /**
16
     * @var DataModel|string|array
17
     */
18
    public $dataModel = DataModel::class;
19
    /**
20
     * [ entity_name => Entity::class ]
21
     */
22
    public array $entityMap = [];
23
24
    private array $entityModel = [];
25
26
    private ActivityLog $owner;
27
28
    public function __construct(ActivityLog $owner)
29
    {
30
        $this->owner = $owner;
31
    }
32
33
    /**
34
     * @return \yii\base\Model|null
35
     */
36
    protected function getEntityModel()
37
    {
38
        $entityName = $this->owner->entity_name;
39
        if (isset($this->entityModel[$entityName]) === false) {
40
            $this->entityModel[$entityName] = $this->getEntityObject($entityName);
41
        }
42
        return $this->entityModel[$entityName] ?: null;
43
    }
44
45
    /**
46
     * @return false|\yii\base\Model
47
     */
48
    private function getEntityObject(string $id)
49
    {
50
        if (isset($this->entityMap[$id]) === false) {
51
            return false;
52
        }
53
        /** @var \yii\base\Model $class */
54
        $class = $this->entityMap[$id];
55
        return $class::instance();
56
    }
57
58
    /**
59
     * @return DataModel[]
60
     */
61
    public function getData(): iterable
62
    {
63
        foreach ($this->owner->getData() as $attribute => $values) {
64
            if (is_string($values)) {
65
                $label = is_string($attribute) ? $this->getEntityAttributeLabel($attribute) : $attribute;
66
                yield $label => $values;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $label => $values returns the type Generator which is incompatible with the documented return type lav45\activityLogger\module\models\DataModel[].
Loading history...
67
            } else {
68
                $dataModel = $this->getDataModel()
69
                    ->setFormat($this->getAttributeFormat($attribute))
70
                    ->setData($values);
71
72
                yield $this->getEntityAttributeLabel($attribute) => $dataModel;
73
            }
74
        }
75
    }
76
77
    protected function getDataModel(): DataModel
78
    {
79
        if (!is_object($this->dataModel)) {
80
            $this->dataModel = Yii::createObject($this->dataModel);
81
        }
82
        return $this->dataModel;
83
    }
84
85
    protected function getEntityAttributeLabel(string $attribute): string
86
    {
87
        if ($entityModel = $this->getEntityModel()) {
88
            return $entityModel->getAttributeLabel($attribute);
89
        }
90
        return $this->owner->generateAttributeLabel($attribute);
91
    }
92
93
    protected function getEntityAttributeFormats(): array
94
    {
95
        $entityModel = $this->getEntityModel();
96
        if (null !== $entityModel && method_exists($entityModel, 'attributeFormats')) {
97
            return $entityModel->attributeFormats();
98
        }
99
        return [];
100
    }
101
102
    /**
103
     * @return string|null|\Closure
104
     */
105
    protected function getAttributeFormat(string $attribute)
106
    {
107
        $formats = $this->getEntityAttributeFormats();
108
        return $formats[$attribute] ?? null;
109
    }
110
}
111