Code Duplication    Length = 45-47 lines in 2 locations

src/service/UserList.php 1 location

@@ 131-177 (lines=47) @@
128
     * @return array|Segment
129
     * @throws ClientException
130
     */
131
    public function getUserList($id = null)
132
    {
133
        $compiledUrl = self::BASE_URL_USER;
134
135
        $requestBody = $this->twigCompiler->getTwig()->render(
136
            self::API_VERSION . '/' . self::GET_USER_LIST_TPL,
137
            [
138
                'clientCustomerId' => $this->clientCustomerId,
139
                'id' => $id
140
            ]
141
        );
142
143
        try {
144
            $response = $this->client->request('POST', $compiledUrl, ['body' => $requestBody]);
145
        } catch (RequestException $e) {
146
            $response = $e->getResponse();
147
        }
148
149
150
        $repositoryResponse = ApiResponse::fromResponse($response);
151
152
        if (!$repositoryResponse->isSuccessful()) {
153
            throw ClientException::failed($repositoryResponse);
154
        }
155
156
        if (!isset($repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'])
157
        ) {
158
            throw ClientException::failed($repositoryResponse);
159
        }
160
161
162
        $entries = $repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'];
163
164
        if (is_array($entries) && isset($entries['id'])) {
165
            //ok, this is the case when you search a specific user list. So we don't have an array of array in response but just a single array
166
167
            return Segment::fromArray($entries);
168
        }
169
170
        $segments = [];
171
172
        foreach ($entries as $entry) {
173
            $segments[] = Segment::fromArray($entry);
174
        }
175
176
        return $segments;
177
    }
178
}
179

src/service/UserListClientService.php 1 location

@@ 128-172 (lines=45) @@
125
     * @return UserListClient[]|UserListClient
126
     * @throws ClientException
127
     */
128
    public function getUserClientList($id = null)
129
    {
130
        $compiledUrl = self::URL_LIST_CLIENT;
131
132
        $requestBody = $this->twigCompiler->getTwig()->render(
133
            self::API_VERSION . '/' . self::GET_USER_CLIENT_LIST_TPL,
134
            [
135
                'clientCustomerId' => $this->clientCustomerId,
136
                'userlistid' => $id
137
            ]
138
        );
139
140
        try {
141
            $response = $this->client->request('POST', $compiledUrl, ['body' => $requestBody]);
142
        } catch (RequestException $e) {
143
            $response = $e->getResponse();
144
        }
145
146
147
        $repositoryResponse = ApiResponse::fromResponse($response);
148
149
        if (!$repositoryResponse->isSuccessful()) {
150
            throw ClientException::failed($repositoryResponse);
151
        }
152
153
        if (!isset($repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'])
154
        ) {
155
            throw ClientException::failed($repositoryResponse);
156
        }
157
158
159
        $entries = $repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'];
160
161
        if (is_array($entries) && isset($entries['id'])) {
162
            return UserListClient::fromArray($entries);
163
        }
164
165
        $licenses = [];
166
167
        foreach ($entries as $entry) {
168
            $licenses[] = UserListClient::fromArray($entry);
169
        }
170
171
        return $licenses;
172
    }
173
}
174