Completed
Push — master ( 1b341d...d4f9db )
by Thibaud
10:00 queued 07:22
created

MetadataHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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