Completed
Push — master ( 16ac11...a34a29 )
by Jacob
7s
created

StorageMetadataFactory::handleValidate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace As3\Modlr\Persister\MongoDb;
4
5
use As3\Modlr\Exception\MetadataException;
6
use As3\Modlr\Metadata\EntityMetadata;
7
use As3\Modlr\Metadata\Interfaces\StorageMetadataFactoryInterface;
8
use As3\Modlr\Util\EntityUtility;
9
10
/**
11
 * Creates MongoDb storage Metadata instances for use with metadata drivers.
12
 * Is also responsible for validating storage objects.
13
 *
14
 * @author Jacob Bare <[email protected]>
15
 */
16
final class StorageMetadataFactory implements StorageMetadataFactoryInterface
17
{
18
    /**
19
     * @var EntityUtility
20
     */
21
    private $entityUtil;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param   EntityUtility   $entityUtil
27
     */
28
    public function __construct(EntityUtility $entityUtil)
29
    {
30
        $this->entityUtil = $entityUtil;
31
    }
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function createInstance(array $mapping)
37
    {
38
        $persistence = new StorageMetadata();
39
40
        if (isset($mapping['db'])) {
41
            $persistence->db = $mapping['db'];
42
        }
43
44
        if (isset($mapping['collection'])) {
45
            $persistence->collection = $mapping['collection'];
46
        }
47
        return $persistence;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function handleLoad(EntityMetadata $metadata)
54
    {
55
        if (null === $metadata->persistence->collection) {
0 ignored issues
show
Bug introduced by
Accessing collection on the interface As3\Modlr\Metadata\Inter...s\StorageLayerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
56
            $metadata->persistence->collection = $metadata->type;
0 ignored issues
show
Bug introduced by
Accessing collection on the interface As3\Modlr\Metadata\Inter...s\StorageLayerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
57
        }
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function handleValidate(EntityMetadata $metadata)
64
    {
65
        $this->validateIdStrategy($metadata);
66
        $this->validateDatabase($metadata);
67
        $this->validateCollectionNaming($metadata);
68
    }
69
70
71
72
    /**
73
     * Validates that the collection naming is correct, based on entity format config.
74
     *
75
     * @param   EntityMetadata  $metadata
76
     * @throws  MetadataException
77
     */
78
    private function validateCollectionNaming(EntityMetadata $metadata)
79
    {
80
        $persistence = $metadata->persistence;
81
        if (false === $this->entityUtil->isEntityTypeValid($persistence->collection)) {
0 ignored issues
show
Bug introduced by
Accessing collection on the interface As3\Modlr\Metadata\Inter...s\StorageLayerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
82
            throw MetadataException::invalidMetadata(
83
                $metadata->type,
84
                sprintf('The entity persistence collection "%s" is invalid based on the configured name format "%s"',
85
                        $persistence->collection,
0 ignored issues
show
Bug introduced by
Accessing collection on the interface As3\Modlr\Metadata\Inter...s\StorageLayerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
86
                        $this->entityUtil->getRestConfig()->getEntityFormat()
87
                )
88
            );
89
        }
90
    }
91
92
    /**
93
     * Validates that the proper database properties are set.
94
     *
95
     * @param   EntityMetadata  $metadata
96
     * @throws  MetadataException
97
     */
98
    private function validateDatabase(EntityMetadata $metadata)
99
    {
100
        $persistence = $metadata->persistence;
101
        if (false === $metadata->isChildEntity() && (empty($persistence->db) || empty($persistence->collection))) {
0 ignored issues
show
Bug introduced by
Accessing db on the interface As3\Modlr\Metadata\Inter...s\StorageLayerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing collection on the interface As3\Modlr\Metadata\Inter...s\StorageLayerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
102
            throw MetadataException::invalidMetadata($metadata->type, 'The persistence database and collection names cannot be empty.');
103
        }
104
    }
105
106
    /**
107
     * Validates the proper id strategy.
108
     *
109
     * @param   EntityMetadata  $metadata
110
     * @throws  MetadataException
111
     */
112
    private function validateIdStrategy(EntityMetadata $metadata)
113
    {
114
        $persistence = $metadata->persistence;
115
        $validIdStrategies = ['object'];
116
        if (!in_array($persistence->idStrategy, $validIdStrategies)) {
0 ignored issues
show
Bug introduced by
Accessing idStrategy on the interface As3\Modlr\Metadata\Inter...s\StorageLayerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
117
            throw MetadataException::invalidMetadata($metadata->type, sprintf('The persistence id strategy "%s" is invalid. Valid types are "%s"', $persistence->idStrategy, implode('", "', $validIdStrategies)));
0 ignored issues
show
Bug introduced by
Accessing idStrategy on the interface As3\Modlr\Metadata\Inter...s\StorageLayerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
118
        }
119
    }
120
}
121