protected function findOrInitializeModel($resource_hash) { $model = null; // Find local Model if exists $mno_id = $resource_hash['id']; $mno_id_map = MnoIdMap::findMnoIdMapByMnoIdAndEntityName($mno_id, $this->connec_entity_name, $this->local_entity_name); error_log("find or initialize entity={$this->connec_entity_name}, mno_id={$mno_id}, mno_id_map=" . json_encode($mno_id_map)); if ($mno_id_map) { // Ignore updates for deleted Models if ($mno_id_map['deleted_flag'] == 1) { error_log("ignore update for locally deleted entity={$this->connec_entity_name}, mno_id={$mno_id}"); return null; } // Load the locally mapped Model $model = $this->loadModelById($mno_id_map['app_entity_id']); } // Match a local Model from hash attributes if ($model == null) { $model = $this->matchLocalModel($resource_hash); } // Create a new Model if none found if ($model == null) { $entity_class = $this->local_entity_name; if (class_exists($entity_class)) { $model = new $entity_class(); } else { error_log("Class {$entity_class} not loaded, model cannot be created"); } } return $model; }
protected function persistLocalModel($user, $resource_hash) { // Will be used to check if the user already exist locally $mno_id_map = MnoIdMap::findMnoIdMapByMnoIdAndEntityName($resource_hash['id'], 'APPUSER', 'USERS'); $user->save("Users", false); // We make sure that the mnoIdMap is created before we start to parse the user's teams to bloc kany eventual recursive loop $this->findOrCreateIdMap($resource_hash, $user); // Add user to corresponding teams if (!$mno_id_map) { // User does not exist locally => we add him to its teams $mno_teams = $resource_hash['teams']; $team_mapper = new TeamMapper(); for ($j = 0; $j < count($mno_teams); $j++) { $team_hash = $mno_teams[$j]; // Retrieve the local group if exists or create it based on data from Connec! Entity::Team otherwise $group_model = $team_mapper->fetchConnecResource($team_hash['id']); $group_members = array_keys($group_model->getMembers()["Users"]); // At this stage, the user should exist (has been saved before), and have an id defined array_push($group_members, "Users:" . json_encode($user->id)); $group_model->set('group_members', $group_members); // Will save the new users list for the team $team_mapper->persistLocalModel($group_model, null); } } }