Passed
Push — master ( f6b34e...90b0c1 )
by Dan Michael O.
03:15
created

AlmaUsers::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
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 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
8
class AlmaUsers
9
{
10
    /**
11
     * @var AlmaClient
12
     */
13
    protected $alma;
14
15
    /**
16
     * AlmaUsers constructor.
17
     * @param AlmaClient $alma
18
     */
19
    public function __construct(AlmaClient $alma)
20
    {
21
        $this->alma = $alma;
22
    }
23
24
    /**
25
     * Find Alma user by some ID.
26
     *
27
     * @param string $userId
28
     * @return User|null
29
     */
30
    public function findById(?string $userId)
31
    {
32
        return User::lookup($this->alma, $userId);
33
    }
34
35
    /**
36
     * Find Alma user from local user.
37
     *
38
     * @param LocalUser $user
39
     * @return User|null
40
     */
41
    public function findFromLocalUser(LocalUser $user)
42
    {
43
        return $this->findById($user->university_id) ?? $this->findById($user->barcode);
44
    }
45
46
    /**
47
     * Find local User from Alma User.
48
     *
49
     * @param User $almaUser
50
     * @return LocalUser|null
51
     */
52
    public function findLocalUserFromAlmaUser(User $almaUser)
53
    {
54
        $builder = LocalUser::query();
55
        if (!is_null($almaUser->primaryId)) {
56
            $builder->orWhere('alma_primary_id', '=', $almaUser->primaryId);
57
        }
58
        if (!is_null($almaUser->getBarcode())) {
59
            $builder->orWhere('barcode', '=', $almaUser->getBarcode());
60
        }
61
        if (!is_null($almaUser->getUniversityId())) {
62
            $builder->orWhere('university_id', '=', $almaUser->getUniversityId());
63
        }
64
65
        if (count($builder->getQuery()->wheres)) {
66
            return $builder->first();
67
        }
68
69
        return null;
70
    }
71
72
    /**
73
     * Update the local user object with user data from Alma.
74
     * Returns true if successful, false if the user could no longer be found in Alma.
75
     *
76
     * @param LocalUser $localUser
77
     * @param User|null $almaUser
78
     * @return bool
79
     */
80
    public function updateLocalUserFromAlmaUser(LocalUser $localUser, User $almaUser = null)
81
    {
82
        if (is_null($almaUser)) {
83
            $almaUser = $this->findFromLocalUser($localUser);
84
        }
85
86
        if (is_null($almaUser)) {
87
            $localUser->in_alma = false;
88
            return false;
89
        }
90
91
        $localUser->in_alma = true;
92
93
        $localUser->setUniqueValue('alma_primary_id', $almaUser->primaryId);
94
        $localUser->setUniqueValue('barcode', $almaUser->getBarcode());
95
        $localUser->setUniqueValue('university_id', $almaUser->getUniversityId());
96
97
        $localUser->alma_user_group = $almaUser->group;
98
        $localUser->lastname = $almaUser->lastName;
99
        $localUser->firstname = $almaUser->firstName;
100
        $localUser->email = $almaUser->email;
101
        $localUser->phone = $almaUser->phone;
102
        $localUser->lang = $almaUser->lang;
103
        $localUser->blocks = $almaUser->blocks;
104
        $localUser->fees = $almaUser->getFees();
105
106
        return true;
107
    }
108
109
    /**
110
     * Update or create local user from Alma user.
111
     *
112
     * @param User $almaUser
113
     * @return LocalUser|null
114
     */
115
    public function updateOrCreateLocalUserFromAlmaUser(User $almaUser)
116
    {
117
        $localUser = $this->findLocalUserFromAlmaUser($almaUser) ?? new LocalUser();
118
119
        $this->updateLocalUserFromAlmaUser($localUser, $almaUser);
120
121
        $localUser->save();
122
123
        \Log::info(sprintf(
124
            'Importerte en bruker fra Alma (<a href="%s">Detaljer</a>)',
125
            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

125
            /** @scrutinizer ignore-call */ 
126
            action('UsersController@getShow', $localUser->id)
Loading history...
126
        ));
127
128
        return $localUser;
129
    }
130
}
131