MixinStrategy   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 2 Features 1
Metric Value
c 7
b 2
f 1
dl 0
loc 49
wmc 6
lcom 1
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createNodeForDocument() 0 10 1
A resolveMetadataForNode() 0 16 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\Component\DocumentManager\Strategy;
13
14
use PHPCR\NodeInterface;
15
use PHPCR\Util\UUIDHelper;
16
use Sulu\Component\DocumentManager\DocumentStrategyInterface;
17
use Sulu\Component\DocumentManager\MetadataFactoryInterface;
18
19
/**
20
 * Manage nodes via. a jcr mixin.
21
 */
22
class MixinStrategy implements DocumentStrategyInterface
23
{
24
    /**
25
     * @var MetadataFactoryInterface
26
     */
27
    private $metadataFactory;
28
29
    /**
30
     * @param MetadataFactoryInterface $metadataFactory
31
     */
32
    public function __construct(MetadataFactoryInterface $metadataFactory)
33
    {
34
        $this->metadataFactory = $metadataFactory;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function createNodeForDocument($document, NodeInterface $parentNode, $name)
41
    {
42
        $metadata = $this->metadataFactory->getMetadataForClass(get_class($document));
43
44
        $node = $parentNode->addNode($name);
45
        $node->addMixin($metadata->getPhpcrType());
46
        $node->setProperty('jcr:uuid', UUIDHelper::generateUUID());
47
48
        return $node;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function resolveMetadataForNode(NodeInterface $node)
55
    {
56
        if (false === $node->hasProperty('jcr:mixinTypes')) {
57
            return;
58
        }
59
60
        $mixinTypes = (array) $node->getPropertyValue('jcr:mixinTypes');
61
62
        foreach ($mixinTypes as $mixinType) {
63
            if (true == $this->metadataFactory->hasMetadataForPhpcrType($mixinType)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
64
                return $this->metadataFactory->getMetadataForPhpcrType($mixinType);
65
            }
66
        }
67
68
        return;
69
    }
70
}
71