AbstractResource::getOperations()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Resource;
10
11
use Cake\Collection\Collection;
12
use Gorynych\Operation\ResourceOperationInterface;
13
14
abstract class AbstractResource
15
{
16
    public const NUMERIC_ID = '(?P<id>[\d]+)';
17
    public const ALNUM_ID = '(?P<id>[\w]+)';
18
19
    public ?string $id;
20
21
    /** @var ResourceOperationInterface[] */
22
    protected array $operations = [];
23
24
    /**
25
     * Returns path of the resource
26
     *
27
     * @return string
28
     * @example '/resources/'
29
     * @example '/resources/' . $this->id ?? self::NUMERIC_ID
30
     */
31
    abstract public function getPath(): string;
32
33
    /**
34
     * Adds given operation to the resource
35
     *
36
     * @param ResourceOperationInterface $operation
37
     */
38
    public function addOperation(ResourceOperationInterface $operation): void
39
    {
40
        $operation->setResource($this);
41
        $this->operations[] = $operation;
42
    }
43
44
    /**
45
     * @return ResourceOperationInterface[]|Collection
46
     */
47
    public function getOperations(): Collection
48
    {
49
        return new Collection($this->operations);
50
    }
51
}
52