Swiftriver\Core\ObjectModel\ObjectFactories\ChannelFactory::CreateChannelFromJSON PHP Method

CreateChannelFromJSON() public static method

public static CreateChannelFromJSON ( $json )
    public static function CreateChannelFromJSON($json)
    {
        //decode the json
        $object = json_decode($json);
        //If there is an error in the JSON
        if (!$object || $object == null) {
            throw new \Exception("There was an error in the JSON passed in to the ChannelFactory.");
        }
        //create a new Channel
        $channel = new \Swiftriver\Core\ObjectModel\Channel();
        //set the basic properties
        $channel->id = isset($object->id) ? $object->id : md5(uniqid(rand(), true));
        $channel->name = isset($object->name) ? $object->name : null;
        $channel->type = isset($object->type) ? $object->type : null;
        $channel->subType = isset($object->subType) ? $object->subType : null;
        $channel->updatePeriod = isset($object->updatePeriod) ? $object->updatePeriod : 30;
        $channel->nextrun = isset($object->nextrun) ? $object->nextrun : strtotime("+ " . $channel->updatePeriod . " minutes");
        $channel->active = isset($object->active) ? $object->active : true;
        $channel->lastSuccess = isset($object->lastSuccess) ? $object->lastSuccess : null;
        $channel->inprocess = isset($object->inprocess) ? $object->inprocess : false;
        $channel->timesrun = isset($object->timesrun) ? $object->timesrun : 0;
        $channel->deleted = isset($object->deleted) ? $object->deleted : false;
        $channel->trusted = isset($object->trusted) ? $object->trusted : false;
        $channel->parameters = array();
        //If te parameters collection is set move tem to the channel
        if (isset($object->parameters)) {
            foreach ($object->parameters as $key => $value) {
                $channel->parameters[$key] = $value;
            }
        }
        //return the Channel
        return $channel;
    }

Usage Example

 public function ParseJSONToChannel($json)
 {
     $logger = \Swiftriver\Core\Setup::GetLogger();
     $logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParseJSONToChannel [Method invoked]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParseJSONToChannel [START: Creating new Channel]", \PEAR_LOG_DEBUG);
     try {
         //Try and get a Channel from the factory
         $channel = \Swiftriver\Core\ObjectModel\ObjectFactories\ChannelFactory::CreateChannelFromJSON($json);
     } catch (\Exception $e) {
         //If exception, get the mesasge
         $message = $e->getMessage();
         //and log it
         $logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParseJSONToChannel [{$message}]", \PEAR_LOG_ERR);
         $logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParseJSONToChannel [Method finished]", \PEAR_LOG_INFO);
         throw new \InvalidArgumentException("The JSON passed to this method did not contain data required to construct a Channel object: {$message}");
     }
     $logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParseJSONToChannel [END: Creating new Channel]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParseJSONToChannel [Method finished]", \PEAR_LOG_DEBUG);
     return $channel;
 }
All Usage Examples Of Swiftriver\Core\ObjectModel\ObjectFactories\ChannelFactory::CreateChannelFromJSON