Passed
Push — master ( 12989d...3ff895 )
by Dan Michael O.
01:53
created

AlmaUsers::hasKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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;
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...
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);
111
        $localUser->alma_user_group = $almaUser->group;
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)
0 ignored issues
show
Bug introduced by
The function action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

148
            /** @scrutinizer ignore-call */ 
149
            action('UsersController@getShow', $localUser->id)
Loading history...
149
        ));
150
151
        return $localUser;
152
    }
153
}
154