Completed
Pull Request — master (#7)
by Thibaud
11:59 queued 01:30
created

MetadataHelper::getStoryFields()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.105

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 12
cts 14
cp 0.8571
rs 8.5125
cc 6
eloc 12
nc 8
nop 3
crap 6.105
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
        // @todo Clean up code
80 3
        if ($locale == null) {
81
            $locale = $this->defaultLocale;
82
        }
83
84 3
        if (! $this->fieldsMap->hasAlias($field, $locale)) {
85 1
            if ($locale !== $this->defaultLocale) {
86
                return $this->getStoryField($story, $field, $this->defaultLocale);
87
            }
88
89 1
            return '';
90
        }
91
92 2
        $key = $this->fieldsMap->getFieldName($field, $locale);
93
94 2
        foreach ($story->getCaption() as $captionField) {
95 1
            if ($key === $captionField->getName()) {
96 1
                return $captionField->getValue();
97
            }
98 1
        }
99
100 1
        if ($locale !== $this->defaultLocale) {
101
            return $this->getStoryField($story, $field, $this->defaultLocale);
102
        }
103
104 1
        return '';
105
    }
106
107 3
    public function getStoryFields(Story $story, array $fields = null, $locale = null)
108
    {
109
        if ($locale == null) {
110 3
            $locale = $this->defaultLocale;
111
        }
112
113
        $map = [];
114 3
115
        foreach ($story->getMetadata() as $metadata) {
116 3
            if (!$this->fieldsMap->isFieldMapped($metadata->getName(), $locale)) {
117 3
                continue;
118 3
            }
119
120
            $alias = $this->fieldsMap->getAliasFromFieldName($metadata->getName(), $locale);
121 3
122
            if ($fields !== null && !in_array($alias, $fields)) {
123 3
                continue;
124 2
            }
125
126
            $map = $this->appendValueToMap($map, $alias, $metadata->getValue());
127 3
        }
128 3
129
        return $map;
130 3
    }
131
132
    public function getRecordFields(Record $record, array $fields = null, $locale = null)
133
    {
134
        // @todo Clean up code
135
        if ($locale == null) {
136
            $locale = $this->defaultLocale;
137
        }
138
139 3
        $map = [];
140
141 3
        foreach ($record->getMetadata() as $metadata) {
142 1
            if (!$this->fieldsMap->isFieldMapped($metadata->getName(), $locale)) {
143 1
                continue;
144 1
            }
145
146 1
            $alias = $this->fieldsMap->getAliasFromFieldName($metadata->getName(), $locale);
147 1
148 3
            if ($fields !== null && !in_array($alias, $fields)) {
149
                continue;
150
            }
151 3
152
            $map = $this->appendValueToMap($map, $alias, $metadata->getValue());
153
        }
154 2
155
        return $map;
156 2
    }
157
158
    /**
159
     * @param array $map
160 2
     * @param string $alias
161 1
     * @param string $value
162
     * @return array
163
     */
164 2
    private function appendValueToMap($map, $alias, $value)
165
    {
166 2
        if (isset($map[$alias])) {
167
            if (!is_array($map[$alias])) {
168 2
                $map[$alias] = [$map[$alias]];
169 1
            }
170
171 2
            $map[$alias][] = $value;
172
        } else {
173 1
            $map[$alias] = $value;
174
        }
175
176 2
        return $map;
177
    }
178 2
179 1
    public function getRecordField(Record $record, $field, $locale = null)
180
    {
181
        if ($locale == null) {
182 1
            $locale = $this->defaultLocale;
183 1
        }
184
185 1
        if (!$this->fieldsMap->hasAlias($field, $locale)) {
186
            return null;
187 1
        }
188 1
189 1
        $key = $this->fieldsMap->getFieldName($field, $locale);
190 1
191
        foreach ($record->getMetadata() as $metadata) {
192 1
            // Try to find the corresponding RecordCaption
193
            if ($key === $metadata->getName()) {
194
                return $metadata->getValue();
195
            }
196
        }
197
198
        return null;
199
    }
200
201
    public function getRecordMultiField(Record $record, $field, $locale = null)
202
    {
203
        if (!$this->fieldsMap->hasAlias($field, $locale)) {
204
            return [];
205
        }
206
207
        $key = $this->fieldsMap->getFieldName($field, $locale);
208
        $values = array();
209
210
        foreach ($record->getMetadata() as $metadata) {
211
            // Try to find the corresponding RecordCaption
212
            if ($key === $metadata->getName()) {
213
                $values[] = $metadata->getValue();
214
            }
215
        }
216
217
        return $values;
218
    }
219
}
220