Completed
Pull Request — develop (#194)
by Wachter
14:07
created

StructureTagTrait::getTagAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 4
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\ArticleBundle\Metadata;
13
14
use Sulu\Bundle\ArticleBundle\Admin\ArticleAdmin;
15
use Sulu\Component\Content\Metadata\StructureMetadata;
16
17
/**
18
 * Encapsulates function to extract the type from structure-metadata.
19
 */
20
trait StructureTagTrait
21
{
22
    /**
23
     * Returns type for given structure-metadata.
24
     *
25
     * @param StructureMetadata $metadata
26
     *
27
     * @return string
28
     */
29
    protected function getType(StructureMetadata $metadata)
30
    {
31
        return $this->getTagAttribute($metadata, ArticleAdmin::STRUCTURE_TAG_TYPE, 'type', 'default');
32
    }
33
34
    /**
35
     * Returns multipage-status for given structure-metadata.
36
     *
37
     * @param StructureMetadata $metadata
38
     *
39
     * @return string
40
     */
41
    protected function getMultipage(StructureMetadata $metadata)
42
    {
43
        return $this->getTagAttribute($metadata, ArticleAdmin::STRUCTURE_TAG_MULTIPAGE, 'enabled', false);
44
    }
45
46
    /**
47
     * Returns attribute for given tag in metadata.
48
     *
49
     * @param StructureMetadata $metadata
50
     * @param string $tag
51
     * @param string $attribute
52
     * @param mixed $default
53
     *
54
     * @return mixed
55
     */
56
    private function getTagAttribute(StructureMetadata $metadata, $tag, $attribute, $default)
57
    {
58
        if (!$metadata->hasTag($tag)) {
59
            return $default;
60
        }
61
62
        $tag = $metadata->getTag($tag);
63
        if (!array_key_exists($attribute, $tag['attributes'])) {
64
            return $default;
65
        }
66
67
        return $tag['attributes'][$attribute];
68
    }
69
}
70