CollectionableTrait::toCollection()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 8.8571
cc 6
eloc 8
nc 5
nop 2
crap 42
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