Total Complexity | 48 |
Total Lines | 288 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like DigiSignClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DigiSignClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | final class DigiSignClient implements DigiSignClientInterface |
||
32 | { |
||
33 | public const HTTP_NO_CONTENT = 204; |
||
34 | public const HTTP_BAD_REQUEST = 400; |
||
35 | public const HTTP_UNAUTHORIZED = 401; |
||
36 | public const HTTP_NOT_FOUND = 404; |
||
37 | public const HTTP_INTERNAL_SERVER_ERROR = 500; |
||
38 | |||
39 | /** @var ClientInterface */ |
||
40 | private $httpClient; |
||
41 | |||
42 | /** @var RequestFactoryInterface */ |
||
43 | private $requestFactory; |
||
44 | |||
45 | /** @var StreamFactoryInterface */ |
||
46 | private $streamFactory; |
||
47 | |||
48 | /** @var UriFactoryInterface */ |
||
49 | private $uriFactory; |
||
50 | |||
51 | public function __construct( |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return mixed[]|null |
||
65 | */ |
||
66 | public static function parseResponse(ResponseInterface $response): ?array |
||
67 | { |
||
68 | $body = (string)$response->getBody(); |
||
69 | |||
70 | if ($body === '') { |
||
71 | if ($response->getStatusCode() === self::HTTP_NO_CONTENT) { |
||
72 | return null; |
||
73 | } |
||
74 | |||
75 | throw new RuntimeException('Empty response body'); |
||
76 | } |
||
77 | |||
78 | try { |
||
79 | $decoded = json_decode($body, true); |
||
80 | |||
81 | if (!is_array($decoded)) { |
||
82 | throw new JsonException(); |
||
83 | } |
||
84 | |||
85 | return $decoded; |
||
86 | } catch (JsonException $e) { |
||
87 | throw new RuntimeException('Unable to parse response', 0, $e); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param mixed[] $options |
||
93 | */ |
||
94 | public function request(string $method, string $uri, array $options = []): ResponseInterface |
||
95 | { |
||
96 | $psrUri = $this->createUri($uri, $options); |
||
97 | $request = $this->createRequest($method, $psrUri, $options); |
||
98 | $response = $this->httpClient->sendRequest($request); |
||
99 | |||
100 | $this->checkResponse($response); |
||
101 | |||
102 | return $response; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param mixed $value |
||
107 | * |
||
108 | * @throws JsonException When the value cannot be json-encoded |
||
109 | */ |
||
110 | public static function jsonEncode($value): string |
||
111 | { |
||
112 | $json = json_encode($value); |
||
113 | |||
114 | if ($json === false || json_last_error() !== JSON_ERROR_NONE) { |
||
115 | throw new JsonException(json_last_error_msg(), json_last_error()); |
||
116 | } |
||
117 | |||
118 | return $json; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return mixed[] |
||
123 | * |
||
124 | * @throws JsonException When the json-string is invalid |
||
125 | */ |
||
126 | public static function jsonDecode(string $json): array |
||
127 | { |
||
128 | $value = json_decode($json, true); |
||
129 | |||
130 | if (!is_array($value) || json_last_error() !== JSON_ERROR_NONE) { |
||
131 | throw new JsonException(json_last_error_msg(), json_last_error()); |
||
132 | } |
||
133 | |||
134 | return $value; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param mixed[] $options |
||
139 | */ |
||
140 | private function createUri(string $uri, array $options): UriInterface |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param mixed[] $options |
||
179 | */ |
||
180 | private function createRequest(string $method, UriInterface $uri, array $options): RequestInterface // phpcs:ignore |
||
272 | } |
||
273 | |||
274 | private function checkResponse(ResponseInterface $response): void |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @param mixed[] $json |
||
298 | * @return mixed[] |
||
299 | */ |
||
300 | protected static function normalizeJson(array $json): array |
||
319 | } |
||
320 | } |
||
321 |