Skip to content

Commit

Permalink
Update OAuth1 keys to correct defect
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmaguire committed Nov 23, 2015
1 parent 3375db9 commit 17c9bdd
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 12 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,23 @@
# Changelog
All Notable changes to `yelp-php` will be documented in this file

## 1.4.2 - 2015-11-23

### Added
- Nothing

### Deprecated
- Nothing

### Fixed
- OAuth1 subscriber configuration issue. (https://github.com/stevenmaguire/yelp-php/issues/9)

### Removed
- Nothing

### Security
- Nothing

## 1.4.1 - 2015-11-17

### Added
Expand Down
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -23,11 +23,11 @@ $ composer require stevenmaguire/yelp-php

```php
$client = new Stevenmaguire\Yelp\Client(array(
'consumer_key' => 'YOUR COSUMER KEY',
'consumer_secret' => 'YOUR CONSUMER SECRET',
'consumerKey' => 'YOUR COSUMER KEY',
'consumerSecret' => 'YOUR CONSUMER SECRET',
'token' => 'YOUR TOKEN',
'token_secret' => 'YOUR TOKEN SECRET',
'api_host' => 'api.yelp.com' // Optional, default 'api.yelp.com'
'tokenSecret' => 'YOUR TOKEN SECRET',
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
));
```

Expand Down
73 changes: 69 additions & 4 deletions src/Client.php
Expand Up @@ -100,11 +100,13 @@ public function __construct($configuration = [])
{
$this->parseConfiguration($configuration);

/*
$this->consumerKey = $configuration['consumerKey'];
$this->consumerSecret = $configuration['consumerSecret'];
$this->token = $configuration['token'];
$this->tokenSecret = $configuration['tokenSecret'];
$this->apiHost = $configuration['apiHost'];
*/
$this->createHttpClient();
}

Expand Down Expand Up @@ -150,10 +152,10 @@ protected function createHttpClient()
$stack = HandlerStack::create();

$middleware = new Oauth1([
'consumerKey' => $this->consumerKey,
'consumerSecret' => $this->consumerSecret,
'consumer_key' => $this->consumerKey,
'consumer_secret' => $this->consumerSecret,
'token' => $this->token,
'tokenSecret' => $this->tokenSecret
'token_secret' => $this->tokenSecret
]);

$stack->push($middleware);
Expand All @@ -179,6 +181,31 @@ public function getBusiness($businessId)
return $this->request($businessPath);
}

/**
* Maps legacy configuration keys to updated keys.
*
* @param array $configuration
*
* @return array
*/
protected function mapConfiguration(array $configuration)
{
$map = [
'consumer_key' => 'consumerKey',
'consumer_secret' => 'consumerSecret',
'token_secret' => 'tokenSecret',
'api_host' => 'apiHost',
];

array_walk($map, function ($value, $key) use (&$configuration) {
if (isset($configuration[$key])) {
$configuration[$value] = $configuration[$key];
}
});

return $configuration;
}

/**
* Parse configuration using defaults
*
Expand All @@ -196,7 +223,12 @@ protected function parseConfiguration(&$configuration = [])
'apiHost' => 'api.yelp.com'
);

$configuration = array_merge($defaults, $configuration);
$configuration = array_merge(
$defaults,
$this->mapConfiguration($configuration)
);

array_walk($configuration, [$this, 'setConfig']);
}

/**
Expand Down Expand Up @@ -253,6 +285,23 @@ public function searchByPhone($attributes = [])
return $this->request($searchPath);
}

/**
* Attempts to set a given value.
*
* @param mixed $value
* @param string $key
*
* @return Client
*/
protected function setConfig($value, $key)
{
if (property_exists($this, $key)) {
$this->$key = $value;
}

return $this;
}

/**
* Set default location
*
Expand Down Expand Up @@ -307,4 +356,20 @@ public function setSearchLimit($limit)
}
return $this;
}

/**
* Retrives the value of a given property from the client.
*
* @param string $property
*
* @return mixed|null
*/
public function __get($property)
{
if (property_exists($this, $property)) {
return $this->$property;
}

return null;
}
}
27 changes: 23 additions & 4 deletions tests/YelpTest.php
Expand Up @@ -15,11 +15,11 @@ class YelpTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->client = new Yelp([
'consumer_key' => getenv('YELP_CONSUMER_KEY'),
'consumer_secret' => getenv('YELP_CONSUMER_SECRET'),
'consumerKey' => getenv('YELP_CONSUMER_KEY'),
'consumerSecret' => getenv('YELP_CONSUMER_SECRET'),
'token' => getenv('YELP_ACCESS_TOKEN'),
'token_secret' => getenv('YELP_ACCESS_TOKEN_SECRET'),
'api_host' => 'api.yelp.com'
'tokenSecret' => getenv('YELP_ACCESS_TOKEN_SECRET'),
'apiHost' => 'api.yelp.com'
]);
}

Expand Down Expand Up @@ -50,6 +50,25 @@ protected function getHttpClient($path, $status = 200, $payload = null)
return $client;
}

public function testConfigurationMapper()
{
$config = [
'consumer_key' => uniqid(),
'consumer_secret' => uniqid(),
'token' => uniqid(),
'token_secret' => uniqid(),
'api_host' => uniqid()
];

$client = new Yelp($config);

$this->assertEquals($config['consumer_key'], $client->consumerKey);
$this->assertEquals($config['consumer_secret'], $client->consumerSecret);
$this->assertEquals($config['token'], $client->token);
$this->assertEquals($config['token_secret'], $client->tokenSecret);
$this->assertEquals($config['api_host'], $client->apiHost);
}

/**
* @expectedException Stevenmaguire\Yelp\Exception
*/
Expand Down

0 comments on commit 17c9bdd

Please sign in to comment.