Skip to content

Commit 5d214f1

Browse files
author
Ryan Yakstis
committed
Introduce a specification for API clients
Use the existing Client public interface as a means of defining an interface for all future API clients which may have differing implementations. https://ocramius.github.io/blog/when-to-declare-classes-final/ When trying to write some unit tests for a client class I had to implement, I found I couldn't mock out the DominionEnterprises\Api\Client class, since it had a final marker. This article suggests providing a public interface for final classes so you can avoid inheritance issues, but still use composition and programming to an interface to provide design flexibility. A side effect of this also allows mocking of final classes by using the interface instead. Signed-off-by: Ryan Yakstis <ryan.yakstis@dominionenterprises.com>
1 parent 1e4325b commit 5d214f1

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

src/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Client for apis
1010
*/
1111
final class Client
12+
implements ClientContract
1213
{
1314
/**
1415
* Flag to cache no requests

src/ClientContract.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
namespace DominionEnterprises\Api;
3+
4+
/**
5+
* Client for apis
6+
*/
7+
interface ClientContract
8+
{
9+
/**
10+
* Get access token and refresh token
11+
*
12+
* @return array two string values, access token and refresh token
13+
*/
14+
function getTokens();
15+
16+
/**
17+
* Search the API resource using the specified $filters
18+
*
19+
* @param string $resource
20+
* @param array $filters
21+
*
22+
* @return mixed opaque handle to be given to endIndex()
23+
*/
24+
function startIndex($resource, array $filters = []);
25+
26+
/**
27+
* @see startIndex()
28+
*/
29+
function index($resource, array $filters = []);
30+
31+
/**
32+
* Get the details of an API resource based on $id
33+
*
34+
* @param string $resource
35+
* @param string $id
36+
*
37+
* @return mixed opaque handle to be given to endGet()
38+
*/
39+
function startGet($resource, $id);
40+
41+
/**
42+
* @see startGet()
43+
*/
44+
function get($resource, $id);
45+
46+
/**
47+
* Create a new instance of an API resource using the provided $data
48+
*
49+
* @param string $resource
50+
* @param array $data
51+
*
52+
* @return mixed opaque handle to be given to endPost()
53+
*/
54+
function startPost($resource, array $data);
55+
56+
/**
57+
* @see startPost()
58+
*/
59+
function post($resource, array $data);
60+
61+
/**
62+
* Update an existing instance of an API resource specified by $id with the provided $data
63+
*
64+
* @param string $resource
65+
* @param string $id
66+
* @param array $data
67+
*
68+
* @return mixed opaque handle to be given to endPut()
69+
*/
70+
function startPut($resource, $id, array $data);
71+
72+
/**
73+
* @see startPut()
74+
*/
75+
function put($resource, $id, array $data);
76+
77+
/**
78+
* Delete an existing instance of an API resource specified by $id
79+
*
80+
* @param string $resource
81+
* @param string $id
82+
* @param array $data
83+
*
84+
* @return mixed opaque handle to be given to endDelete()
85+
*/
86+
function startDelete($resource, $id, array $data = null);
87+
88+
/**
89+
* @see startDelete()
90+
*/
91+
function delete($resource, $id, array $data = null);
92+
93+
/**
94+
* Get response of start*() method
95+
*
96+
* @param mixed $handle opaque handle from start*()
97+
*
98+
* @return Response
99+
*/
100+
function end($handle);
101+
102+
/**
103+
* Set the default headers
104+
*
105+
* @param array The default headers
106+
*
107+
* @return void
108+
*/
109+
function setDefaultHeaders($defaultHeaders);
110+
}

0 commit comments

Comments
 (0)