使用我的控制器方法更简洁的方法

时间:2016-09-21 13:15:37

标签: php laravel-5 coding-style code-cleanup

我正在创建一个应用程序,将单词从一种语言翻译成英语并获取有关它的信息(例如定义,在句子中使用,同义词,声音表示)

我的功能是什么:

  1. 在数据库中搜索翻译。如果找到,我们将其退回。
  2. 如果找不到,我们会使用谷歌翻译翻译一个单词,或者使用Yandex翻译API。
  3. 如果找到翻译,我们会下载它的声音表示,将翻译保存到数据库并添加其他API的其他信息
  4. 我们返回包含所有信息的json响应。
  5. 现在我的控制器方法非常庞大,我找不到更简洁的方法来解决它。

    感谢任何帮助。

    public function store(Request $request)
    {
        $translated = $request->get('translated');
        $translation = $this->translation->findBy('translated', $translated)->first();
    
        if ($translation) {
            return Response::json(['translation' => $this->translation->with(['examples', 'definitions', 'synonyms', 'images'])->find($translation->id)], ResponseCode::HTTP_CREATED);
        }
    
        $data = $request->all();
        $data['translation'] = $this->translate($translated);
    
        if ($translated == $data['translation']) {
            Log::info('Translation not found: ' . $data['translation']);
            return $this->translationNotFound();
        }
    
        $downloader = new Downloader(new GoogleSpeechDownloader());
        $filename = $downloader->download($data['translation']);
    
        if ($filename) $data['sound_name'] = $filename;
    
        $translation = $this->translation->create($data);
    
        $this->createDefinition($translation);
        $this->createExample($translation);
        $this->createSynonym($translation);
    
        return Response::json(['translation' => $this->translation->with(['examples', 'definitions', 'synonyms', 'images'])->find($translation->id)], ResponseCode::HTTP_CREATED);
    }
    
    private function translationNotFound()
    {
        return Response::json(['error' => 'Vertimas nerastas.'], ResponseCode::HTTP_NOT_FOUND);
    }
    
    private function createDefinition($translation)
    {
        $definition = new Definition();
        $definer = new Definer(new DictionaryApiDefiner());
        try {
            $definition->definition = $definer->getDefinition($translation->translation);
            $definition->approved = true;
            $translation->definitions()->save($definition);
        } catch (\Exception $e) {
            Log::alert('Definition for word ' . $translation->translation . ' not found.');
        }
    }
    
    private function createExample($translation)
    {
        $example = new Example();
        $exampler = new ExampleCreator(new YourDictionaryGouteParserExampler());
        try {
            $example->example = $exampler->getExample($translation->translation);
            $example->approved = true;
            $translation->examples()->save($example);
        } catch (\Exception $e) {
            Log::alert('Example for word ' . $translation->translation . ' not found.');
        }
    }
    
    private function createSynonym($translation)
    {
        $creator = new SynonymCreator(new BigHugeLabsSynonymCreator());
        foreach ($creator->getSynonyms($translation->translation) as $s) {
            $synonym = new Synonym();
            $synonym->synonym = $s;
            $synonym->approved = true;
            $translation->synonyms()->save($synonym);
        }
    }
    
    private function translate($translated)
    {
        $translator = new Translator(new GoogleTranslator());
    
        try {
            return $translator->translate($translated);
        } catch (\Exception $e) {
            Log::critical($e->getMessage());
        }
    
        $translator = new Translator(new YandexTranslator());
        return $translator->translate($translated);
    }
    

1 个答案:

答案 0 :(得分:2)

如果你想要更干净的代码,只需为这份工作上课。这两个API的两个类和控制器中的两个类检查单词,如果数据库中不存在,请检查其他两个API,只需将每个操作拆分为新的两个类中的方法你会做的。