CollectionableTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 27
ccs 0
cts 13
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B toCollection() 0 15 6
1
<?php
2
3
namespace Majora\Framework\Model;
4
5
use Doctrine\Common\Collections\Collection;
6
7
/**
8
 * Trait for collectionable objects.
9
 */
10
trait CollectionableTrait
11
{
12
    /**
13
     * helper method to use for cast arrays to
14
     * collections of entities.
15
     *
16
     * @param Collection|array $data
17
     * @param string           $collectionClass
18
     *
19
     * @return EntityCollection
20
     */
21
    protected function toCollection($data, $collectionClass)
22
    {
23
        if ($data instanceof $collectionClass) {
24
            return $data;
25
        }
26
27
        $data = $data ?: array();
28
        if (!is_array($data) && !$data instanceof Collection) {
29
            throw new \InvalidArgumentException('Can transform only Collections or arrays.');
30
        }
31
32
        return new $collectionClass(
33
            is_array($data) ? $data : $data->toArray()
34
        );
35
    }
36
}
37