aboutsummaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
Diffstat (limited to 'classes')
-rw-r--r--classes/Mail.php3
-rw-r--r--classes/Model.php36
-rw-r--r--classes/Offer.php41
-rw-r--r--classes/Payment.php91
4 files changed, 93 insertions, 78 deletions
diff --git a/classes/Mail.php b/classes/Mail.php
index dcc5cf5..06c2eb6 100644
--- a/classes/Mail.php
+++ b/classes/Mail.php
@@ -28,5 +28,6 @@ class Mail extends Model {
/** {@inheritDoc} */
public
$table = 'mail',
- $fillable_columns = ['contactId', 'offerId', 'subject'];
+ $fillable_columns = ['contactId', 'offerId', 'subject'],
+ $timestamps = ['date'];
}
diff --git a/classes/Model.php b/classes/Model.php
index 6ee7821..3b1c49e 100644
--- a/classes/Model.php
+++ b/classes/Model.php
@@ -45,15 +45,21 @@ class ModelSetFailedException extends Exception {
abstract class Model {
/**
* @var string $table The database table
+ * @var string $primary_key The table's primary key
* @var string[] $protected_columns Columns that cannot be edited
* @var string[] $fillable_columns Columns that can be edited
- * @var string $primary_key The table's primary key
+ * @var string[] $timestamps Columns that are TIMESTAMPs (special treatment in accessor and mutator)
+ * @var string[] $dates Columns that are DATEs (special treatment in accessor and mutator)
+ * @var string[] $booleans Columns that are BOOLEANs (special treatment in accessor and mutator)
*/
public
$table = '',
+ $primary_key = 'id',
$protected_columns = ['id'],
$fillable_columns = [],
- $primary_key = 'id';
+ $timestamps = [],
+ $dates = [],
+ $booleans = [];
/**
* @var PDO $pdo A PDO instance for database communication
@@ -91,7 +97,7 @@ abstract class Model {
*/
public function __set($key, $value) {
if (!in_array($key, $this->fillable_columns)) {
- throw new ModelIllegalAccessException("Column $key cannot be edited.");
+ throw new ModelIllegalAccessException("Column `{$this->table()}`.`$key` cannot be edited.");
}
if ($this->data[$key] == $value) {
return;
@@ -102,7 +108,7 @@ abstract class Model {
$this->data[$this->primary_key]
]);
if ($stmt->rowCount() != 1) {
- throw new ModelEditFailedException();
+ throw new ModelEditFailedException("Failed to update `{$this->table()}`.`$key` to '$value'.");
}
$this->data[$key] = $value;
}
@@ -124,10 +130,18 @@ abstract class Model {
* @param string $key The column
* @param string $value The value
*
- * @return mixed The modified value (default: $value)
+ * @return mixed The modified value
*/
protected function accessor($key, $value) {
- return $value;
+ if (is_null($value)) {
+ return null;
+ } elseif (in_array($key, $this->booleans)) {
+ return (bool) $value;
+ } elseif (in_array($key, $this->dates) || in_array($key, $this->timestamps)) {
+ return strtotime($value);
+ } else {
+ return $value;
+ }
}
/**
@@ -136,10 +150,16 @@ abstract class Model {
* @param string $key The column
* @param mixed $value The value
*
- * @return string The modified value (default: $value casted to string)
+ * @return string The modified value
*/
protected function mutator($key, $value) {
- return (string) $value;
+ if (in_array($key, $this->dates) && is_int($value)) {
+ return date('Y-m-d', $value);
+ } elseif (in_array($key, $this->timestamps) && is_int($value)) {
+ return date('Y-m-d H:i:s', $value);
+ } else {
+ return (string) $value;
+ }
}
/**
diff --git a/classes/Offer.php b/classes/Offer.php
index 2a48604..202c0e8 100644
--- a/classes/Offer.php
+++ b/classes/Offer.php
@@ -28,44 +28,9 @@ class Offer extends Model{
/** {@inheritDoc} */
public
$table = 'offer',
- $fillable_columns = ['contactId', 'start_date', 'end_date', 'invoice_date', 'accepted', 'invoice_fileId', 'payment_key'];
-
- /**
- * {@inheritDoc}
- * @param $key {@inheritDoc}
- * @param $value {@inheritDoc}
- */
- protected function accessor($key, $value) {
- switch ($key) {
- case 'start_date':
- case 'end_date':
- case 'invoice_date':
- return strtotime($value);
- break;
- case 'accepted':
- return (bool) $value;
- case 'invoice_fileId':
- return $value == null ? null : (int) $value;
- default:
- return parent::accessor($key, $value);
- }
- }
-
- /**
- * {@inheritDoc}
- * @param $key {@inheritDoc}
- * @param $value {@inheritDoc}
- */
- protected function mutator($key, $value) {
- switch ($key) {
- case 'start_date':
- case 'end_date':
- case 'invoice_date':
- return is_string($value) ? $value : date('Y-m-d', $value);
- default:
- return parent::mutator($key, $value);
- }
- }
+ $fillable_columns = ['contactId', 'start_date', 'end_date', 'invoice_date', 'accepted', 'invoice_fileId', 'payment_key'],
+ $dates = ['start_date', 'end_date', 'invoice_date'],
+ $booleans = ['accepted'];
/**
* A random max-63-char string that can be used as payment_key
diff --git a/classes/Payment.php b/classes/Payment.php
index 64b65ae..bab55be 100644
--- a/classes/Payment.php
+++ b/classes/Payment.php
@@ -28,7 +28,8 @@ class Payment extends Model {
/** {@inheritDoc} */
public
$table = 'payment',
- $fillable_columns = ['offerId', 'date', 'braintree_id'];
+ $fillable_columns = ['offerId', 'date', 'braintree_id', 'braintree_status'],
+ $timestamps = ['date'];
/**
* Get the offer that this payment is linked to
@@ -40,36 +41,6 @@ class Payment extends Model {
}
/**
- * {@inheritDoc}
- * @param $key {@inheritDoc}
- * @param $value {@inheritDoc}
- */
- protected function accessor($key, $value) {
- switch ($key) {
- case 'date':
- return strtotime($value);
- break;
- default:
- return parent::accessor($key, $value);
- }
- }
-
- /**
- * {@inheritDoc}
- * @param $key {@inheritDoc}
- * @param $value {@inheritDoc}
- */
- protected function mutator($key, $value) {
- switch ($key) {
- case 'date':
- return is_string($value) ? $value : date('Y-m-d H:i:s', $value);
- break;
- default:
- return parent::mutator($key, $value);
- }
- }
-
- /**
* Make a mailer to send about this payment
*
* @return Mailer The mailer
@@ -92,4 +63,62 @@ class Payment extends Model {
public function send() {
return $this->mailer()->send();
}
+
+ /**
+ * Refresh the Braintree status
+ *
+ * @return string|null The new status, or null if this Payment does not have a Braintree id
+ */
+ public function refreshBraintreeStatus() {
+ if (is_null($this->braintree_id)) {
+ return null;
+ }
+
+ $trans = Braintree_Transaction::find($this->braintree_id);
+ $this->braintree_status = $trans->status;
+ }
+
+ /**
+ * Is the Braintree transaction finished?
+ *
+ * @return bool|null True iff the Braintree transaction is finished, or null if this Payment does not have a Braintree id
+ */
+ public function isBraintreeFinished() {
+ if (is_null($this->braintree_id)) {
+ return null;
+ }
+
+ return in_array($this->braintree_status, [
+ Braintree_Transaction::AUTHORIZATION_EXPIRED,
+ Braintree_Transaction::GATEWAY_REJECTED,
+ Braintree_Transaction::FAILED,
+ Braintree_Transaction::PROCESSOR_DECLINED,
+ Braintree_Transaction::SETTLED,
+ Braintree_Transaction::VOIDED,
+ Braintree_Transaction::SETTLEMENT_DECLINED,
+ ]);
+ }
+
+ /**
+ * Is the Braintree transaction successful?
+ *
+ * A transaction can be successful if it is not yet finished.
+ *
+ * @return bool|null True iff the Braintree transaction is successful, or null if this Payment does not have a Braintree id
+ */
+ public function isBraintreeSuccessful() {
+ if (is_null($this->braintree_id)) {
+ return null;
+ }
+
+ return in_array($this->braintree_status, [
+ Braintree_Transaction::AUTHORIZING,
+ Braintree_Transaction::AUTHORIZED,
+ Braintree_Transaction::SETTLED,
+ Braintree_Transaction::SETTLING,
+ Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT,
+ Braintree_Transaction::SETTLEMENT_PENDING,
+ Braintree_Transaction::SETTLEMENT_CONFIRMED,
+ ]);
+ }
}