Completed
Pull Request — master (#5)
by Jacob
02:31
created

StorageMetadataFactory::validateCollectionNaming()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
nc 2
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\StorageLayerInterface;
8
use As3\Modlr\Metadata\Interfaces\StorageMetadataFactoryInterface;
9
use As3\Modlr\Util\EntityUtility;
10
11
/**
12
 * Creates MongoDb storage Metadata instances for use with metadata drivers.
13
 * Is also responsible for validating storage objects.
14
 *
15
 * @author Jacob Bare <[email protected]>
16
 */
17
final class StorageMetadataFactory implements StorageMetadataFactoryInterface
18
{
19
    /**
20
     * @var EntityUtility
21
     */
22
    private $entityUtil;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param   EntityUtility   $entityUtl
0 ignored issues
show
Documentation introduced by
There is no parameter named $entityUtl. Did you maybe mean $entityUtil?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
28
     */
29
    public function __construct(EntityUtility $entityUtil)
30
    {
31
        $this->entityUtil = $entityUtil;
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function createInstance(array $mapping)
38
    {
39
        $persistence = new StorageMetadata();
40
41
        if (isset($mapping['db'])) {
42
            $persistence->db = $mapping['db'];
43
        }
44
45
        if (isset($mapping['collection'])) {
46
            $persistence->collection = $mapping['collection'];
47
        }
48
        return $persistence;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function handleLoad(EntityMetadata $metadata)
55
    {
56
        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...
57
            $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...
58
        }
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function handleValidate(EntityMetadata $metadata)
65
    {
66
        $this->validateIdStrategy($metadata);
67
        $this->validateDatabase($metadata);
68
        $this->validateCollectionNaming($metadata);
69
    }
70
71
72
73
    /**
74
     * Validates that the collection naming is correct, based on entity format config.
75
     *
76
     * @param   EntityMetadata  $metadata
77
     * @throws  MetadataException
78
     */
79
    private function validateCollectionNaming(EntityMetadata $metadata)
80
    {
81
        $persistence = $metadata->persistence;
82
        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...
83
            throw MetadataException::invalidMetadata(
84
                $metadata->type,
85
                sprintf('The entity persistence collection "%s" is invalid based on the configured name format "%s"',
86
                        $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...
87
                        $this->entityUtil->getRestConfig()->getEntityFormat()
88
                )
89
            );
90
        }
91
    }
92
93
    /**
94
     * Validates that the proper database properties are set.
95
     *
96
     * @param   EntityMetadata  $metadata
97
     * @throws  MetadataException
98
     */
99
    private function validateDatabase(EntityMetadata $metadata)
100
    {
101
        $persistence = $metadata->persistence;
102
        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...
103
            throw MetadataException::invalidMetadata($metadata->type, 'The persistence database and collection names cannot be empty.');
104
        }
105
    }
106
107
    /**
108
     * Validates the proper id strategy.
109
     *
110
     * @param   EntityMetadata  $metadata
111
     * @throws  MetadataException
112
     */
113
    private function validateIdStrategy(EntityMetadata $metadata)
114
    {
115
        $persistence = $metadata->persistence;
116
        $validIdStrategies = ['object'];
117
        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...
118
            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...
119
        }
120
    }
121
}
122