Passed
Push — 2.x ( 0b5227...cb81b7 )
by butschster
16:17
created

IlluminateCollectionFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 32
ccs 10
cts 12
cp 0.8333
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A withCollectionClass() 0 5 1
A getInterface() 0 3 1
A collect() 0 3 1
A __construct() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Collection;
6
7
use Cycle\ORM\Exception\CollectionFactoryException;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * @template TCollection of Collection
12
 * @template-implements CollectionFactoryInterface<TCollection>
13
 */
14
final class IlluminateCollectionFactory implements CollectionFactoryInterface
15
{
16
    /** @var class-string<TCollection> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TCollection> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TCollection>.
Loading history...
17
    private string $class = Collection::class;
18
19 102
    public function __construct()
20
    {
21 102
        if (!class_exists(Collection::class, true)) {
22
            throw new CollectionFactoryException(
23
                sprintf(
24
                    'There is no %s class. To resolve this issue you can install `illuminate/collections` package.',
25
                    Collection::class
26
                )
27
            );
28
        }
29
    }
30
31 90
    public function getInterface(): ?string
32
    {
33 90
        return Collection::class;
34
    }
35
36 6
    public function withCollectionClass(string $class): static
37
    {
38 6
        $clone = clone $this;
39 6
        $clone->class = $class;
40 6
        return $clone;
41
    }
42
43 36
    public function collect(iterable $data): Collection
44
    {
45 36
        return new $this->class($data);
46
    }
47
}
48