I'm developing an application which sends quotes to clients via email. Since quotes are obtained from various resources (per each client) they end up in a single array which is then used as a source for the email.
The desired functionality is that the client doesn't receive the same quote until the full rotation is complete, but there can be new quotes appearing in the array since I don't control that source.
E.g.
- The client has an array of quotes
[Foo, Bar, Baz]
. - Algorithm randomly picks
Bar
to be sent, flagging it as sent. - Next quote being sent is picking from
[Foo, Baz]
. - Algorithm randomly picks
Foo
to be sent, flagging it as sent. - A new quote becomes available
Qux
. - Next quote being sent is picking from
[Baz, Qux]
. - Algorithm randomly picks
Baz
to be sent, flagging it as sent. - Next quote being sent is picking from
[Qux]
. - Algorithm picks
Qux
to be sent, flagging it as sent. - Next quote being sent has no available items and rerolls already sent items, picking again from
[Foo, Bar, Baz, Qux]
. - Algorithm randomly picks
Foo
to be sent, flagging it as sent.
etc.
I can roughly imagine it being done like this, storing records of already SentQuote
to the database and manually subtracting them from the array. In case there are zero matches I would reroll - invalidating the SentQuote
.
This seems a bit cumbersome to me. Could there be a better way? I am using PHP 7.2
SentQuote
. I was thinking of having a table withquote.id
,client.id
,date_sent
,exclude_rotation
. Setting theexclude_rotation
to 1 in case it is part of the active rotation, invalidating would mean to setexclude_rotation
to 0 for the whole client. This would serve as a log of set quotes as well as an active rotation mechanism.