The reason I'm posting this piece of code is because I want to know how I'm supposed to properly handle the change in requirements/specifications and how I should accommodate it in unit testing.
/**
* Create an Affiliate
*/
public function createUser($username, $password, $email, $firstName, $lastName, $ip, $notifications = null) {
// These values are assumed to be validated prior to this function being
// called.
$date = new \DateTime();
$user = new Affiliate();
$user->setUsername($username)
->setPassword(AffiliateUtils::hashPassword($password))
->setStatus(Affiliate::STATUS_PENDING)
->setEmail($email)
->setFirstName($firstName)
->setLastName($lastName)
->setLastIp($ip)
->setRegisteredIp($ip)
->setLastDate($date)
->setRegisteredDate($date);
try {
$this->em->persist($user);
$this->em->flush();
} catch (DBALException $e) { return false; }
if ($notifications != null)
$notifications->sendAffiliateEmail("WELCOME_AFFILIATE", $user);
return true;
}
This function is apart of an AffiliateHandler
, which is responsible for the various business-logic-related operations on an Affiliate
which is a database entity (active record). This function is called when a user registers for my site.
Prior to the change in requirements, the $notifications
parameter was not required. The notifications class is responsible for sending a welcome email containing a link for the user to verify their email. I added a default value to the variable because the creation of the user is not dependent upon a notification and I didn't want to break/modify the unit tests associated with this particular function.
I feel like this is bad style because this function is doing too many things. Also, it creates somewhat of a dependency on the notifications class. But at the same time, if I break up the function, I need to have access to the new user created so I can send him/her the notification.
What should I be doing to handle this change without adding too much complexity to my code?