Completed
Push — master ( 82e0b5...620952 )
by Jens
17:43
created

QueryHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getAll() 0 22 5
1
<?php
2
/**
3
 * @author @ct-jensschulze <[email protected]>
4
 */
5
6
namespace Commercetools\Commons\Helper;
7
8
9
use Commercetools\Core\Client;
10
use Commercetools\Core\Request\QueryAllRequestInterface;
11
12
class QueryHelper
13
{
14
    const DEFAULT_PAGE_SIZE = 500;
15
16
    public function getAll(Client $client, QueryAllRequestInterface $request)
17
    {
18
        $lastId = null;
19
        $data = ['results' => []];
20
        do {
21
            $request->sort('id')->limit(static::DEFAULT_PAGE_SIZE)->withTotal(false);
22
            if ($lastId != null) {
23
                $request->where('id > "' . $lastId . '"');
24
            }
25
            $response = $client->execute($request);
26
            if ($response->isError() || is_null($response->toObject())) {
27
                break;
28
            }
29
            $results = $response->toArray()['results'];
30
            $data['results'] = array_merge($data['results'], $results);
31
            $lastId = end($results)['id'];
32
        } while (count($results) >= static::DEFAULT_PAGE_SIZE);
33
34
        $result = $request->mapResult($data, $client->getConfig()->getContext());
35
36
        return $result;
37
    }
38
}
39