private static function genCode($length = 4) { $count = 0; while ($count < COUPON_CODE_TRIES) { $code = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyz"), 0, $length); if (!Coupon::try_get_coupon_by_code($code)) { return $code; } } return null; }