18
Dec
Posted by Chris Hartjes in Uncategorized. 1 Comment
So, dear readers, I received some nice emails and comments from those you have used Zend_Service_Audioscrobbler, along with having a nice email conversation with Wil Sinclair from Zend (who is involved with Zend Framework). Wil told me that backwards compatibility for the 1.5 version is important so I can’t just rip everything up like I want to. Well, I could but it probably wouldn’t be accepted. I’d have to write some letter to convince the Elder Gods Of Zend Framework of why I needed to break backwards compatibility. In Wil’s words: “‘I find it embarrassing and want to destroy the evidence’ will not be considered a good enough reason. “.
I had a good chuckle at that, but Wil did offer up a very good suggestion on how to go about the refactor that I hadn’t thought of: use a _call() method in the class to intercept all the old calls and translate them into new calls. I would also get it to trigger a warning that the old method would be deprecated in a future release as a way to get people to upgrade to the newer ones. Once I have some code, I will show it. Thanks Wil!
Article Tags >>
Audioscrobbler ||
PHP ||
Zend Framework
11
Dec
Posted by Chris Hartjes in Chris' Brain. 5 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:
PHP:
-
//////////////////////////////////////////////////////////
-
/////////////////////// 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:
PHP:
-
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:
PHP:
-
$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.
Article Tags >>
Audioscrobbler ||
PHP ||
refactoring ||
Zend Framework ||
Zend_Service_Audioscrobbler
Recent Comments