One of our client’s sites integrates with a third party CRM and we ran into the issue of staging site edits being pushed to it which wasn’t ideal; we’d do all sorts of testing on staging resulting in test data ending up in the live CRM account.
An interesting way around this, with minimal code changes, is to create a new class which allows the original class to do “nothing” and crucially doesn’t produce an error when calling methods. Let’s take a look at the code:
class ClassFactory{
public static function create(){
if(strpos($_SERVER['SERVER_NAME'], "staging.mysite.com") !== false){
return new NullClass(); // Return null version on staging
}
return new OriginalCRMClass(); // Return real class
}
}
The above class will check the domain of the site it’s currently on and, if the staging version, will return NullClass() as defined below, and if not on the staging site will return the original CRM class we created.
// Null class that overrides all methods
class NullClass{
// Magic method to catch any method calls and do nothing
public function __call($name, $arguments){
// Do nothing, no matter which method is called
}
}
Now we can initiate the class ($class = ClassFactory::create()
) and use like normal (e.g. $class->updateContact($user)
) which will work as it did previously on the live site but the CRM code won’t run on the staging site, “failing” gracefully without error.
This code is free to use at your own discretion. It comes without warranty. Please feel free to feedback any edits.
Like what you’ve read, then why not tell others about it... they might enjoy it too
If you think Bronco has the skills to take your business forward then what are you waiting for?
Get in Touch Today!
Discussion