User::__construct()   B
last analyzed

Complexity

Conditions 7
Paths 27

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 20
c 2
b 0
f 0
nc 27
nop 1
dl 0
loc 30
rs 8.6666
1
<?php
2
3
namespace App\Alma;
4
5
use Scriptotek\Alma\Client as AlmaClient;
6
use Scriptotek\Alma\Exception\ResourceNotFound;
7
use Scriptotek\Alma\Users\User as AlmaUser;
8
9
class User
10
{
11
    public $primaryId;
12
    public $barcodes;
13
    public $universityIds;
14
    public $group;
15
    public $email;
16
    public $phone;
17
    public $lang;
18
    public $firstName;
19
    public $lastName;
20
    public $name;
21
    public $blocks;
22
    public $type = 'alma';
23
24
    protected $user;
25
26
    public function __construct(AlmaUser $user)
27
    {
28
        $this->user = $user;
29
30
        foreach ($user->contact_info->email as $e) {
31
            if ($e->preferred) {
32
                $this->email = $e->email_address;
33
            }
34
        }
35
        foreach ($user->contact_info->phone as $e) {
36
            if ($e->preferred) {
37
                $this->phone = $e->phone_number;
38
            }
39
        }
40
        if (in_array($user->preferred_language->value, ['no', 'nb', 'nob'])) {
41
            $this->lang = 'nob';
42
        } elseif (in_array($user->preferred_language->value, ['nn', 'nno'])) {
43
            $this->lang = 'nno';
44
        } else {
45
            $this->lang = 'eng';
46
        }
47
48
        $this->firstName = $user->first_name;
49
        $this->lastName = $user->last_name;
50
        $this->primaryId = $user->primary_id;
51
        $this->group = $user->user_group->desc;
52
        $this->name = $this->lastName . ', ' . $this->firstName;
53
54
        $this->blocks = array_filter($user->user_block, function ($block) {
0 ignored issues
show
Bug introduced by
It seems like $user->user_block can also be of type null; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $this->blocks = array_filter(/** @scrutinizer ignore-type */ $user->user_block, function ($block) {
Loading history...
55
            return $block->block_status == 'ACTIVE';
56
        });
57
    }
58
59
    /**
60
     * Lookup user by primary or non-primary identifier.
61
     *
62
     * @param AlmaClient $alma
63
     * @param $identifier
64
     * @return User|null
65
     */
66
    public static function lookup(AlmaClient $alma, $identifier)
67
    {
68
        if (empty($identifier)) {
69
            return null;
70
        }
71
        if (is_null($alma->key)) {
0 ignored issues
show
introduced by
The condition is_null($alma->key) is always false.
Loading history...
72
            return null;
73
        }
74
        try {
75
            return new self($alma->users->get($identifier)->init());
76
        } catch (ResourceNotFound $e) {
77
            return null;
78
        }
79
    }
80
81
    public static function isUserBarcode($value)
82
    {
83
        // 2-5 letters followed by 5-8 digits, making up 10 characters in total.
84
        return preg_match('/^[a-zA-Z]{2}[0-9a-zA-Z]{3}[0-9a-zA-Z]{5}$/', $value);
85
    }
86
87
    public function getBarcodes()
88
    {
89
//        if (self::isUserBarcode($this->primaryId)) {
90
//            return $this->primaryId;
91
//        }
92
        return $this->user->barcodes;
93
    }
94
95
    public function getUniversityIds()
96
    {
97
        return $this->user->universityIds;
98
    }
99
100
    public function getFees()
101
    {
102
        return $this->user->fees->total_sum;
103
    }
104
105
    public function getIdentifiers()
106
    {
107
        return $this->user->identifiers;
108
    }
109
}
110