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

MichaelJ2324 / PHP-REST-Client / 13080829877

31 Jan 2025 09:10PM UTC coverage: 94.359% (+2.5%) from 91.889%
13080829877

push

github

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

v3.0.1 Test Coverage + Auth Request Logging/Handling

67 of 73 new or added lines in 11 files covered. (91.78%)

1 existing line in 1 file now uncovered.

1037 of 1099 relevant lines covered (94.36%)

1.48 hits per line

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

96.15
/src/Endpoint/Abstracts/AbstractSmartEndpoint.php
1
<?php
2

3
namespace MRussell\REST\Endpoint\Abstracts;
4

5
use GuzzleHttp\Psr7\Request;
6
use MRussell\REST\Endpoint\Data\DataInterface;
7
use MRussell\REST\Endpoint\Data\EndpointData;
8
use MRussell\REST\Exception\Endpoint\InvalidData;
9
use MRussell\REST\Exception\Endpoint\InvalidDataType;
10

11
abstract class AbstractSmartEndpoint extends AbstractEndpoint
12
{
13
    public const PROPERTY_DATA = 'data';
14

15
    /**
16
     * @inheritdoc
17
     */
18
    protected static array $_DEFAULT_PROPERTIES = [
19
        self::PROPERTY_URL => '',
20
        self::PROPERTY_HTTP_METHOD => '',
21
        self::PROPERTY_AUTH => false,
22
        self::PROPERTY_DATA => [
23
            EndpointData::DATA_PROPERTY_DEFAULTS => [],
24
            EndpointData::DATA_PROPERTY_NULLABLE => [],
25
        ],
26
    ];
27

28
    protected string $_dataInterface = EndpointData::class;
29

30
    /**
31
     * The data being passed to the API Endpoint.
32
     * Uses the DataInterface to provide a more robust way of configuring data and an automation API
33
     */
34
    protected string|array|\ArrayAccess|null $_data;
35

36
    public function __construct(array $properties = [], array $urlArgs = [])
1✔
37
    {
38
        parent::__construct($properties, $urlArgs);
1✔
39
        $this->setData($this->buildDataObject());
1✔
40
    }
41

42
    /**
43
     * @inheritdoc
44
     * Passes through the data properties on the Data Object
45
     * @return $this
46
     */
47
    public function setProperties(array $properties): static
1✔
48
    {
49
        if (!isset($properties[self::PROPERTY_DATA])) {
1✔
50
            $properties[self::PROPERTY_DATA] = [];
1✔
51
        }
52

53
        parent::setProperties($properties);
1✔
54
        $this->configureDataProperties();
1✔
55
        return $this;
1✔
56
    }
57

58
    /**
59
     * @inheritdoc
60
     */
61
    public function setProperty(string $name, $value): static
2✔
62
    {
63
        parent::setProperty($name, $value);
2✔
64
        if ($name === self::PROPERTY_DATA && isset($this->_data)) {
2✔
65
            $this->configureDataProperties();
1✔
66
        }
67

68
        return $this;
2✔
69
    }
70

71
    /**
72
     * @inheritdoc
73
     */
74
    public function setData(string|array|\ArrayAccess|null $data): static
3✔
75
    {
76
        if ($data instanceof DataInterface) {
3✔
77
            $this->_data = $data;
1✔
78
        } elseif (is_array($data)) {
3✔
79
            $this->getData()->reset();
2✔
80
            $this->getData()->set($data);
1✔
81
        } elseif (is_null($data)) {
2✔
82
            $this->getData()->clear();
1✔
83
        } else {
84
            throw new InvalidDataType(static::class);
1✔
85
        }
86

87
        return $this;
1✔
88
    }
89

90
    /**
91
     * Get the current data object, or build out a new one if one is not set
92
     */
93
    public function getData(): DataInterface
1✔
94
    {
95
        if (!isset($this->_data)) {
1✔
96
            $this->_data = $this->buildDataObject();
1✔
97
        }
98

99
        $data = parent::getData();
1✔
100
        if (!($data instanceof DataInterface)) {
1✔
101
            $di = $this->buildDataObject();
×
NEW
102
            $this->_data = $di->set($data);
×
103
        }
104

105
        return $this->_data;
1✔
106
    }
107

108
    /**
109
     * Passes Data properties to Endpoint Data object
110
     * @return $this
111
     */
112
    protected function configureDataProperties(): static
2✔
113
    {
114
        $dataProps = $this->getProperty(self::PROPERTY_DATA);
2✔
115
        if (!empty($dataProps)) {
2✔
116
            $this->getData()->setProperties($dataProps);
2✔
117
        }
118

119
        return $this;
2✔
120
    }
121

122
    /**
123
     * Parse Data Object to array for handling by Guzzle
124
     * @param $data
125
     */
126
    protected function configureRequest(Request $request, $data): Request
1✔
127
    {
128
        $parsedData = $data;
1✔
129
        if ($data instanceof DataInterface) {
1✔
130
            $parsedData = $data->toArray();
1✔
131
            if (method_exists($data, 'isNull')) {
1✔
132
                $parsedData = $data->isNull() ? null : $parsedData;
1✔
133
            }
134
        }
135

136
        return parent::configureRequest($request, $parsedData);
1✔
137
    }
138

139
    /**
140
     * @inheritDoc
141
     * Reset data
142
     * @return $this
143
     */
144
    public function reset(): static
1✔
145
    {
146
        $this->getData()->reset();
1✔
147
        return parent::reset();
1✔
148
    }
149

150
    /**
151
     * Build out the configured Data Object for the Endpoint
152
     * @throws InvalidData
153
     */
154
    protected function buildDataObject(): DataInterface
2✔
155
    {
156
        $implements = class_implements($this->_dataInterface);
2✔
157
        if (is_array($implements) && isset($implements[DataInterface::class])) {
2✔
158
            return new $this->_dataInterface([], $this->getProperty(self::PROPERTY_DATA) ?? []);
2✔
159
        }
160

161
        throw new InvalidData($this->_dataInterface . " does not implement MRussell\\REST\\Endpoint\\Data\\DataInterface");
1✔
162
    }
163
}
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

© 2025 Coveralls, Inc