Profile   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 86
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getId() 0 8 2
C getDetails() 0 34 13
1
<?php
2
/**
3
 * Profile.php
4
 *
5
 * @copyright	More in license.md
6
 * @license		http://www.ipublikuj.eu
7
 * @author		Adam Kadlec http://www.ipublikuj.eu
8
 * @package		iPublikuj:Flickr!
9
 * @subpackage	common
10
 * @since		5.0
11
 *
12
 * @date		20.02.15
13
 */
14
15
namespace IPub\Flickr;
16
17
use Nette;
18
use Nette\Utils;
19
20
use IPub;
21
use IPub\Flickr\Exceptions;
22
23
/**
24
 * Flickr's user profile
25
 *
26
 * @package		iPublikuj:Flickr!
27
 * @subpackage	common
28
 *
29
 * @author Adam Kadlec <[email protected]>
30
 */
31
class Profile extends Nette\Object
32
{
33
	/**
34
	 * @var Client
35
	 */
36
	private $flickr;
37
38
	/**
39
	 * @var string
40
	 */
41
	private $profileId;
42
43
	/**
44
	 * @var Utils\ArrayHash
45
	 */
46
	private $details;
47
48
	/**
49
	 * @param Client $flickr
50
	 * @param string $profileId
51
	 *
52
	 * @throws Exceptions\InvalidArgumentException
53
	 */
54
	public function __construct(Client $flickr, $profileId = NULL)
55
	{
56
		$this->flickr = $flickr;
57
58
		if (is_numeric($profileId)) {
59
			throw new Exceptions\InvalidArgumentException("ProfileId must be a username of the account you're trying to read or NULL, which means actually logged in user.");
60
		}
61
62
		$this->profileId = $profileId;
63
	}
64
65
	/**
66
	 * @return string
67
	 */
68
	public function getId()
69
	{
70
		if ($this->profileId === NULL) {
71
			return $this->flickr->getUser();
72
		}
73
74
		return $this->profileId;
75
	}
76
77
	/**
78
	 * @param string $key
79
	 *
80
	 * @return Utils\ArrayHash|NULL
81
	 */
82
	public function getDetails($key = NULL)
83
	{
84
		if ($this->details === NULL) {
85
			try {
86
87
				if ($this->profileId !== NULL) {
88
					if (($user = $this->flickr->get('flickr.people.findByUsername', ['username' => $this->profileId]))
89
						&& ($user instanceof Utils\ArrayHash)
90
						&& ($result = $this->flickr->get('flickr.people.getInfo', ['user_id' => $user->user->id]))
91
						&& ($result instanceof Utils\ArrayHash)
92
					) {
93
						$this->details = $result->person;
94
					}
95
96
				} else if ($user = $this->flickr->getUser()) {
97
					if (($result = $this->flickr->get('flickr.people.getInfo', ['user_id' => $user])) && ($result instanceof Utils\ArrayHash)) {
98
						$this->details = $result->person;
99
					}
100
101
				} else {
102
					$this->details = new Utils\ArrayHash;
103
				}
104
105
			} catch (\Exception $e) {
106
				// todo: log?
107
			}
108
		}
109
110
		if ($key !== NULL) {
111
			return isset($this->details[$key]) ? $this->details[$key] : NULL;
112
		}
113
114
		return $this->details;
115
	}
116
}