2017 © Pedro Peláez
 

library stubborn

Concept taken from Stubborn https://github.com/derekdowling/stubborn

image

gsdevme/stubborn

Concept taken from Stubborn https://github.com/derekdowling/stubborn

  • Tuesday, December 16, 2014
  • by gsdev
  • Repository
  • 2 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

WIP: Stubborn

Travis

Build Status, (*1)

Scrutinizer

Build Status Scrutinizer Code Quality Code Coverage, (*2)

Credit

I was browsing reddit and came across a post.. I like the concept but thought I could improve on the implementation.. so I did * Reddit: http://www.reddit.com/r/PHP/comments/2mqyxw/a_little_library_i_wrote_for_dealing_with/ * Original Developer: https://github.com/derekdowling/stubborn, (*3)

Concept

For each API vendor (e.g. Facebook, Twitter, Dropbox) have the requests implement StubbornAwareInterface and throw the class into Stubborn, You can implement the methods to handle the API oddities, (*4)

Example

class StubbornDummyApi implements \Stubborn\StubbornAwareInterface
{

    private $apiKey;

    public function __construct($apikey)
    {
        $this->apiKey = $apiKey;
    }

    public function getRetryNumber()
    {
        // try twice
        return 1;
    }

    public function getRetryWaitSeconds()
    {
        // wait 5 seconds after each try
        return 10;
    }

    public function run()
    {
        // use the API key or something in the real world?
        $this->apiKey = null;

        // Actual API logic here.. so Facebook, Twitter etc
        return new \Stubborn\StubbornResponse('{"status":true}', 200);
    }

    public function getHttpActionRequest(\Stubborn\StubbornResponseInterface $response)
    {
        // Handle the HTTP code, 501/408 lets retry
        switch($response->getHttpCode()){
            case 501:
            case 408:
                return self::RETRY_ACTION;
            default:
                return false;
        }
    }

    public function getExceptionActionRequest(\Exception $exception)
    {
        switch(true){
            // If UnexpectedValueException retry wait.. maybe got something odd back from the API
            case ($exception instanceof \UnexpectedValueException):
                return self::RETRY_WAIT_ACTION;
            // Default action is to just rethrow the Exception, we don't know what to do with it
            case ($exception instanceof \Exception):
            default:
                throw $exception;
        }
    }
}

$dummyApiRequest = new StubbornDummyApi('123456789qwerty');
$stubborn = new \Stubborn\Stubborn($dummyApiRequest);
$result = $stubborn->run();

if($result instanceOf \Stubborn\StubbornResponseInterface){
    var_dump($result->getHttpCode());
    var_dump($result->getData());
}

The Versions

16/12 2014

dev-master

9999999-dev

Concept taken from Stubborn https://github.com/derekdowling/stubborn

  Sources   Download

The Development Requires

stubborn