Skip to content

Commit

Permalink
refactor(location): Relocate twitter client in twitter lib
Browse files Browse the repository at this point in the history
This commit adds classes previously located in twitter-stream in this lib
  • Loading branch information
remi-san committed Jul 8, 2017
1 parent d83f326 commit a019f96
Show file tree
Hide file tree
Showing 37 changed files with 3,599 additions and 6 deletions.
111 changes: 111 additions & 0 deletions bin/twitter
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env php
<?php
use Symfony\Component\Console\Application;
use Twitter\API\REST\Factory\CodebirdFactory;
use Twitter\API\REST\TwitterApiGateway;
use Twitter\Console\AuthenticationCommand;

function autoload()
{
$autoloadFiles = [
__DIR__.'/../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php'
];

foreach ($autoloadFiles as $autoloadFile) {
if (file_exists($autoloadFile)) {
require_once $autoloadFile;
}
}
}

/**
* @return null|string
*/
function getConfig()
{
$directories = [
getcwd(),
getcwd() . DIRECTORY_SEPARATOR . 'config',
__DIR__ . '/..',
__DIR__ . '/../config',
__DIR__ . '/../..',
__DIR__ . '/../../config',
__DIR__ . '/../../../..',
__DIR__ . '/../../../../config'
];

$configFile = null;
foreach ($directories as $directory) {
$configFile = $directory . DIRECTORY_SEPARATOR . 'twitter-cli-config.php';

if (file_exists($configFile)) {
break;
}
}

if ($configFile === null || ! file_exists($configFile)) {
showHelp();
exit(1);
}

if (! is_readable($configFile)) {
showReadErrorMessage($configFile);
exit(1);
}

$config = require $configFile;

if (!is_array($config) ||
! isset($config['key']) ||
! isset($config['secret'])
) {
echo 'Configuration file must return an array containing the "key", "secret" keys.' . "\n";
exit(1);
}

return $config;
}

function showHelp()
{
echo <<<HELP
You are missing a "twitter-cli-config.php" or "config/twitter-cli-config.php" file
in your project, which is required to get the Burrow Console working. You can use
the following sample as a template:
<?php
return [
'key' => '<your_key>',
'secret' => '<your_secret>'
];
HELP;
}

/**
* @param $configFile
*/
function showReadErrorMessage($configFile)
{
echo 'Configuration file [' . $configFile . '] does not have read permission.' . "\n";
}


autoload();
$config = getConfig();

$application = new Application();
$application->add(
new AuthenticationCommand(
new TwitterApiGateway(
new CodebirdFactory(),
$config['key'],
$config['secret']
),
'api:authenticate'
)
);

$application->run();
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"role": "Developer"
}
],
"require": {
"beberlei/assert": "^2.6"
},
"require-dev": {
"jublonet/codebird-php": "~3.0",
"phpunit/phpunit": "~4.5",
"Mockery/Mockery": "~0.9",
"friendsofphp/php-cs-fixer": "^1.10",
Expand All @@ -23,7 +27,8 @@
"fzaninotto/faker": "^1.6"
},
"suggest": {
"doctrine/orm": "Use the twitter types in doctrine"
"doctrine/orm": "Use the twitter types in doctrine",
"jublonet/codebird-php": "To get the codebird implementation to access the twitter API"
},
"autoload": {
"psr-4": {
Expand All @@ -35,12 +40,10 @@
"Twitter\\Test\\" : "tests/unit/"
}
},
"bin": [ "bin/twitter" ],
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
}
},
"require": {
"beberlei/assert": "^2.6"
}
}
5 changes: 5 additions & 0 deletions config/twitter-cli-config.example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
return [
'key' => '<your_key>',
'secret' => '<your_secret>'
];
7 changes: 7 additions & 0 deletions src/API/Exception/TwitterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Twitter\API\Exception;

class TwitterException extends \Exception
{
}
45 changes: 45 additions & 0 deletions src/API/Exception/TwitterRateLimitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Twitter\API\Exception;

use Twitter\API\REST\Response\ApiRate;

class TwitterRateLimitException extends TwitterException
{
/** @var string */
private $category;

/** @var ApiRate */
private $rate;

/**
* @return string
*/
public function getCategory()
{
return $this->category;
}

/**
* @return \DateTimeInterface
*/
public function nextWindow()
{
return $this->rate->nextWindow();
}

/**
* @param string $category
* @param ApiRate $rate
*
* @return TwitterRateLimitException
*/
public static function create($category, ApiRate $rate)
{
$exception = new self('You have reached rate limit. You cannot make an API call yet.');
$exception->category = $category;
$exception->rate = $rate;

return $exception;
}
}
11 changes: 11 additions & 0 deletions src/API/REST/ApiParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Twitter\API\REST;

interface ApiParameters
{
/**
* @return array
*/
public function toArray();
}

0 comments on commit a019f96

Please sign in to comment.