PersonDataService   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 70
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getPersonData() 0 60 8
1
<?php
2
3
namespace Kuleuven\AuthenticationBundle\Service;
4
5
class PersonDataService
6
{
7
    protected $url;
8
9
    public function __construct($url = 'https://webwsp.aps.kuleuven.be/esap/public/odata/sap/zh_person_srv/Persons(\'%s\')?$format=json&$expand=WorkAddresses')
10
    {
11
        $this->url = $url;
12
    }
13
14
    public function getPersonData($uid)
15
    {
16
        $url = sprintf($this->url, str_replace('u', '0', $uid));
17
        $ch = curl_init();
18
        curl_setopt($ch, CURLOPT_URL, $url);
19
        curl_setopt($ch, CURLOPT_HEADER, false);
20
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
21
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
22
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
23
        $output = curl_exec($ch);
24
        $error = curl_error($ch);
25
        curl_close($ch);
26
        if (false === $output) {
27
            throw new \Exception('Could not get person data for ' . $uid . ': ' . $error);
28
        }
29
        $decodedOutput = json_decode($output);
30
        if (null === $decodedOutput) {
31
            throw new \Exception('Could not get person data for ' . $uid . ': ' . json_last_error_msg());
32
        }
33
        if (0 === count($decodedOutput->d)) {
34
            throw new \Exception('Could not get person data for ' . $uid . ': ' . json_encode($decodedOutput));
35
        }
36
        $user = $decodedOutput->d;
37
        $mainWorkAddresses = array_filter($user->WorkAddresses->results, function (\stdClass $value) {
38
            return $value->isMainWorkAddress;
39
        });
40
        $mainWorkAddress = reset($mainWorkAddresses);
41
        if (!empty($mainWorkAddress)) {
42
            $workAddress = [
43
                'phone'      => $mainWorkAddress->phone,
44
                'label'      => $mainWorkAddress->buildingDescription,
45
                'street'     => $mainWorkAddress->street,
46
                'number'     => $mainWorkAddress->houseNr,
47
                'postalCode' => $mainWorkAddress->postalCode,
48
                'city'       => $mainWorkAddress->city,
49
                'province'   => null,
50
                'country'    => null,
51
            ];
52
        } else {
53
            $workAddress = null;
54
        }
55
        $userData = [
56
            'username'               => $uid,
57
            'primaryEmail'           => $user->preferredMailAddress,
58
            'firstName'              => !empty($user->firstName) ? $user->firstName : $user->preferredName,
59
            'lastName'               => $user->surname,
60
            'title'                  => !empty($user->title) ? $user->title : null,
61
            'sexe'                   => $user->gender,
62
            'birthDate'              => null,
63
            'birthLocation'          => null,
64
            'birthCountry'           => null,
65
            'nationality'            => null,
66
            'correspondenceLanguage' => null,
67
            'picture'                => !empty($user->pictureUrl) ? $user->pictureUrl : null,
68
            'privateEmail'           => null,
69
            'mobilePhoneNumber'      => $user->mobilePhone,
70
            'workAddress'            => $workAddress,
71
        ];
72
        return $userData;
73
    }
74
}
75