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

Commit

Permalink
Apply redis commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Chekalskiy committed Jan 3, 2016
1 parent 20d9a7a commit 40b69dc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ Simple library to track App Store reviews with [Slack](https://slack.com/).
composer.phar require tjournal/reviewer
```

After installing, you should make database directory `storage` writable:

```bash
chmod 777 storage
```

and require Composer's autoloader:
Next require Composer's autoloader:

```php
require 'vendor/autoload.php';
```

### Simple usage
You should use external database to store already sent reviews. We advice Redis with [Predis](https://github.com/nrk/predis) library. Library should implement `IStorage` interface.

You need to [create new Incoming webhook](https://slack.com/services/new/incoming-webhook) in Slack and change `{APPID}` with [the real app id](https://www.codeproof.com/blog/how-to-find-aitunes-store-id-or-appid/):

```php
try {
$storage = new Predis\Client();

$reviewer = new TJ\Reviewer({APPID});
$reviewer->setStorage($storage);
$reviewer->setSlackSettings(['endpoint' => 'https://hooks.slack.com/services/ABCDE/QWERTY', 'channel' => '#reviews']);
$reviewer->start();
} catch (Exception $e) {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"psr/log": "~1.0"
},
"suggest": {
"monolog/monolog": "Monolog sends your logs to files, sockets, inboxes, databases and various web services"
"monolog/monolog": "Monolog sends your logs to files, sockets, inboxes, databases and various web services",
"predis/predis": "Predis is a flexible and feature-complete Redis client library for PHP"
},
"autoload": {
"psr-4": {"TJ\\": "src/"}
Expand Down
9 changes: 9 additions & 0 deletions src/IStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace TJ;

interface IStorage {
public function sadd();
public function sismember();
public function set();
public function exists();
}
32 changes: 22 additions & 10 deletions src/Reviewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ class Reviewer
*/
protected $maxPages;

/**
* Is sending fired for a first time
*
* @var boolean
*/
protected $firstTime = false;

/**
* List of Slack settings
*
Expand Down Expand Up @@ -83,8 +76,19 @@ public function __construct($appId, $maxPages = 3)
$this->appId = intval($appId);
$this->maxPages = max(1, intval($maxPages));
$this->client = new Guzzle(['defaults' => ['timeout' => 20, 'connect_timeout' => 10]]);
}

/**
* Storage setter
*
* @param object $storage implements IStorage
* @return TJ\Reviewer
*/
public function setStorage(IStorage $storage)
{
$this->storage = $storage;

return $this;
}

/**
Expand Down Expand Up @@ -160,7 +164,7 @@ public function getReviewsByCountry($appId, $countryCode, $countryName)
}

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

Expand Down Expand Up @@ -232,6 +236,8 @@ public function sendReviews($reviews)
return false;
}

$firstTime = !((boolean) $this->storage->exists('tjournal:reviewer:init'));

$config = [
'username' => 'TJ Reviewer',
'icon' => 'https://i.imgur.com/GX1ASZy.png'
Expand All @@ -250,7 +256,7 @@ public function sendReviews($reviews)
}

try {
if ($this->firstTime === false) {
if (!$firstTime) {
$slack->attach([
'fallback' => "{$ratingText} {$review['title']}{$review['content']}",
'author_name' => $review['application']['name'],
Expand Down Expand Up @@ -286,14 +292,16 @@ public function sendReviews($reviews)
])->send();
}

$this->storage->set("r{$review['id']}", 1);
$this->storage->sadd("tjournal:reviewer:reviews", $review['id']);
} catch (Exception $e) {
if ($this->logger) {
$this->logger->error('Reviewer: exception while sending reviews', ['exception' => $e]);
}
}
}

$this->storage->set('tjournal:reviewer:init', 1);

return true;
}

Expand All @@ -304,6 +312,10 @@ public function sendReviews($reviews)
*/
public function start()
{
if (!$this->storage) {
throw new Exception("Storage is not set");
}

$reviews = $this->getReviews();
$this->sendReviews($reviews);

Expand Down

0 comments on commit 40b69dc

Please sign in to comment.