@TheKeyboard

The Story and Rebirth of Zend_Service_Audioscrobbler, Part 1

| Comments

Many moons ago I set out (along with my friend Derek) to make a contribution to Zend Framework in the form of an add-on to let people easily access the Audioscrobbler web service. We worked really hard, implemented all the features of the web service at that time. Imagine our surprise when it got accepted as part of the 1.0 release of Zend Framework! Awesome! It even has it’s own entry in the manual and everything.

However, I have a confession to make, although it will not come as a surprise to anyone who reads my blog on a regular basis: the code is a complete piece of shit. There. I said it. How do I know that it’s terrible and needs refactoring in a fierce way? Check out this lovely snippet of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    //////////////////////////////////////////////////////////
    ///////////////////////  USER  ///////////////////////////
    //////////////////////////////////////////////////////////

    /**
    * Utility function to get Audioscrobbler profile information (eg: Name, Gender)
    * @return array containing information
    */
    public function userGetProfileInformation()
    {
        $service = "/{$this->get('version')}/user/{$this->get('user')}/profile.xml";
        return $this->getInfo($service);
    }

    /**
     * Utility function get this user's 50 most played artists
     * @return array containing info
    */
    public function userGetTopArtists()
    {
        $service = "/{$this->get('version')}/user/{$this->get('user')}/topartists.xml";
        return $this->getInfo($service);
    }

    /**
     * Utility function to get this user's 50 most played albums
     * @return SimpleXML object containing result set
    */
    public function userGetTopAlbums()
    {
        $service = "/{$this->get('version')}/user/{$this->get('user')}/topalbums.xml";
        return $this->getInfo($service);
    }
It goes on and on and freakin’ on like this. SEVENTEEN methods just for dealing with user stuff. This is insane. Going back over the comments when I was building this thing I realized I totally ignored created elegant code and instead just ‘banged out something that worked’. So, I’m going back to the drawing board and are going to refactor this puppy so it makes sense. First up, let’s talk about dealing with users. Wouldn’t it be better if we had something like this:
1
2
3
4
public function user($action) {
     $service = "/{$this->get('version')}/user/{$this->get('user')}/{$action}.xml";
     return $this->getInfo($service);
}
Now, THAT looks like nice and elegant. All I have to do is establish the convention on how to connect to various user-related web services. That’s as simple as comments in the file itself. Now, to replicate what I was doing before, here are how simple the calls could be:
1
2
3
4
5
6
$zsa = new Zend_Service_Audioscrobbler();
$zsa->set('user', 'chartjes');
$zsa->set('version', '1.0');
$userProfile = $zsa->user('profile');
$userTopArtists = $zsa->user('topartists');
$userTopAlbums = $zsa->user('topalbums');
I think I just got rid of something like 200 lines of code…and that’s just in the user section. Clearly, I majorly screwed it up when I did it the first. Luckily for me, there is built-in testing for all this stuff so I can refactor and test as I go.