Passed
Pull Request — master (#57)
by Daniel
16:36 queued 11:06
created

Collection::getDefaultQueryParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\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\ApiComponentsBundle\Annotation as Silverback;
20
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent;
21
use Silverback\ApiComponentsBundle\Entity\Utility\TimestampedTrait;
22
use Symfony\Component\Validator\Constraints as Assert;
23
use Traversable;
24
25
/**
26
 * @author Daniel West <[email protected]>
27
 *
28
 * @Silverback\Timestamped
29
 * @ORM\Entity
30
 */
31
class Collection extends AbstractComponent
32
{
33
    use TimestampedTrait;
34
35
    /**
36
     * @ORM\Column(nullable=false)
37
     * @Assert\NotNull(message="The resource class for a collection component is required")
38
     */
39
    private string $resourceClass;
40
41
    /**
42
     * @ORM\Column(type="integer", nullable=true)
43
     */
44
    private ?int $perPage;
45
46
    /**
47
     * @ORM\Column(type="json", nullable=true)
48
     */
49
    private ?array $defaultQueryParameters;
50
51
    /**
52
     * @ApiProperty(writable=false)
53
     */
54
    private array $collection = [];
55
56
    /**
57
     * @ApiProperty(writable=false)
58
     */
59
    private ?ArrayCollection $endpoints = null;
60
61
    public function getResourceClass(): string
62
    {
63
        return $this->resourceClass;
64
    }
65
66
    public function setResourceClass(string $resourceClass): self
67
    {
68
        $this->resourceClass = $resourceClass;
69
70
        return $this;
71
    }
72
73
    public function getPerPage(): ?int
74
    {
75
        return $this->perPage;
76
    }
77
78
    public function setPerPage(?int $perPage): self
79
    {
80
        $this->perPage = $perPage;
81
82
        return $this;
83
    }
84
85
    public function getDefaultQueryParameters(): ?array
86
    {
87
        return $this->defaultQueryParameters;
88
    }
89
90
    public function setDefaultQueryParameters(?array $defaultQueryParameters): self
91
    {
92
        $this->defaultQueryParameters = $defaultQueryParameters;
93
94
        return $this;
95
    }
96
97
    public function getCollection()
98
    {
99
        return $this->collection;
100
    }
101
102
    public function setCollection($collection): self
103
    {
104
        if (!$collection instanceof Traversable && !\is_array($collection)) {
105
            return $this;
106
        }
107
        $this->collection = $collection;
108
109
        return $this;
110
    }
111
112
    public function getEndpoints(): ?ArrayCollection
113
    {
114
        return $this->endpoints;
115
    }
116
117
    public function setEndpoints(ArrayCollection $endpoints): self
118
    {
119
        $this->endpoints = $endpoints;
120
121
        return $this;
122
    }
123
124
    public function addEndpoint(string $method, string $route): self
125
    {
126
        if (!$this->endpoints) {
127
            $this->endpoints = new ArrayCollection();
128
        }
129
        $this->endpoints->set($method, $route);
130
131
        return $this;
132
    }
133
}
134