AlmaUsers::findLocalUserFromAlmaUser()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 17
rs 9.2222
1
<?php declare(strict_types=1);
2
3
namespace App\Alma;
4
5
use App\User as LocalUser;
6
use App\UserIdentifier;
7
use Scriptotek\Alma\Client as AlmaClient;
8
9
class AlmaUsers
10
{
11
    /**
12
     * @var AlmaClient
13
     */
14
    protected $alma;
15
16
    /**
17
     * AlmaUsers constructor.
18
     * @param AlmaClient $alma
19
     */
20
    public function __construct(AlmaClient $alma)
21
    {
22
        $this->alma = $alma;
23
    }
24
25
    public function hasKey()
26
    {
27
        return !is_null($this->alma->key);
28
    }
29
30
    /**
31
     * Find Alma user by some ID.
32
     *
33
     * @param string $userId
34
     * @return User|null
35
     */
36
    public function findById(?string $userId)
37
    {
38
        return User::lookup($this->alma, $userId);
39
    }
40
41
    /**
42
     * Find Alma user from local user.
43
     *
44
     * @param LocalUser $user
45
     * @return User|null
46
     */
47
    public function findFromLocalUser(LocalUser $user)
48
    {
49
        if (!is_null($user->alma_primary_id)) {
50
            $almaUser = $this->findById($user->alma_primary_id);
51
            if (!is_null($almaUser)) {
52
                return $almaUser;
53
            }
54
        }
55
        foreach ($user->identifiers as $identifier) {
56
            $almaUser = $this->findById($identifier['value']);
57
            if (!is_null($almaUser)) {
58
                return $almaUser;
59
            }
60
        }
61
        return null;
62
    }
63
64
    /**
65
     * Find local User from Alma User.
66
     *
67
     * @param User $almaUser
68
     * @return LocalUser|null
69
     */
70
    public function findLocalUserFromAlmaUser(User $almaUser)
71
    {
72
        if (!is_null($almaUser->primaryId)) {
73
            $localUser = LocalUser::where('alma_primary_id', '=', $almaUser->primaryId)->first();
74
            if (!is_null($localUser)) {
75
                return $localUser;
76
            }
77
        }
78
        foreach ($almaUser->getIdentifiers() as $identifier) {
79
            if ($identifier->status == 'ACTIVE') {
80
                $userIdent = UserIdentifier::where('value', '=', $identifier->value)->first();
81
                if (!is_null($userIdent)) {
82
                    return $userIdent->user;
83
                }
84
            }
85
        }
86
        return null;
87
    }
88
89
    /**
90
     * Update the local user object with user data from Alma.
91
     * Returns true if successful, false if the user could no longer be found in Alma.
92
     *
93
     * @param LocalUser $localUser
94
     * @param User|null $almaUser
95
     * @return bool
96
     */
97
    public function updateLocalUserFromAlmaUser(LocalUser $localUser, User $almaUser = null)
98
    {
99
        if (is_null($almaUser)) {
100
            $almaUser = $this->findFromLocalUser($localUser);
101
        }
102
103
        if (is_null($almaUser)) {
104
            $localUser->in_alma = false;
105
            return false;
106
        }
107
108
        // Set user props
109
        $localUser->in_alma = true;
110
        $localUser->setAlmaPrimaryId($almaUser->primaryId, true);
0 ignored issues
show
Bug introduced by
It seems like $almaUser->primaryId can also be of type null; however, parameter $value of App\User::setAlmaPrimaryId() does only seem to accept string, 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

110
        $localUser->setAlmaPrimaryId(/** @scrutinizer ignore-type */ $almaUser->primaryId, true);
Loading history...
111
        $localUser->alma_user_group = $almaUser->group;
0 ignored issues
show
Bug introduced by
The property alma_user_group does not seem to exist on App\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
112
        $localUser->lastname = $almaUser->lastName;
113
        $localUser->firstname = $almaUser->firstName;
114
        $localUser->email = $almaUser->email;
115
        $localUser->phone = $almaUser->phone;
116
        $localUser->lang = $almaUser->lang;
117
        $localUser->blocks = $almaUser->blocks;
118
        $localUser->fees = $almaUser->getFees();
119
        $localUser->save();
120
121
        // Set user identifiers
122
        $identifiers = [];
123
        foreach ($almaUser->getBarcodes() as $value) {
124
            $identifiers[] = ['type' => 'barcode', 'value' => $value];
125
        }
126
        foreach ($almaUser->getUniversityIds() as $value) {
127
            $identifiers[] = ['type' => 'university_id', 'value' => $value];
128
        }
129
        $localUser->setIdentifiers($identifiers, true);
130
131
        return true;
132
    }
133
134
    /**
135
     * Update or create local user from Alma user.
136
     *
137
     * @param User $almaUser
138
     * @return LocalUser|null
139
     */
140
    public function updateOrCreateLocalUserFromAlmaUser(User $almaUser)
141
    {
142
        $localUser = $this->findLocalUserFromAlmaUser($almaUser) ?? new LocalUser();
143
144
        $this->updateLocalUserFromAlmaUser($localUser, $almaUser);
145
146
        \Log::info(sprintf(
147
            'Importerte en bruker fra Alma (<a href="%s">Detaljer</a>)',
148
            action('UsersController@getShow', $localUser->id)
149
        ));
150
151
        return $localUser;
152
    }
153
}
154