Passed
Branch php-scrutinizer (9ddcba)
by Jens
09:45
created

QueryHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 25
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

1 Method

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