Passed
Push — develop ( 28c299...928e2a )
by Mathias
12:40
created

LinkedIn::parseDate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 6
cp 0
rs 10
cc 3
nc 4
nop 1
crap 12
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** LinkedIn.php */
11
namespace Auth\Controller\Plugin\SocialProfiles;
12
13
use Hybrid_Provider_Adapter;
14
15
class LinkedIn extends AbstractAdapter
16
{
17
    /**
18
     * {@inheritDoc}
19
     * @see \Auth\Controller\Plugin\SocialProfiles\AbstractAdapter::initFetch()
20
     */
21
    public function init($api, Hybrid_Provider_Adapter $hauthAdapter)
22
    {
23
        $api->curl_header = [
24
            "Authorization: Bearer {$hauthAdapter->getAccessToken()['access_token']}"
25
        ];
26
    }
27
    
28
    protected function queryApi($api)
29
    {
30
        /** @var \OAuth2Client $api */
31
        $result = $api->get('people/~:(id,first-name,last-name,location,industry,public-profile-url,picture-url,email-address,date-of-birth,phone-numbers,summary,positions,educations,languages,last-modified-timestamp)', [], false);
32
        $xml = @simplexml_load_string($result);
33
        
34
        if (false === $xml) {
35
            return false;
36
        }
37
        
38
        $data = $this->getDataArray($xml);
39
       
40
        return $data;
41
    }
42
    
43
44
    protected function getDataArray($xmlElement)
45
    {
46
        $return = array();
47
        
48
        foreach ($xmlElement->children() as $child) {
49
            $name = $child->getName();
50
            
51
            switch ($name) {
52
                default:
53
                    if (count($child->children())) {
54
                        $return[$name][] = $this->getDataArray($child);
55
                    } else {
56
                        $return[$name] = strval($child);
57
                    }
58
                    break;
59
                    
60
                case "location":
61
                    $return['location'] = array(
62
                        'name' => strval($child->name),
63
                        'country' => strval($child->country->code)
64
                    );
65
                    break;
66
                    
67
                case "relation-to-viewer":
68
                    $return['relation-to-viewer'] = strval($child->distance);
69
                    break;
70
                    
71
                case "connections":
72
                    $return['connections'] = $child['total'];
73
                    break;
74
                    
75
                case "positions":
76
                    $return['positions'] = $this->parsePositions($child);
77
                    break;
78
                    
79
                case "educations":
80
                    $return['educations'] = $this->parseEducations($child);
81
                    break;
82
            }
83
        }
84
        
85
        return $return;
86
    }
87
    
88
    protected function parsePositions($xml)
89
    {
90
        $positions = array();
91
        foreach ($xml->children() as $pos) {
92
            if ('position' != $pos->getName()) {
93
                continue;
94
            }
95
            
96
            $position = array();
97
            foreach ($pos->children() as $p) {
98
                $n = $p->getName();
99
                switch ($n) {
100
                    default:
101
                        $position[$n] = strval($p);
102
                        break;
103
                        
104
                    case 'start-date':
105
                    case 'end-date':
106
                        $position[$n] = $this->parseDate($p);
107
                        break;
108
                        
109
                    case 'company':
110
                        $position[$n] = strval($p->name);
111
                        break;
112
                }
113
            }
114
            $positions[] = $position;
115
        }
116
        
117
        return $positions;
118
    }
119
    
120
    protected function parseEducations($xml)
121
    {
122
        $educations = array();
123
        foreach ($xml->children() as $edu) {
124
            if ('education' != $edu->getName()) {
125
                continue;
126
            }
127
128
            $education = array();
129
            foreach ($edu->children() as $e) {
130
                $n = $e->getName();
131
                switch ($n) {
132
                    default:
133
                        $education[$n] = strval($e);
134
                        break;
135
136
                    case 'start-date':
137
                    case 'end-date':
138
                        $education[$n] = $this->parseDate($e);
139
                        break;
140
                }
141
            }
142
            $educations[] = $education;
143
        }
144
    
145
        return $educations;
146
    }
147
    
148
    protected function parseDate($xml)
149
    {
150
        return array(
151
            'year' => strval($xml->year),
152
            'month'=> isset($xml->month) ? strval($xml->month) : '01',
153
            'day'  => isset($xml->day)   ? strval($xml->day)   : '01',
154
        );
155
    }
156
}
157