Passed
Push — master ( ac51a8...1ecf9f )
by Dan Michael O.
02:12
created

Users   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 68
rs 10
wmc 16
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
F search() 0 56 16
1
<?php
2
3
namespace Scriptotek\Alma\Users;
4
5
use Scriptotek\Alma\ResourceList;
6
7
class Users extends ResourceList
8
{
9
    protected $resourceName = User::class;
10
11
    /**
12
     * Iterates over all users matching the given query.
13
     * Handles continuation.
14
     * @param string $query
15
     * @param array $options
16
     * @return \Generator
17
     */
18
    public function search($query, array $options = [])
19
    {
20
        // Max number of records to fetch. Set to 0 to fetch all.
21
        $limit = array_key_exists('limit', $options) ? $options['limit'] : 0;
22
23
        // Set to true to do a phrase search
24
        $phrase = array_key_exists('phrase', $options) ? $options['phrase'] : false;
25
26
        // Set to true to expand all query results to full records.
27
        // Please note that this will make queries significantly slower!
28
        $expand = array_key_exists('expand', $options) ? $options['expand'] : false;
29
30
        // Number of records to fetch each batch. Usually no need to change this.
31
        $batchSize = array_key_exists('batchSize', $options) ? $options['batchSize'] : 10;
32
33
        if ($limit != 0 && $limit < $batchSize) $batchSize = $limit;
34
35
        // The API will throw a 400 response if you include properly encoded spaces,
36
        // but underscores work as a substitute.
37
        $query = explode(' AND ', $query);
38
        $query = $phrase ? str_replace(' ', '_', $query) : str_replace(' ', ',', $query);
39
        $query = implode(' AND ', $query);
40
41
        $offset = 0;
42
        while (true) {
43
            $response = $this->client->getJSON('/users', ['q' => $query, 'limit' => $batchSize, 'offset' => $offset]);
44
45
            if ($response->total_record_count == 0) {
46
                break;
47
            }
48
49
            foreach ($response->user as $data) {
50
51
                // Contacts without a primary identifier will have the primary_id
52
                // field populated with something weird like "no primary id (123456789023)".
53
                // We ignore those.
54
                // See: https://github.com/scriptotek/php-alma-client/issues/6
55
                if (strpos($data->primary_id, 'no primary id') === 0) {
56
                    continue;
57
                }
58
59
                $user = User::fromResponse($this->client, $data);
60
                if ($expand) {
61
                    $user->fetch();
62
                }
63
                yield $user;
64
                $offset++;
65
            }
66
            if ($offset >= $response->total_record_count) {
67
                break;
68
            }
69
            if ($limit != 0 && $offset >= $limit) {
70
                break;
71
            }
72
        }
73
    }
74
}
75