Passed
Push — v2 ( a6d212...5d15db )
by Daniel
04:43
created

Collection::addEndpoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Entity\Component;
15
16
use ApiPlatform\Core\Annotation\ApiProperty;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Doctrine\ORM\Mapping as ORM;
19
use Silverback\ApiComponentBundle\Entity\Core\AbstractComponent;
20
use Symfony\Component\Validator\Constraints as Assert;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 * @ORM\Entity
25
 */
26
class Collection extends AbstractComponent
27
{
28
    /**
29
     * @ORM\Column(nullable=false)
30
     * @Assert\NotNull(message="The resource class for a collection component is required")
31
     */
32
    private string $resourceClass;
33
34
    /**
35
     * @ORM\Column(type="integer", nullable=true)
36
     */
37
    private ?int $perPage;
38
39
    /**
40
     * @ORM\Column(type="array", nullable=true)
41
     */
42
    private ?array $defaultQueryParameters;
43
44
    /**
45
     * @ApiProperty(writable=false)
46
     */
47
    private array $collection = [];
48
49
    /**
50
     * @ApiProperty(writable=false)
51
     */
52
    private ?ArrayCollection $endpoints = null;
53
54
    public function getResourceClass(): string
55
    {
56
        return $this->resourceClass;
57
    }
58
59
    public function setResourceClass(string $resourceClass): self
60
    {
61
        $this->resourceClass = $resourceClass;
62
63
        return $this;
64
    }
65
66
    public function getPerPage(): ?int
67
    {
68
        return $this->perPage;
69
    }
70
71
    public function setPerPage(?int $perPage): self
72
    {
73
        $this->perPage = $perPage;
74
75
        return $this;
76
    }
77
78
    public function getDefaultQueryParameters(): ?array
79
    {
80
        return $this->defaultQueryParameters;
81
    }
82
83
    public function setDefaultQueryParameters(?array $defaultQueryParameters): self
84
    {
85
        $this->defaultQueryParameters = $defaultQueryParameters;
86
87
        return $this;
88
    }
89
90
    public function getCollection()
91
    {
92
        return $this->collection;
93
    }
94
95
    public function setCollection($collection): self
96
    {
97
        if (!$collection instanceof Traversable && !\is_array($collection)) {
0 ignored issues
show
Bug introduced by
The type Silverback\ApiComponentB...y\Component\Traversable was not found. Did you mean Traversable? If so, make sure to prefix the type with \.
Loading history...
98
            return $this;
99
        }
100
        $this->collection = $collection;
101
102
        return $this;
103
    }
104
105
    public function getEndpoints(): ?ArrayCollection
106
    {
107
        return $this->endpoints;
108
    }
109
110
    public function setEndpoints(ArrayCollection $endpoints): self
111
    {
112
        $this->endpoints = $endpoints;
113
114
        return $this;
115
    }
116
117
    public function addEndpoint(string $method, string $route): self
118
    {
119
        if (!$this->endpoints) {
120
            $this->endpoints = new ArrayCollection();
121
        }
122
        $this->endpoints->set($method, $route);
123
124
        return $this;
125
    }
126
}
127