Skip to content
This repository has been archived by the owner on Nov 28, 2017. It is now read-only.

Commit

Permalink
Codestyle and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Chekalskiy committed May 16, 2015
1 parent 0a67158 commit 2206d81
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions src/Reviewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Reviewer
*
* @var array
*/
public $countries = [ 'ru' => 'Russia', 'us' => 'US', 'ua' => 'Ukraine', 'by' => 'Belarus' ];
public $countries = ['ru' => 'Russia', 'us' => 'US', 'ua' => 'Ukraine', 'by' => 'Belarus'];

/**
* Application id
Expand Down Expand Up @@ -83,13 +83,14 @@ class Reviewer
/**
* Create Guzzle and Flintstone objects
*
* @param integer $appId application App Store id
* @param integer $appId application App Store id
* @param integer $maxPages max pages count to check
* @throws Exception when DB directory is not writable
*/
public function __construct($appId, $maxPages = 3)
{
$this->appId = intval($appId);
$this->maxPages = intval ($maxPages);
$this->maxPages = max(1, intval($maxPages));
$this->client = new Guzzle(['defaults' => ['timeout' => 20, 'connect_timeout' => 10]]);

$databaseDir = realpath(__DIR__ . '/..') . '/storage';
Expand All @@ -112,7 +113,7 @@ public function __construct($appId, $maxPages = 3)
/**
* Slack options setter
*
* @param array $slackSettings e.g. [ 'endpoint' => 'https://hook.slack.com/...', 'channel' => '#reviews' ]
* @param array $slackSettings e.g. [ 'endpoint' => 'https://hook.slack.com/...', 'channel' => '#reviews' ]
* @return TJ\Reviewer
*/
public function setSlackSettings($slackSettings)
Expand All @@ -125,50 +126,53 @@ public function setSlackSettings($slackSettings)
/**
* PSR-3 compatible logger setter
*
* @param LoggerInterface $logger
* @param LoggerInterface $logger
* @return TJ\Reviewer
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;

if ($this->initException && $this->logger) {
$this->logger->error('Reviewer: exception while init', [ 'exception' => $this->initException ]);
$this->logger->error('Reviewer: exception while init', ['exception' => $this->initException]);
$this->initException = null;
}

return $this;
}

/**
* Send requests and get reviews
* Get reviews from country App Store
*
* @return array list of reviews
* @param integer $appId
* @param string $countryCode
* @param string $countryName
* @return array list of reviews
*/
public function getReviewPages($countryCode, $countryName)
public function getReviewsByCountry($appId, $countryCode, $countryName)
{
$appId = $this->appId;
$reviews = [];

$requests = [];
for ($i=1; $i<=$this->maxPages; $i++) {
for ($i = 1; $i <= $this->maxPages; $i++) {
array_push($requests, $this->client->createRequest("GET", "https://itunes.apple.com/{$countryCode}/rss/customerreviews/page={$i}/id={$appId}/sortBy=mostRecent/json"));
}

try {
$responses = Pool::batch($this->client, $requests);

foreach ($responses->getSuccessful() as $page => $response) {
$realPage = $page + 1;
$reviewsData = $response->json();

if (!isset($reviewsData['feed']) || !isset($reviewsData['feed']['entry']) || count($reviewsData['feed']['entry']) == 0) {
// Received empty page
if ($this->logger) {
$this->logger->debug('Empty page received for page ' . ($page+1) . ' and country ' . $countryCode);
$this->logger->debug('Empty page received for page ' . $realPage . ' and country ' . $countryCode);
}
} else {
if ($this->logger) {
$this->logger->debug('Received ' . count($reviewsData['feed']['entry']) . ' entries for page ' . ($page+1) . ' country ' . $countryCode);
$this->logger->debug('Received ' . count($reviewsData['feed']['entry']) . ' entries for page ' . $realPage . ' in country ' . $countryCode);
}

$applicationData = [];
Expand All @@ -177,14 +181,16 @@ public function getReviewPages($countryCode, $countryName)
// First element is always an app metadata
$applicationData = [
'name' => $reviewEntry['im:name']['label'],
'image' => end($reviewEntry['im:image'])['label'],
'image' => end($reviewEntry['im:image'])['label']
];
continue;
}

$reviewId = intval($reviewEntry['id']['label']);
if ($this->storage->get("r{$reviewId}")) {
continue;
}

$review = [
'id' => $reviewId,
'author' => [
Expand All @@ -199,12 +205,12 @@ public function getReviewPages($countryCode, $countryName)
];

array_push($reviews, $review);
}
}
}
}
} catch (Exception $e) {
if ($this->logger) {
$this->logger->error('Reviewer: exception while getting reviews', [ 'exception' => $e ]);
$this->logger->error('Reviewer: exception while getting reviews', ['exception' => $e]);
}
}

Expand All @@ -219,24 +225,24 @@ public function getReviewPages($countryCode, $countryName)
*/
public function getReviews()
{

$appId = $this->appId;
$reviews = [];

foreach ($this->countries as $countryCode => $countryName) {
$reviewsByCountry = $this->getReviewsByCountry($appId, $countryCode, $countryName);

$reviewPage = $this->getReviewPages($countryCode, $countryName);
if ($reviewPage) {
$reviews = array_merge((array)$reviews, (array)$reviewPage);
if (is_array($reviewsByCountry) && count($reviewsByCountry)) {
$reviews = array_merge($reviews, $reviewsByCountry);
}

}

return $reviews;
}

/**
* Send reviews to Slack
*
* @param array $reviews list of reviews to send
*
* @param array $reviews list of reviews to send
* @return boolean successful sending
*/
public function sendReviews($reviews)
Expand Down Expand Up @@ -270,6 +276,7 @@ public function sendReviews($reviews)

try {
$slack = new Slack($this->slackSettings['endpoint']);

if ($this->firstTime === false) {
$slack->attach([
'fallback' => "{$ratingText} {$review['author']['name']}: {$review['title']}{$review['content']}",
Expand All @@ -287,7 +294,7 @@ public function sendReviews($reviews)
$this->storage->set("r{$review['id']}", 1);
} catch (Exception $e) {
if ($this->logger) {
$this->logger->error('Reviewer: exception while sending reviews', [ 'exception' => $e ]);
$this->logger->error('Reviewer: exception while sending reviews', ['exception' => $e]);
}
}
}
Expand All @@ -309,4 +316,4 @@ public function start()
$this->logger->debug('Sent ' . count($reviews) . ' reviews');
}
}
}
}

0 comments on commit 2206d81

Please sign in to comment.