Passed
Push — master ( d4f64f...42f03e )
by Dan Michael O.
03:47
created

User::isUserBarcode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Alma;
4
5
use Scriptotek\Alma\Exception\RequestFailed;
0 ignored issues
show
Bug introduced by
The type Scriptotek\Alma\Exception\RequestFailed was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Scriptotek\Alma\Client as AlmaClient;
0 ignored issues
show
Bug introduced by
The type Scriptotek\Alma\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Scriptotek\Alma\Users\User as AlmaUser;
0 ignored issues
show
Bug introduced by
The type Scriptotek\Alma\Users\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class User
10
{
11
    public $primaryId;
12
    public $barcode;
13
    public $universityId;
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
    /**
27
     * Lookup user by primary or non-primary identifier.
28
     *
29
     * @param AlmaClient $alma
30
     * @param $identifier
31
     * @return User|null
32
     */
33
    public static function lookup(AlmaClient $alma, $identifier)
34
    {
35
        try {
36
            return new self($alma->users->get($identifier));
37
        } catch (RequestFailed $exception) {
38
            $res = $alma->users->search('identifiers~' . $identifier, ['limit' => 1]);
39
            return count($res) ? new self($res[0]) : null;
40
        }
41
    }
42
43
    public static function isUserBarcode($value)
44
    {
45
        // 2-5 letters followed by 5-8 digits, making up 10 characters in total.
46
        return preg_match('/^[a-zA-Z]{2}[0-9a-zA-Z]{3}[0-9a-zA-Z]{5}$/', $value);
47
    }
48
49
    public function __construct(AlmaUser $user)
50
    {
51
        $this->user = $user;
52
53
        foreach ($user->contact_info->email as $e) {
54
            if ($e->preferred) {
55
                $this->email = $e->email_address;
56
            }
57
        }
58
        foreach ($user->contact_info->phone as $e) {
59
            if ($e->preferred) {
60
                $this->phone = $e->phone_number;
61
            }
62
        }
63
        if (in_array($user->preferred_language->value, ['no', 'nb', 'nob'])) {
64
            $this->lang = 'nob';
65
        } elseif (in_array($user->preferred_language->value, ['nn', 'nno'])) {
66
            $this->lang = 'nno';
67
        } else {
68
            $this->lang = 'eng';
69
        }
70
71
        $this->firstName = $user->first_name;
72
        $this->lastName = $user->last_name;
73
        $this->primaryId = $user->primary_id;
74
        $this->group = $user->user_group->desc;
75
        $this->name = $this->lastName . ', ' . $this->firstName;
76
        $this->blocks = $user->user_block ?: [];
77
    }
78
79
    public function getBarcode()
80
    {
81
        if (self::isUserBarcode($this->primaryId)) {
82
            return $this->primaryId;
83
        }
84
        return $this->user->barcode;
85
    }
86
87
    public function getUniversityId()
88
    {
89
        return $this->user->universityId;
90
    }
91
92
    public function getFees()
93
    {
94
        return $this->user->fees->total_sum;
95
    }
96
}
97