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);
}
1
2
3
4
public function user($action) {
$service = "/{$this->get('version')}/user/{$this->get('user')}/{$action}.xml";
return $this->getInfo($service);
}
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');