Test Failed
Branch feature/v1_stable_fixes (e805e7)
by Diego
04:22
created

Enumerations::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Blackmine\Repository;
6
7
use Blackmine\Collection\IdentityCollection;
8
use Blackmine\Exception\MethodNotImplementedException;
9
use Blackmine\Model\AbstractModel;
10
use Blackmine\Model\Enumeration\AbstractEnumeration;
11
use Blackmine\Model\Enumeration\DocumentCategory;
12
use Blackmine\Model\Enumeration\IssuePriority;
13
use Blackmine\Model\Enumeration\TimeEntryActivity;
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Error;
16
17
class Enumerations extends AbstractRepository
18
{
19
20
    public const API_ROOT = "enumerations";
21
22
    public const ENUM_ISSUE_PRIORITIES = "issue_priorities";
23
    public const ENUM_TIME_ENTRY_ACTIVITIES = "time_entry_activities";
24
    public const ENUM_TYPE_DOCUMENT_CATEGORIES = "document_categories";
25
26
    protected static array $allowed_enums = [
27
        self::ENUM_ISSUE_PRIORITIES => IssuePriority::class,
28
        self::ENUM_TIME_ENTRY_ACTIVITIES => TimeEntryActivity::class,
29
        self::ENUM_TYPE_DOCUMENT_CATEGORIES => DocumentCategory::class
30
    ];
31
32
    public function getModelClass(): string
33
    {
34
        return AbstractEnumeration::class;
35
    }
36
37
    public function all(string $endpoint = null): IdentityCollection
38
    {
39
        if (array_key_exists($endpoint, self::$allowed_enums)) {
40
            $api_response = $this->client->get($this->getEndpoint() . "/" . $endpoint . "." . $this->client->getFormat());
41
            if (isset($api_response->getData()[$endpoint])) {
42
                $elements = [];
43
44
                foreach ($api_response->getData()[$endpoint] as $item) {
45
                    $object_class = self::$allowed_enums[$endpoint];
46
                    $object = new $object_class();
47
                    $object->fromArray($item);
48
49
                    $elements[] = $object;
50
                }
51
52
                return new IdentityCollection($elements);
53
            }
54
        }
55
56
        return new IdentityCollection();
57
    }
58
59
    /**
60
     * @throws MethodNotImplementedException
61
     */
62
    public function create(AbstractModel $model): ?AbstractModel
63
    {
64
        throw new MethodNotImplementedException(
65
            "Method " . __FUNCTION__ . " not implemented for apì: " . self::API_ROOT
66
        );
67
    }
68
69
    /**
70
     * @throws MethodNotImplementedException
71
     */
72
    public function get(mixed $id): ?AbstractModel
73
    {
74
        throw new MethodNotImplementedException(
75
            "Method " . __FUNCTION__ . " not implemented for apì: " . self::API_ROOT
76
        );
77
    }
78
79
    /**
80
     * @throws MethodNotImplementedException
81
     */
82
    public function update(AbstractModel $model): ?AbstractModel
83
    {
84
        throw new MethodNotImplementedException(
85
            "Method " . __FUNCTION__ . " not implemented for apì: " . self::API_ROOT
86
        );
87
    }
88
89
    /**
90
     * @throws MethodNotImplementedException
91
     */
92
    public function delete(AbstractModel $model): void
93
    {
94
        throw new MethodNotImplementedException(
95
            "Method " . __FUNCTION__ . " not implemented for apì: " . self::API_ROOT
96
        );
97
    }
98
}
99