Profile   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 82
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getId() 0 8 2
C getDetails() 0 30 11
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:Twitter!
9
 * @subpackage	common
10
 * @since		5.0
11
 *
12
 * @date		04.03.15
13
 */
14
15
namespace IPub\Twitter;
16
17
use Nette;
18
use Nette\Utils;
19
20
use IPub;
21
use IPub\Twitter\Exceptions;
22
23
/**
24
 * Twitter's user profile
25
 *
26
 * @package		iPublikuj:Twitter!
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 $twitter;
37
38
	/**
39
	 * @var string
40
	 */
41
	private $profileId;
42
43
	/**
44
	 * @var Utils\ArrayHash
45
	 */
46
	private $details;
47
48
	/**
49
	 * @param Client $twitter
50
	 * @param string $profileId
51
	 *
52
	 * @throws Exceptions\InvalidArgumentException
53
	 */
54
	public function __construct(Client $twitter, $profileId = NULL)
55
	{
56
		$this->twitter = $twitter;
57
58
		if (is_numeric($profileId)) {
59
			throw new Exceptions\InvalidArgumentException("ProfileId must be a screen name 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->twitter->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 (($result = $this->twitter->get('users/show.json', ['screen_name' => $this->profileId])) && ($result instanceof Utils\ArrayHash)) {
89
						$this->details = $result;
90
					}
91
92
				} else if ($user = $this->twitter->getUser()) {
93
					if (($result = $this->twitter->get('users/show.json', ['user_id' => $user])) && ($result instanceof Utils\ArrayHash)) {
94
						$this->details = $result;
95
					}
96
97
				} else {
98
					$this->details = new Utils\ArrayHash;
99
				}
100
101
			} catch (\Exception $e) {
102
				// todo: log?
103
			}
104
		}
105
106
		if ($key !== NULL) {
107
			return isset($this->details[$key]) ? $this->details[$key] : NULL;
108
		}
109
110
		return $this->details;
111
	}
112
}