Completed
Pull Request — master (#5)
by Thibaud
02:13
created

MetadataHelper   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 69.41%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 35
c 7
b 0
f 0
lcom 1
cbo 3
dl 0
loc 186
ccs 59
cts 85
cp 0.6941
rs 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFieldName() 0 14 4
B getFieldAlias() 0 16 5
C getStoryField() 0 29 7
A appendValueToMap() 0 14 3
B getRecordField() 0 21 5
B getRecordFields() 0 25 6
A getRecordMultiField() 0 18 4
1
<?php
2
3
namespace Alchemy\Phraseanet\Helper;
4
5
use Alchemy\Phraseanet\Mapping\FieldMap;
6
use PhraseanetSDK\Entity\Record;
7
use PhraseanetSDK\Entity\Story;
8
9
class MetadataHelper
10
{
11
12
    /**
13
     * @var string
14
     */
15
    private $defaultLocale;
16
17
    /**
18
     * @var FieldMap
19
     */
20
    private $fieldsMap;
21
22
    /**
23
     * @var string
24
     */
25
    private $fallbackLocale;
26
27
    /**
28
     * @param FieldMap $fieldsMap
29
     * @param string $defaultLocale
30
     * @param string $fallbackLocale
31
     */
32 11
    public function __construct(FieldMap $fieldsMap, $defaultLocale, $fallbackLocale)
33
    {
34 11
        $this->fieldsMap = $fieldsMap;
35 11
        $this->defaultLocale = $defaultLocale;
36 11
        $this->fallbackLocale = $fallbackLocale;
37 11
    }
38
39
    public function getFieldName($alias, $locale = null)
40
    {
41
        if ($locale === null) {
42
            $locale = $this->defaultLocale;
43
        }
44
45
        try {
46
            return $this->fieldsMap->getFieldName($alias, $locale);
47
        } catch (\OutOfBoundsException $exception) {
48
            if ($locale !== $this->defaultLocale) {
49
                return $this->getFieldName($alias, $this->defaultLocale);
50
            }
51
        }
52
    }
53
54
    /**
55
     * @param $fieldName
56
     * @param null $locale
57
     * @param bool $fallback Whether the lookup should fallback to the default locale
58
     * @return string
59
     */
60
    public function getFieldAlias($fieldName, $locale = null, $fallback = true)
61
    {
62
        if ($locale == null) {
63
            $locale = $this->defaultLocale;
64
        }
65
66
        if ($this->fieldsMap->isFieldMapped($fieldName, $locale)) {
67
            return $this->fieldsMap->getAliasFromFieldName($fieldName, $locale);
68
        }
69
70
        if ($locale !== $this->defaultLocale && $fallback) {
71
            return $this->getFieldAlias($fieldName, $this->defaultLocale);
72
        }
73
74
        throw new \RuntimeException("No alias is available for field '$fieldName' with locale '$locale'.");
75
    }
76
77 3
    public function getStoryField(Story $story, $field, $locale = null)
78
    {
79 3
        // @todo Clean up code
80
        if ($locale == null) {
81
            $locale = $this->defaultLocale;
82
        }
83 3
84 1
        if (! $this->fieldsMap->hasAlias($field, $locale)) {
85
            if ($locale !== $this->defaultLocale) {
86
                return $this->getStoryField($story, $field, $this->defaultLocale);
87
            }
88 1
89
            return '';
90
        }
91 2
92
        $key = $this->fieldsMap->getFieldName($field, $locale);
93 2
94 1
        foreach ($story->getCaption() as $captionField) {
95 1
            if ($key === $captionField->getName()) {
96
                return $captionField->getValue();
97 1
            }
98
        }
99 1
100
        if ($locale !== $this->defaultLocale) {
101
            return $this->getStoryField($story, $field, $this->defaultLocale);
102
        }
103 1
104
        return '';
105
    }
106 3
107
    public function getRecordFields(Record $record, array $fields = null, $locale = null)
108 3
    {
109
        // @todo Clean up code
110
        if ($locale == null) {
111
            $locale = $this->defaultLocale;
112 3
        }
113
114 3
        $map = [];
115 3
116 3
        foreach ($record->getMetadata() as $metadata) {
117
            if (!$this->fieldsMap->isFieldMapped($metadata->getName(), $locale)) {
118
                continue;
119 3
            }
120
121 3
            $alias = $this->fieldsMap->getAliasFromFieldName($metadata->getName(), $locale);
122 2
123
            if ($fields !== null && !in_array($alias, $fields)) {
124
                continue;
125 3
            }
126 3
127
            $map = $this->appendValueToMap($map, $alias, $metadata->getValue());
128 3
        }
129
130
        return $map;
131
    }
132
133
    /**
134
     * @param array $map
135
     * @param string $alias
136
     * @param string $value
137 3
     * @return array
138
     */
139 3
    private function appendValueToMap($map, $alias, $value)
140 1
    {
141 1
        if (isset($map[$alias])) {
142 1
            if (!is_array($map[$alias])) {
143
                $map[$alias] = [$map[$alias]];
144 1
            }
145 1
146 3
            $map[$alias][] = $value;
147
        } else {
148
            $map[$alias] = $value;
149 3
        }
150
151
        return $map;
152 2
    }
153
154 2
    public function getRecordField(Record $record, $field, $locale = null)
155
    {
156
        if ($locale == null) {
157
            $locale = $this->defaultLocale;
158 2
        }
159 1
160
        if (!$this->fieldsMap->hasAlias($field, $locale)) {
161
            return null;
162 2
        }
163
164 2
        $key = $this->fieldsMap->getFieldName($field, $locale);
165
166 2
        foreach ($record->getMetadata() as $metadata) {
167 1
            // Try to find the corresponding RecordCaption
168
            if ($key === $metadata->getName()) {
169 2
                return $metadata->getValue();
170
            }
171 1
        }
172
173
        return null;
174 2
    }
175
176 2
    public function getRecordMultiField(Record $record, $field, $locale = null)
177 1
    {
178
        if (!$this->fieldsMap->hasAlias($field, $locale)) {
179
            return [];
180 1
        }
181 1
182
        $key = $this->fieldsMap->getFieldName($field, $locale);
183 1
        $values = array();
184
185 1
        foreach ($record->getMetadata() as $metadata) {
186 1
            // Try to find the corresponding RecordCaption
187 1
            if ($key === $metadata->getName()) {
188 1
                $values[] = $metadata->getValue();
189
            }
190 1
        }
191
192
        return $values;
193
    }
194
}
195