MixinStrategy::resolveMetadataForNode()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 4
nop 1
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