AlmaUserData::isValidUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace App\Service;
4
5
use GuzzleHttp\Psr7\Response;
6
use SimpleXMLElement;
7
8
/**
9
 * Utility class used for parsing responses from the Alma API.
10
 */
11
class AlmaUserData
12
{
13
    /**
14
     * Given a guzzle response from Alma, return the list of fees as an associative array with each
15
     * fee and each fees properties
16
     *
17
     * @param Response $response
18
     * @return array
19
     */
20
    public function listFees(Response $response)
21
    {
22
        $sxml = new SimpleXMLElement($response->getBody());
23
24
        $list_fees = [];
25
26
        // "fee" is an array that includes each individual fee in the user "fees" object in Alma
27
        foreach ($sxml->fee as $indv_fee) {
28
            $list_fees[] = [
29
                'id' => (string)$indv_fee->id,
30
                'label' => (string)$indv_fee->type->attributes()->desc,
31
                'balance' => (string)$indv_fee->balance,
32
                'title' => (string)$indv_fee->title,
33
                'date' => new \DateTime($indv_fee->creation_time),
34
                'comment' => (string)$indv_fee->comment
35
            ];
36
        }
37
        return $list_fees;
38
    }
39
40
    /**
41
     * Get the full name of user from Alma API response
42
     *
43
     * @param Response $response
44
     * @return string
45
     */
46
    public function getFullNameAsString(Response $response)
47
    {
48
        $sxml = new SimpleXMLElement($response->getBody());
49
        return $sxml->full_name->__toString();
50
    }
51
52
    /**
53
     * Checks that there is exactly one user has primary_id set to 'Shib-uaId' property (the user UA id)
54
     *
55
     * @param Response $response
56
     * @return bool
57
     */
58
    public function isValidUser(Response $response)
59
    {
60
        $sxml = new SimpleXMLElement($response->getBody());
61
        return $sxml->attributes()->total_record_count == '1';
62
    }
63
}
64