cosenary / Instagram-PHP-API

An easy-to-use PHP Class for accessing Instagram's API.
http://cosenary.github.com/Instagram-PHP-API
BSD 3-Clause "New" or "Revised" License
1.46k stars 781 forks source link

Added event onResponse. #207

Closed ghost closed 3 years ago

ghost commented 8 years ago

Hey,

i added this event-logic on the class, because I found hard to make logging possible.

I do it this way, because I wanted to avoid inheritance as bad practice and composition (via decorator for example) is not easily possible because of missing interface. Also, it would be extremely weird to wrap all those public method in some LoggingInstagramDecorator.

ghost commented 8 years ago

I tested it via this test (if you want to I can add it to repository):

File: tests/OnResponseTest.phpt

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use MetzWeb\Instagram\Instagram;

$apiKey = '';
$token = '';

$logData = [];

$instagram = new Instagram($apiKey);
$instagram->setAccessToken($token);

$instagram->onResponse[] = function ($apiCall, array $params, $method, $jsonData) use (&$logData) {
    $logData = [
        'apiCall' => $apiCall,
        'params' => $params,
        'method' => $method,
        'jsonData' => $jsonData,
    ];
};

$instagram->getUserMedia();

if ($logData['apiCall'] !== 'https://api.instagram.com/v1/users/self/media/recent?access_token=261457396.55621d5.b1763002ff49447d86a1977500c04506&') {
    print_r($logData['apiCall']);
    die ("Assert 'apiCall' FAILED!\n");
}

if ($logData['params'] !== []) {
    print_r($logData['params']);
    die ("Assert 'params' FAILED!\n");
}

if ($logData['method'] !== 'GET') {
    print_r($logData['method']);
    die ("Assert 'params' FAILED!\n");
}

print_r($logData['jsonData']);