/** * @param Account $account * @param string $projectName * @param UploadedFile[] $files * * @return \AppBundle\Model\Project */ public function create(Account $account, $projectName, array $files) { $project = $this->store->addProject($account, $projectName); foreach ($files as $file) { if ($file instanceof UploadedFile) { $this->store->addFile($account, $project, $file); } else { throw new \InvalidArgumentException('Expected UploadedFile'); } } return $project; }
/** * @param Account $account * @param Project $project * @param UploadedFile $file * @param bool $save * * @return Change */ private function addFile(Account $account, Project $project, UploadedFile $file, $save) { $result = new Change(); list($domain, $locale, $format) = $this->getDomainLocaleFormatFromFilename($file->getClientOriginalName()); /** @var MessageCatalogue $inputCatalogue */ $inputCatalogue = $this->loaderManager->loadFile($file->getPathname(), $format, $locale, $domain); $inputDomain = $inputCatalogue->getDomain($domain); $loadedProject = $this->projectLoader->load($project, $domain, $locale); if ($loadedProject->getDomain() == $domain && $loadedProject->getLocale() == $locale) { // existing file /** * 1) remove from local all keys that do not exist in input * 2) add to local all keys that exist in input and do not exist in local * 3) update all empty app keys with values from input * 4) report conflicts where app values are different then input */ $path = $project->getFilePathName($domain, $locale); $localCatalogue = $loadedProject->getCatalogue(); $localDomain = $localCatalogue->getDomain($domain); /** @var Message $localMessage */ /** @var Message $inputMessage */ // find deleted foreach ($localDomain->all() as $localMessage) { $id = $localMessage->getId(); if (false == $inputDomain->has($id)) { $result->addDeleted($id); } } // find added foreach ($inputDomain->all() as $inputMessage) { $id = $inputMessage->getId(); if (false == $localDomain->has($id)) { $result->addAdded($id); } } // find updated empty local values & find conflicts and set values to local values foreach ($inputDomain->all() as $inputMessage) { $id = $inputMessage->getId(); if ($localDomain->has($id)) { $localMessage = $localDomain->get($id); $valueLocal = $localMessage->getLocaleString(); $valueInput = $inputMessage->getLocaleString(); if ($valueInput == $valueLocal) { continue; } if ($valueLocal == '' && $valueInput != '') { $result->addModified($id); } elseif ($valueLocal != $valueInput && $valueInput != '') { $result->addConflict($id, $localMessage->getLocaleString(), $inputMessage->getLocaleString()); $inputMessage->setLocaleString($localMessage->getLocaleString()); } else { $inputMessage->setLocaleString($valueLocal); } } } if ($save) { $this->writer->write($inputCatalogue, $domain, $path, $format); } } else { // new file /** @var Message $message */ foreach ($inputDomain->all() as $message) { $result->addAdded($message->getId()); } if ($save) { $this->store->addFile($account, $project, $file); } } return $result; }
/** * Refreshes the user for the account interface. * * It is up to the implementation to decide if the user data should be * totally reloaded (e.g. from the database), or if the UserInterface * object can just be merged into some internal array of users / identity * map. * * @param UserInterface $user * * @return UserInterface * * @throws UnsupportedUserException if the account is not supported */ public function refreshUser(UserInterface $user) { return new Account($user->getUsername(), $this->store->load($user->getUsername())); }