AbstractResource   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 8
c 4
b 0
f 0
dl 0
loc 36
ccs 0
cts 6
cp 0
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getOperations() 0 3 1
A addOperation() 0 4 1
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