0

I have a lot of status and I'm building functions for each status. I'm trying to figure out how I can revert changes based on status. The be a bit more clear: Say you have 5 status: START INSTALLED PROC COMPLETE UPLOADED

In order to go from status COMPLETE to UPLOADED I would have to run PROC status and INSTALLED. I'm trying to build a simple system where I can call a function and it will know which functions to run depending on their dependencies define in an array or something.

Assumptions: Each status can be reverted backwards only. So if in the above example, UPLOADED can't go to the COMPLETE status. Each status can revert to itself (Status UPLOADED and revert to UPLOADED).

Anyone know how I can go about this? of course there's gonna be a lot more status, I'm just trying to avoid 50 if statements :/

user962449
  • 3,743
  • 9
  • 38
  • 53

2 Answers2

0

Create a DAG (Directed Acyclic Graph) with your possible status paths. Then you can traverse the graph with a provided starting point, and call functions based on the value of the node that you're visiting.

So if your node had the value complete, I assume that you want to call the complete(...) function. So you can do something like this:

call_user_func($nodeValue, ...);
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

I'd wrap this into a simple old integer. Think of it this way:

class Foo {
    const START     = 1; // 2^0
    const INSTALLED = 2; // 2^1
    const PROC      = 4; // 2^2
    const COMPLETE  = 8; // 2^3
    const UPLOADED  = 16;// 2^4

    // all powers of 2
}

If you back your tasks in a DB, it'll be very elegant too, a single integer can contain entire status. You could also quickly query all jobs with a given status very easily using a bit mask.

I digress. If you think of a job that got to PROC, it'll look like this:

00111

(which is actually a 7)

So moving jobs forward and rolling back, is just a question of checking next bit... tons of ways to skin that cat. What you get then, is an easy means to associate functions to bits, a compact means of storing status, and tons of math to help you evaluate steps needed.

Saeven
  • 2,280
  • 1
  • 20
  • 33