I'm writing a very basic custom class for coupons and I've come up with a basic layout for the class, which consists of a number of small methods as I generally understand is a best practice.
What I'm unsure of is how the flow of a class should be dictated. Right now the code looks something like this:
public function isCouponValid($coupon) {
if (strlen($coupon) != 8) return false;
$query = $this->db->prepare('SELECT coupon_code FROM coupons WHERE coupon_code=:coupon');
try ($query->execute(array(':coupon' => $coupon))) {
while ($row = $query->fetch()) {
return true;
}
} catch (PDOException $e) {
$log->addError('Could not execute SQL query: '.$query.' '.$e->getMessage());
}
return false;
}
public function isCouponUsed($coupon) {
if (self::isCouponValid($coupon)) {
$query = $this->db->prepare('SELECT coupon_used FROM coupons WHERE coupon_code=:coupon');
try ($query->execute(array(':coupon' => $coupon))) {
while ($row = $query->fetch()) {
return ($row['coupon_used'] == '1') ? true : false;
}
} catch (PDOException $e) {
$log->addError('Could not execute SQL query: '.$query.' '.$e->getMessage());
}
}
return false;
}
public function setCouponUsed($coupon) {
if (self::isCouponValid($coupon) && !self::isCouponUsed($coupon)) {
etc...
}
}
Basically I have these checks integrated into the class right now - isCouponUsed() calls isCouponValid() in order to check that it's valid first. Is this a good idea, or is it better for me to use isCouponValid() outside of the class and then pass that value to isCouponUsed(), such that isCouponUsed() should just be concerned with validating a coupon and not check if that code is valid first?