PHP Finite State Machine