Skip to content

Commit

Permalink
[TECH DEBT] Removing Whitespace and removing redundant tracker proper…
Browse files Browse the repository at this point in the history
…ty getting / setting code
  • Loading branch information
matthewfitz committed Jun 11, 2012
1 parent c2c9800 commit 58fc1bc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
73 changes: 39 additions & 34 deletions Analytics.php
Expand Up @@ -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()
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions Tests/AnalyticsWebTest.php
Expand Up @@ -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';
Expand Down

0 comments on commit 58fc1bc

Please sign in to comment.