Skip to main content
Notice removed Draw attention by gearsdigital
Bounty Ended with Corbin's answer chosen by gearsdigital
Tweeted twitter.com/#!/StackCodeReview/status/274645805586055168
Notice added Draw attention by gearsdigital
Bounty Started worth 50 reputation by gearsdigital
added 22 characters in body
Source Link

I'm looking for a better way to to provide configuration options and improvements in general. I'm using the constructor injection pattern to get access to the Cache and Twitter class. $callback* and $callback$params and are both part of an$params$_GET` are both part of an $_GET array.

I'm not happy with $config at all. Maybe it isit's better to add the whole $_GET array.? But i'm not sure how to handle the JSONP Callback. 

By the way the callback is added by jQuery and necessary to return a valid JSONP.

Array
(
    [jsonp_callback]   => jQuery1820268558845622465_1354013902493
    [include_entities] => true
    [include_rts]      => true
    [screen_name]      => my_screen_name
    [count]            => 3
)

I'm looking for a better way to to provide configuration options and improvements in general. I'm using the constructor injection pattern to get access to the Cache and Twitter class. $callback* and $paramsare both part of an$_GET` array.

I'm not happy with $config at all. Maybe it is better to add the whole $_GET array. But i'm not sure how to handle the JSONP Callback. By the way the callback is added by jQuery and necessary to return a valid JSONP.

Array
(
    [jsonp_callback] => jQuery1820268558845622465_1354013902493
    [include_entities] => true
    [include_rts] => true
    [screen_name] => my_screen_name
    [count] => 3
)

I'm looking for a better way to provide configuration options and improvements in general. I'm using the constructor injection pattern to get access to the Cache and Twitter class. $callback and $params are both part of an $_GET array.

I'm not happy with $config at all. Maybe it's better to add the whole $_GET array? But i'm not sure how to handle the JSONP Callback. 

By the way the callback is added by jQuery and necessary to return a valid JSONP.

Array
(
    [jsonp_callback]   => jQuery1820268558845622465_1354013902493
    [include_entities] => true
    [include_rts]      => true
    [screen_name]      => my_screen_name
    [count]            => 3
)
Source Link

Optimize PHP Class

I'm looking for a better way to to provide configuration options and improvements in general. I'm using the constructor injection pattern to get access to the Cache and Twitter class. $callback* and $paramsare both part of an$_GET` array.

I'm not happy with $config at all. Maybe it is better to add the whole $_GET array. But i'm not sure how to handle the JSONP Callback. By the way the callback is added by jQuery and necessary to return a valid JSONP.

##Instantiation:

// setup config object
$config = (object) array(
    'callback' => $_GET['jsonp_callback'],
    'params'   => $_GET,
);

// new instance of Cache
$cache = new Cache();

// new instance of TwitterAuth
$twitter = new TwitterAuth();

// new instance of tweet wrapper
$ctweet  = new CTweet($cache, $twitter, $config->callback, $config->params);

##Array Data

Array
(
    [jsonp_callback] => jQuery1820268558845622465_1354013902493
    [include_entities] => true
    [include_rts] => true
    [screen_name] => my_screen_name
    [count] => 3
)

The array is built by jQuery. Excluding the [jsonp_callback] all array entries are to retrieve API Data by the TwitterAuth class.

##Complete Class

/**
 * CTweet
 *
 * This class gets json data from the twitter api,
 * stores them into a flat file and returns the
 * data as jsonp.
 *
 * @version 1.0
 */
class CTweet
{
    /** @type string Cache expiration time */
    private $expiration = "3600";
    
    
    /** @type string Twitter API Status */
    private $api_status = "statuses/user_timeline";
    
    
    /** @type object Instance of Cache */
    private $cache;
    
    
    /** @type object Instance of TwitterOAuth */
    private $twitter;
    
    
    /** @type array TwitterOAuth API Params */
    private $params;
    
    
    /** @type object JSONP Callback */
    private $callback;
    
    
    /**
     * Constructor
     * 
     * @access public
     * @param object $cache
     * @param object $twitter
     * @param string $callback
     * @param array $params
     */
    public function __construct( $cache, $api, $callback, $params )
    {         
        if( is_array( $params ) ) {
            $this->params = $params;
        }    

        $this->cache     = $cache;
        $this->twitter   = $api;
        $this->callback  = $callback;
        $this->cachename = $this->cache->getCache();
    }
    
    
    /**
     * Cache validation
     *
     * This Method deletes expired entries and regenerates cached data.
     * 
     * @access private
     * @param $cache string
     * @return boolean
     */
    private function validate_cache( $cache )
    {
        $this->cache->eraseExpired();
        
        if ( ! $this->cache->isCached( $cache ) ) {
            $this->refresh_cache();
        }
        
        return true;
    }
    
    
    /**
     * Cache refreshing
     *
     * This Method generates the cache file and sets the cachname, data
     * and the expiration time.
     * 
     * @access private
     * @see Cache::store() https://github.com/cosenary/Simple-PHP-Cache
     */
    private function refresh_cache()
    {
        return $this->cache->store( $this->cachename, $this->twitter_api_call( $this->api_status, $this->params ), $this->expiration );
    }
    
    
    /**
     * Get Cache
     *
     * This Method returns the cached data if cache validation was successfull
     * 
     * @access private
     * @param string
     * @see Cache::retrieve() https://github.com/cosenary/Simple-PHP-Cache
     * @return object
     */
    private function get_json( $cache )
    {
        if ( $this->validate_cache( $cache ) ) {
            $json = $this->cache->retrieve( $cache );
        }
        return $json;
    }
    
    
    /**
     * Retrieve Data
     *
     * This Method retrieves and returned data from Twitter API.
     * 
     * @access private
     * @param $status string api status
     * @param $params array return parameters
     * @see TwitterOAuth::get() https://github.com/abraham/twitteroauth/blob/master/DOCUMENTATION
     * @return object
     */
    private function twitter_api_call( $status, $params )
    {
        return $this->twitter->get( $status, $params );
    }
    
    
    /**
     * Has callback
     *
     * This Method returns true only if a JSONP Callback is available
     * 
     * @access private
     * @return boolean
     */
    private function has_callback()
    {
        if ( isset( $this->callback ) ) {
            $callback = true;
        }
        return $callback;
    }
    
    
    /**
     * Output mimetype
     * 
     * This Method returns a specified file header
     *
     * @access private
     * @param $header string
     */
    private function set_header( $header )
    {
        switch ( $header ) {
            case "json":
                $header = header( "content-type: application/json" );
                break;
        }
        return $header;
    }
    
    
    /**
     * JSONP callback
     *
     * This Method creates the a jsonp callback
     * 
     * @access private
     * @param $cache string
     * @return jsonp
     */
    private function get_jsonp( $cache )
    {
        return $this->callback . '(' . $this->get_json( $cache ) . ');';
    }
    
    
    /**
     * Data 
     * 
     * This Method sets output mime type and returns jsonp if a callback is
     * available, otherwise the return value is plain json.
     * 
     * @access private
     * @return object
     */
    private function data( $cache )
    {
        if ( $this->has_callback() ) {
            $data = $this->get_jsonp( $this->cachename );
        } else {
            $data = $this->get_json( $this->cachename );
        }
        
        $this->set_header( "json" );
        
        return $data;
    }
    
    
    /**
     * Public interface
     *
     * This Method return cached data to the client.
     * 
     * @access public
     * @return object
     */
    public function get_data()
    {
        return $this->data();
    }
}