OpauthResponseHelper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_first_name() 0 4 1
A get_last_name() 0 5 1
A get_twitter_locale() 0 4 1
A get_google_locale() 0 7 2
B get_smart_locale() 0 23 4
A parse_source_path() 0 11 3
1
<?php
2
3
/**
4
 * OpauthResponseHelper
5
 * Utility class for handling responses from Opauth.
6
 * Used in parsing. Can and should be referenced in _config.yml for helping
7
 * parse things for member_mapping.
8
 * @author Will Morgan <@willmorgan>
9
 * @copyright Copyright (c) 2013, Better Brief LLP
10
 */
11
class OpauthResponseHelper {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
13
	/**
14
	 * Take the first part of the name
15
	 * @return string
16
	 */
17
	public static function get_first_name($source) {
18
		$name = explode(' ', self::parse_source_path('info.name', $source));
19
		return array_shift($name);
20
	}
21
22
	/**
23
	 * Take all but the first part of the name
24
	 * @return string
25
	 */
26
	public static function get_last_name($source) {
27
		$name = explode(' ', self::parse_source_path('info.name', $source));
28
		array_shift($name);
29
		return join(' ', $name);
30
	}
31
32
	/**
33
	 * Twitter responds with just a language (also a TZ, but unused for now)
34
	 * If the PECL Locale extension is used it may be possible to combine both
35
	 * the TZ and the language to fine tune a user's location, but a bit OTT.
36
	 * @return string
37
	 */
38
	public static function get_twitter_locale($source) {
39
		$language = self::parse_source_path('raw.lang', $source);
40
		return self::get_smart_locale($language);
41
	}
42
43
	/**
44
	 * Google responds near perfectly for locales, if populated.
45
	 * Fallback otherwise.
46
	 * @return string
47
	 */
48
	public static function get_google_locale($source) {
49
		$locale = self::parse_source_path('raw.locale', $source);
50
		if(!$locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
51
			return self::get_smart_locale();
52
		}
53
		return str_replace('-', '_', $locale);
54
	}
55
56
	/**
57
	 * Try very hard to get a locale for this user. Helps for i18n etc.
58
	 * @return string
59
	 */
60
	public static function get_smart_locale($language = null) {
61
62
		require_once FRAMEWORK_PATH . '/thirdparty/Zend/Locale.php';
63
		$locale = Zend_Locale::getBrowser();
64
65
		if(!$locale) {
66
			if($language) {
67
				return i18n::get_locale_from_lang($language);
68
			}
69
			else {
70
				return i18n::get_locale();
71
			}
72
		}
73
74
		$locale = array_keys($locale);
75
		$firstPref = array_shift($locale);
76
77
		if(strpos($firstPref, '_') === false) {
78
			return i18n::get_locale_from_lang($language);
79
		}
80
81
		return $firstPref;
82
	}
83
84
	/**
85
	 * Dot notation parser. Looks for an index or fails gracefully if not found.
86
	 * @param string $path The path, dot notated.
87
	 * @param array $source The source in which to search.
88
	 * @return string|null
89
	 */
90
	public static function parse_source_path($path, $source) {
91
		$fragments = explode('.', $path);
92
		$currentFrame = $source;
93
		foreach($fragments as $fragment) {
94
			if(!isset($currentFrame[$fragment])) {
95
				return null;
96
			}
97
			$currentFrame = $currentFrame[$fragment];
98
		}
99
		return $currentFrame;
100
	}
101
102
}
103