0

I have a Subscription entity that has status and canceledAt fields.

I want status to change from active to canceled when canceledAt has expired.

So, I imagine checking canceledAt in Subscription::getStatus method:

// Subscription.php

public function getStatus()
{
     // .... check canceledAt

     return $this->status;
}

but that would also require to change the status at persistence layer.

Should I do something with events?

Tseng
  • 61,549
  • 15
  • 193
  • 205
jerkan
  • 685
  • 2
  • 10
  • 28

3 Answers3

0

If you don't consider time an input value, think about it until you do - it is an important concept -- John Carmack, 1998

For a simple query, a straight forward way to handle this is to pass the current time as an input

// Subscription.php

public function getStatus(currentTime)
{
    // .... check canceledAt
    if (currentTime > canceledAt) {
        // ....
    }

    return $this->status;
}

On the other hand, if what you are trying to do is effect an authoritative change in status, then you pass the current time to you domain model in a command.

public function onTick(currentTime) {
    // ....
}

In some cases, it may make sense to represent time via a domain service representation of a clock, rather than as a value. The important point is that the application, not the domain model, is responsible for "what time is it?"

VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
0

In the end, I created an application service to get the status of a subscription so I can store it in the database in case it is needed.

Moreover, I removed the getStatus method from Subscription entity.

jerkan
  • 685
  • 2
  • 10
  • 28
0

I wouldn't persist any status. Just the canceledAt datetime. Why? Because the status depends on current datetime. You could persist a status value now, but that value could be wrong e.g. the next minute. Then, from the next minute and later, you would have a wrong value persisted.

In my opinion, it doesn't make sense to persist a value that changes depending on time, because the meaning of persist is right the opposite, is to store a value that won't change per se.

I would calculate the status when requested, depending on current datetime and canceledAt, but I wouldn't persist it. Since it depends on current datetime you should consider current datetime as an input value, like @VoiceOfUnreason said in his answer.

choquero70
  • 4,470
  • 2
  • 28
  • 48