BaseMetadataFactory   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 185
Duplicated Lines 22.16 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 2 Features 2
Metric Value
c 7
b 2
f 2
dl 41
loc 185
wmc 17
lcom 1
cbo 5
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getMetadataForAlias() 13 13 2
A getMetadataForPhpcrType() 13 13 2
A hasMetadataForPhpcrType() 0 4 1
A hasMetadataForClass() 0 6 1
A getMetadataForClass() 15 15 2
A hasAlias() 0 4 1
A getAliases() 0 4 1
A getMetadataForPhpcrNode() 0 6 1
A getPhpcrTypeMap() 0 4 1
B loadMetadata() 0 33 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Component\DocumentManager\Metadata;
13
14
use PHPCR\NodeInterface;
15
use Sulu\Component\DocumentManager\ClassNameInflector;
16
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent;
17
use Sulu\Component\DocumentManager\Events;
18
use Sulu\Component\DocumentManager\Exception\MetadataNotFoundException;
19
use Sulu\Component\DocumentManager\Metadata;
20
use Sulu\Component\DocumentManager\MetadataFactoryInterface;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
23
/**
24
 * Simple metadata factory which uses an array map.
25
 *
26
 * Note that this class does not  implement the getMetadataForPhpcrNode method
27
 * as that would require a circular dependency.
28
 */
29
class BaseMetadataFactory implements MetadataFactoryInterface
30
{
31
    /**
32
     * @var array
33
     */
34
    private $aliasMap = [];
35
36
    /**
37
     * @var array
38
     */
39
    private $classMap = [];
40
41
    /**
42
     * @var array
43
     */
44
    private $phpcrTypeMap = [];
45
46
    /**
47
     * @var EventDispatcherInterface
48
     */
49
    private $dispatcher;
50
51
    /**
52
     * @var Metadata[]
53
     */
54
    private $metadata = [];
55
56
    /**
57
     * @param array $mapping
58
     */
59
    public function __construct(EventDispatcherInterface $dispatcher, array $mapping)
60
    {
61
        $this->dispatcher = $dispatcher;
62
63
        foreach ($mapping as $map) {
64
            $this->aliasMap[$map['alias']] = $map;
65
            $this->classMap[$map['class']] = $map;
66
            $this->phpcrTypeMap[$map['phpcr_type']] = $map;
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 View Code Duplication
    public function getMetadataForAlias($alias)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        if (!isset($this->aliasMap[$alias])) {
76
            throw new MetadataNotFoundException(sprintf(
77
                'Metadata with alias "%s" not found, known aliases: "%s"',
78
                $alias, implode('", "', array_keys($this->aliasMap))
79
            ));
80
        }
81
82
        $map = $this->aliasMap[$alias];
83
84
        return $this->loadMetadata($map);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 View Code Duplication
    public function getMetadataForPhpcrType($phpcrType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        if (!isset($this->phpcrTypeMap[$phpcrType])) {
93
            throw new MetadataNotFoundException(sprintf(
94
                'Metadata with phpcrType "%s" not found, known phpcrTypes: "%s"',
95
                $phpcrType, implode('", "', array_keys($this->phpcrTypeMap))
96
            ));
97
        }
98
99
        $map = $this->phpcrTypeMap[$phpcrType];
100
101
        return $this->loadMetadata($map);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function hasMetadataForPhpcrType($phpcrType)
108
    {
109
        return isset($this->phpcrTypeMap[$phpcrType]);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function hasMetadataForClass($class)
116
    {
117
        $class = ClassNameInflector::getUserClassName($class);
118
119
        return isset($this->classMap[$class]);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 View Code Duplication
    public function getMetadataForClass($class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $class = ClassNameInflector::getUserClassName($class);
128
129
        if (!isset($this->classMap[$class])) {
130
            throw new MetadataNotFoundException(sprintf(
131
                'Metadata with class "%s" not found, known classes: "%s"',
132
                $class, implode('", "', array_keys($this->classMap))
133
            ));
134
        }
135
136
        $map = $this->classMap[$class];
137
138
        return $this->loadMetadata($map);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function hasAlias($alias)
145
    {
146
        return isset($this->aliasMap[$alias]);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getAliases()
153
    {
154
        return array_keys($this->aliasMap);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function getMetadataForPhpcrNode(NodeInterface $node)
161
    {
162
        throw new \BadMethodCallException(
163
            'The BaseMetadataFactory does not implement this method'
164
        );
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function getPhpcrTypeMap()
171
    {
172
        return $this->phpcrTypeMap;
173
    }
174
175
    /**
176
     * @param array $mapping
177
     *
178
     * @return Metadata
179
     */
180
    private function loadMetadata($mapping)
181
    {
182
        $mapping = array_merge([
183
            'alias' => null,
184
            'phpcr_type' => null,
185
            'class' => null,
186
            'mapping' => [],
187
        ], $mapping);
188
189
        if (isset($this->metadata[$mapping['alias']])) {
190
            return $this->metadata[$mapping['alias']];
191
        }
192
193
        $metadata = new Metadata();
194
        $metadata->setAlias($mapping['alias']);
195
        $metadata->setPhpcrType($mapping['phpcr_type']);
196
        $metadata->setClass($mapping['class']);
197
198
        foreach ($mapping['mapping'] as $fieldName => $fieldMapping) {
199
            $fieldMapping = array_merge([
200
                'encoding' => 'content',
201
                'property' => $fieldName,
202
            ], $fieldMapping);
203
            $metadata->addFieldMapping($fieldName, $fieldMapping);
204
        }
205
206
        $event = new MetadataLoadEvent($metadata);
207
        $this->dispatcher->dispatch(Events::METADATA_LOAD, $event);
208
209
        $this->metadata[$mapping['alias']] = $metadata;
210
211
        return $metadata;
212
    }
213
}
214