izzum\statemachine\persistence\Redis::isPersisted PHP Method

isPersisted() public method

{@inheritDoc}
public isPersisted ( Identifier $identifier )
$identifier izzum\statemachine\Identifier
    public function isPersisted(Identifier $identifier)
    {
        try {
            $redis = $this->getRedis();
            //get key from known entity ids set
            $key = sprintf(self::KEY_ENTITYIDS, $identifier->getMachine());
            return $redis->sismember($key, $identifier->getEntityId());
        } catch (\Exception $e) {
            throw new Exception(sprintf('getting persistence info failed: [%s]', $e->getMessage()), Exception::PERSISTENCE_LAYER_EXCEPTION);
        }
    }

Usage Example

 /**
  * @test
  */
 public function shouldBeAbleToLoadConfigurationFromSpecificConfigurationKey()
 {
     $redis = new Redis();
     $redis->setDatabase(15);
     //clear the redis database for testing
     $redis->flushdb();
     $identifier = new Identifier(1, 'test-machine');
     $machine = new StateMachine(new Context($identifier, null, $redis));
     //create the loader
     //get the configuration from the json file
     $configuration = file_get_contents(__DIR__ . '/../loader/fixture-example.json');
     //set it. normally, this would be done by a seperate process that has already loaded the configuration
     $redis->set(sprintf(Redis::KEY_CONFIGURATION_SPECIFIC, $redis->getConfigurationKey(), 'test-machine'), $configuration);
     //load the machine
     $count = $redis->load($machine);
     $this->assertEquals(4, $count, 'expect 4 transitions to be loaded');
     $this->assertCount(4, $machine->getTransitions(), 'there is a regex transition that adds 2 transitions (a-c and b-c)');
     $this->assertCount(4, $machine->getStates());
     $this->assertNotNull($redis->toString());
     $this->assertNotNull($redis . '');
     $this->assertFalse($redis->isPersisted($identifier));
     $this->assertTrue($machine->add('add to the backend'));
     $this->assertFalse($machine->add('add to the backend'), 'already added');
     $this->assertEquals('a', $machine->getCurrentState());
     $this->assertTrue($redis->isPersisted($identifier));
     $this->assertTrue($machine->run('run from a to b'));
     $this->assertEquals('b', $machine->getCurrentState());
     $this->assertTrue($redis->isPersisted($identifier));
     $this->assertTrue($machine->run('some message here to store'));
     $this->assertEquals('done', $machine->getCurrentState());
     $ids = $redis->getEntityIds('test-machine');
     $this->assertEquals(1, count($ids));
     //destroy
     $redis = null;
 }