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

Commit

Permalink
Merge pull request #5 from tjournal/universal-storage
Browse files Browse the repository at this point in the history
Redis storage
  • Loading branch information
Ilya Chekalskiy committed Jan 3, 2016
2 parents cfafb90 + 40b69dc commit 54faaa2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 51 deletions.
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"php": ">=5.4.0",
"guzzlehttp/guzzle": "~6.0",
"maknz/slack": "~1.7",
"fire015/flintstone": "~1.9",
"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();
}
66 changes: 25 additions & 41 deletions src/Reviewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
namespace TJ;

use Exception;
use Flintstone\Flintstone;
use Flintstone\FlintstoneException;
use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Promise;
use Maknz\Slack\Client as Slack;
Expand Down Expand Up @@ -35,20 +33,6 @@ class Reviewer
*/
protected $maxPages;

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

/**
* Exception caught during the initialization
*
* @var Exception
*/
protected $initException;

/**
* List of Slack settings
*
Expand All @@ -74,14 +58,14 @@ class Reviewer
protected $logger;

/**
* Flintstone instance
* Storage instance
*
* @var Flintstone
* @var Object
*/
protected $storage;

/**
* Create Guzzle and Flintstone objects
* Create Guzzle objects
*
* @param integer $appId application App Store id
* @param integer $maxPages max pages count to check
Expand All @@ -92,22 +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]]);
}

$databaseDir = realpath(__DIR__ . '/..') . '/storage';

if (!realpath($databaseDir) || !is_dir($databaseDir) || !is_writable($databaseDir)) {
throw new Exception("Please make '{$databaseDir}' dir writable");
}

if (!file_exists($databaseDir . '/reviews.dat')) {
$this->firstTime = true;
}
/**
* Storage setter
*
* @param object $storage implements IStorage
* @return TJ\Reviewer
*/
public function setStorage(IStorage $storage)
{
$this->storage = $storage;

try {
$this->storage = Flintstone::load('reviews', ['dir' => $databaseDir]);
} catch (FlintstoneException $e) {
$this->initException = $e;
}
return $this;
}

/**
Expand All @@ -133,11 +114,6 @@ public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;

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

return $this;
}

Expand Down Expand Up @@ -188,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 @@ -260,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 @@ -278,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 @@ -314,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 @@ -332,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
Empty file removed storage/.gitkeep
Empty file.

0 comments on commit 54faaa2

Please sign in to comment.