• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MichaelJ2324 / PHP-REST-Client / 15164546007

21 May 2025 02:13PM UTC coverage: 98.369%. Remained the same
15164546007

push

github

web-flow
Merge pull request #20 from MichaelJ2324/3.x

v3.0.7

0 of 2 new or added lines in 1 file covered. (0.0%)

1146 of 1165 relevant lines covered (98.37%)

1.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.44
/src/Endpoint/Provider/AbstractEndpointProvider.php
1
<?php
2

3
namespace MRussell\REST\Endpoint\Provider;
4

5
use MRussell\REST\Endpoint\Interfaces\EndpointInterface;
6
use MRussell\REST\Exception\Endpoint\InvalidRegistration;
7
use MRussell\REST\Exception\Endpoint\UnknownEndpoint;
8

9
abstract class AbstractEndpointProvider implements EndpointProviderInterface
10
{
11
    public const ENDPOINT_CLASS = 'class';
12

13
    public const ENDPOINT_PROPERTIES = 'properties';
14

15
    public const ENDPOINT_VERSIONS = 'versions';
16

17
    public const ENDPOINT_NAME = 'name';
18

19
    protected array $registry = [];
20

21
    /**
22
     * @inheritdoc
23
     * @throws InvalidRegistration
24
     */
25
    public function registerEndpoint(string $name, string $className, array $properties = []): static
3✔
26
    {
27
        try {
28
            $implements = class_implements($className);
3✔
29
            if (is_array($implements) && isset($implements[EndpointInterface::class])) {
2✔
30
                if (isset($properties[self::ENDPOINT_VERSIONS])) {
2✔
31
                    $versions = $properties[self::ENDPOINT_VERSIONS];
1✔
32
                    unset($properties[self::ENDPOINT_VERSIONS]);
1✔
33
                }
34

35
                $this->addEndpointRegistry($name, [
2✔
36
                    self::ENDPOINT_NAME => $name,
2✔
37
                    self::ENDPOINT_CLASS => $className,
2✔
38
                    self::ENDPOINT_PROPERTIES => $properties,
2✔
39
                    self::ENDPOINT_VERSIONS => $versions ?? [],
2✔
40
                ]);
2✔
41
                return $this;
2✔
42
            }
43
        } catch (\Exception) {
1✔
44
            //Class Implements failed to Load Class completely
45
        }
46

47
        throw new InvalidRegistration([$className]);
1✔
48
    }
49

50
    protected function addEndpointRegistry(string $name, array $properties): void
2✔
51
    {
52
        if (!isset($properties[self::ENDPOINT_CLASS])) {
2✔
53
            throw new InvalidRegistration([$name]);
1✔
54
        }
55

56
        if (!isset($properties[self::ENDPOINT_NAME])) {
1✔
57
            $properties[self::ENDPOINT_NAME] = $name;
1✔
58
        }
59

60
        $this->registry[$name] = $properties;
1✔
61
    }
62

63
    /**
64
     * @inheritdoc
65
     */
66
    public function hasEndpoint(string $name, string $version = null): bool
1✔
67
    {
68
        $definition = $this->getEndpointDefinition($name, $version);
1✔
69
        return !empty($definition);
1✔
70
    }
71

72
    protected function getEndpointDefinition(string $name, string $version = null): array
1✔
73
    {
74
        $definition = $this->registry[$name] ?? [];
1✔
75
        if (!empty($definition) && $version !== null && !empty($definition[self::ENDPOINT_VERSIONS])) {
1✔
76
            $ranges = $this->registry[$name][self::ENDPOINT_VERSIONS];
1✔
77
            if (is_string($ranges)) {
1✔
78
                $ranges = [$ranges];
×
79
            }
80

81
            if (!$this->isInVersionRange($version, $ranges)) {
1✔
82
                $definition = [];
1✔
83
            }
84
        }
85

86
        return $definition;
1✔
87
    }
88

89
    /**
90
     * @inheritdoc
91
     * @throws UnknownEndpoint
92
     */
93
    public function getEndpoint(string $name, string $version = null): EndpointInterface
2✔
94
    {
95
        if ($this->hasEndpoint($name, $version)) {
2✔
96
            return $this->buildEndpoint($name, $version);
1✔
97
        } else {
98
            throw new UnknownEndpoint($name);
1✔
99
        }
100
    }
101

102
    protected function buildEndpoint(string $name, string $version = null): EndpointInterface
1✔
103
    {
104
        $endPointDef = $this->getEndpointDefinition($name, $version);
1✔
105
        $Class = $endPointDef[self::ENDPOINT_CLASS];
1✔
106
        $properties = $endPointDef[self::ENDPOINT_PROPERTIES] ?? [];
1✔
107
        $Endpoint = new $Class();
1✔
108
        if (!empty($properties)) {
1✔
109
            foreach ($properties as $prop => $value) {
1✔
110
                $Endpoint->setProperty($prop, $value);
1✔
111
            }
112
        }
113

114
        return $Endpoint;
1✔
115
    }
116

117
    protected function isInVersionRange(string $version, array $ranges): bool
1✔
118
    {
119
        $is = true;
1✔
120
        foreach ($ranges as $compare => $range) {
1✔
121
            if (is_numeric($compare)) {
1✔
122
                $compare = "==";
1✔
123
            }
124

125
            $internalComp = true;
1✔
126
            if (is_array($range)) {
1✔
127
                foreach ($range as $c => $v) {
1✔
128
                    if (is_array($v)) {
1✔
129
                        continue;
1✔
130
                    }
131

132
                    if (!$this->isInVersionRange($version, [$c => $v])) {
1✔
133
                        $internalComp = false;
1✔
134
                        break;
1✔
135
                    }
136
                }
137
            } else {
138
                $internalComp = version_compare($version, $range, $compare);
1✔
139
            }
140

141
            if (!$internalComp) {
1✔
142
                $is = false;
1✔
143
                break;
1✔
144
            }
145
        }
146

147
        return $is;
1✔
148
    }
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc