Permalink
Browse files

[TECH DEBT] Removing Whitespace and removing redundant tracker proper…

…ty getting / setting code
  • Loading branch information...
matthewfitz committed Jun 11, 2012
1 parent c2c9800 commit 58fc1bcb852ea9395e435b32aca2d10d6d0a42fb
Showing with 47 additions and 34 deletions.
  1. +39 −34 Analytics.php
  2. +8 −0 Tests/AnalyticsWebTest.php
View
@@ -33,7 +33,7 @@ public function __construct(ContainerInterface $container,
$this->whitelist = $whitelist;
$this->api_key = isset($dashboard['api_key']) ? $dashboard['api_key'] : '';
$this->client_id = isset($dashboard['client_id']) ? $dashboard['client_id'] : '';
- $this->table_id = isset($dashboard['table_id']) ? $dashboard['table_id'] : '';
+ $this->table_id = isset($dashboard['table_id']) ? $dashboard['table_id'] : '';
}
public function excludeBaseUrl()
@@ -46,32 +46,51 @@ public function includeBaseUrl()
$this->pageViewsWithBaseUrl = true;
}
+ private function isValidConfigKey($trackerKey)
+ {
+ if (!array_key_exists($trackerKey, $this->trackers)) {
+ throw new \InvalidArgumentException(sprintf('There is no tracker configuration assigned with the key "%s".', $trackerKey));
+ }
+ return true;
+ }
+
+ private function setTrackerProperty($tracker, $property, $value)
+ {
+ if ($this->isValidConfigKey($tracker)) {
+ $this->trackers[$tracker][$property] = $value;
+ }
+ }
+
+ private function getTrackerProperty($tracker, $property)
+ {
+ if (!$this->isValidConfigKey($tracker)) {
+ return;
+ }
+
+ if (array_key_exists($property, $this->trackers[$tracker])) {
+ return $this->trackers[$tracker][$property];
+ }
+ }
+
/**
* @param string $trackerKey
* @param boolean $allowHash
*/
public function setAllowHash($trackerKey, $allowHash)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return;
- }
- $this->trackers[$trackerKey]['allowHash'] = $allowHash;
+ $this->setTrackerProperty($trackerKey, 'allowHash', $allowHash);
}
/**
* @param string $trackerKey
- * @return boolean $allowHash
+ * @return boolean $allowHash (default:false)
*/
public function getAllowHash($trackerKey)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return false;
- }
- $trackerConfig = $this->trackers[$trackerKey];
- if (!array_key_exists('allowHash', $trackerConfig)) {
+ if (null === ($property = $this->getTrackerProperty($trackerKey, 'allowHash'))) {
return false;
}
- return $trackerConfig['allowHash'];
+ return $property;
}
/**
@@ -80,26 +99,19 @@ public function getAllowHash($trackerKey)
*/
public function setAllowLinker($trackerKey, $allowLinker)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return;
- }
- $this->trackers[$trackerKey]['allowLinker'] = $allowLinker;
+ $this->setTrackerProperty($trackerKey, 'allowLinker', $allowLinker);
}
/**
* @param string $trackerKey
- * @return boolean $allowLinker
+ * @return boolean $allowLinker (default:true)
*/
public function getAllowLinker($trackerKey)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
+ if (null === ($property = $this->getTrackerProperty($trackerKey, 'allowLinker'))) {
return true;
}
- $trackerConfig = $this->trackers[$trackerKey];
- if (!array_key_exists('allowLinker', $trackerConfig)) {
- return true;
- }
- return $trackerConfig['allowLinker'];
+ return $property;
}
/**
@@ -108,26 +120,19 @@ public function getAllowLinker($trackerKey)
*/
public function setTrackPageLoadTime($trackerKey, $trackPageLoadTime)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return;
- }
- $this->trackers[$trackerKey]['trackPageLoadTime'] = $trackPageLoadTime;
+ $this->setTrackerProperty($trackerKey, 'trackPageLoadTime', $trackPageLoadTime);
}
/**
* @param string $trackerKey
- * @return boolean $trackPageLoadTime
+ * @return boolean $trackPageLoadTime (default:false)
*/
public function getTrackPageLoadTime($trackerKey)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return false;
- }
- $trackerConfig = $this->trackers[$trackerKey];
- if (!array_key_exists('trackPageLoadTime', $trackerConfig)) {
+ if (null === ($property = $this->getTrackerProperty($trackerKey, 'trackPageLoadTime'))) {
return false;
}
- return $trackerConfig['trackPageLoadTime'];
+ return $property;
}
/**
@@ -40,6 +40,14 @@ public function testConstructor()
$this->assertFalse($this->analytics->getAllowHash('default'));
}
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testExpectedInvalidArgumentException()
+ {
+ $this->analytics->getAllowLinker('not-a-tracker');
+ }
+
public function testSetGetCustomPageView()
{
$customPageView = '/profile/mattfitz';

0 comments on commit 58fc1bc

Please sign in to comment.