izzum\statemachine\persistence\MongoDB::getEntityIds PHP Method

getEntityIds() public method

{@inheritDoc}
public getEntityIds ( $machine, $state = null )
    public function getEntityIds($machine, $state = null)
    {
        $output = array();
        try {
            $client = $this->getClient();
            $query = array("machine" => $machine);
            if ($state !== null) {
                $query["state"] = $state;
            }
            $projection = array("entity_id" => 1);
            //find all in the 'states' collection
            $found = $client->izzum->states->find($query, $projection);
            foreach ($found as $data) {
                $output[] = $data['entity_id'];
            }
        } catch (\Exception $e) {
            throw new Exception($e->getMessage(), Exception::PERSISTENCE_LAYER_EXCEPTION, $e);
        }
        return $output;
    }

Usage Example

 /**
  * @test
  */
 public function shouldBeAbleToStoreAndRetrieveData()
 {
     $adapter = new MongoDB("mongodb://localhost:27017");
     //fixture
     $adapter->getClient()->izzum->states->drop();
     $adapter->getClient()->izzum->configuration->drop();
     $adapter->getClient()->izzum->history->drop();
     $configuration = file_get_contents(__DIR__ . '/../loader/fixture-example.json');
     //via the mongo shell, you could directly enter the json.
     //via php, we first need to decode the json to input it via the php mongo driver as an array
     $configuration = json_decode($configuration, true);
     //var_dump( $configuration);
     $adapter->getClient()->izzum->configuration->insert($configuration);
     //end fixture
     $machine = new StateMachine(new Context(new Identifier('mongo', 'test-machine'), null, $adapter));
     $adapter->load($machine);
     $machine->add("adding for " . __FUNCTION__);
     $machine->runToCompletion("testing 213");
     $this->assertEquals(array("mongo"), $adapter->getEntityIds('test-machine'));
     $this->assertEquals(array("mongo"), $adapter->getEntityIds('test-machine', 'done'));
     $this->assertEquals(array(), $adapter->getEntityIds('test-machine', 'a'));
     $this->assertEquals(array(), $adapter->getEntityIds('test-machine', 'b'));
     $this->assertEquals(array(), $adapter->getEntityIds('test-machine', 'c'));
     $machine = new StateMachine(new Context(new Identifier('another-mongo', 'test-machine'), null, $adapter));
     $adapter->load($machine);
     $machine->add("adding for " . __FUNCTION__);
     $machine->runToCompletion("testing 213");
     $machine = new StateMachine(new Context(new Identifier('foobar', 'non-used-machine'), null, $adapter));
     $adapter->load($machine);
     $machine->add("adding for " . __FUNCTION__);
     $machine->runToCompletion("testing 213");
     $index = array("entity_id" => 1, "machine" => 1);
     $options = array("background" => true);
     $adapter->getClient()->izzum->history->createIndex($index, $options);
     //getting the state for an entity_id/machine should be fast
     //db.states.createIndex({entity_id: 1, machine: 1}, {background: true});
     $index = array("entity_id" => 1, "machine" => 1);
     $options = array("background" => true);
     $adapter->getClient()->izzum->states->createIndex($index, $options);
     //recreate the existing statemachine
     $machine = new StateMachine(new Context(new Identifier('another-mongo', 'test-machine'), null, $adapter));
     $adapter->load($machine);
     $this->assertFalse($machine->add(), 'already added');
     $this->assertEquals('done', $machine->getCurrentState()->getName(), 'state persisted');
     $this->assertEquals(0, $machine->runToCompletion(), 'alread in a final state, no transitions');
     $this->markTestIncomplete("needs more tests to check the database and some scenarios");
 }