A cleaner way to go with my controller method
<p>I am making an app that translates a word from one language to English and gets information about it (e.g. definition, use in a sentence, synonyms, sound representation)</p>

<p>What my function does:</p>

<ol>
<li>Searches for the translation in the database. If it is found, we return it.</li>
<li>If it is not found we translate a word using google translate, or Yandex translate API.</li>
<li>If translation is found we download it's sound representation, save the translation to the database and add additional information from other API's</li>
<li>We return a json response with all of the information.</li>
</ol>

<p>Now my controllers method is really big and I can't find a cleaner way to go about it.</p>

<p>Any help is appreciated.</p>

<pre><code>public function store(Request $request)
{
    $translated = $request-&gt;get('translated');
    $translation = $this-&gt;translation-&gt;findBy('translated', $translated)-&gt;first();

    if ($translation) {
        return Response::json(['translation' =&gt; $this-&gt;translation-&gt;with(['examples', 'definitions', 'synonyms', 'images'])-&gt;find($translation-&gt;id)], ResponseCode::HTTP_CREATED);
    }

    $data = $request-&gt;all();
    $data['translation'] = $this-&gt;translate($translated);

    if ($translated == $data['translation']) {
        Log::info('Translation not found: ' . $data['translation']);
        return $this-&gt;translationNotFound();
    }

    $downloader = new Downloader(new GoogleSpeechDownloader());
    $filename = $downloader-&gt;download($data['translation']);

    if ($filename) $data['sound_name'] = $filename;

    $translation = $this-&gt;translation-&gt;create($data);

    $this-&gt;createDefinition($translation);
    $this-&gt;createExample($translation);
    $this-&gt;createSynonym($translation);

    return Response::json(['translation' =&gt; $this-&gt;translation-&gt;with(['examples', 'definitions', 'synonyms', 'images'])-&gt;find($translation-&gt;id)], ResponseCode::HTTP_CREATED);
}

private function translationNotFound()
{
    return Response::json(['error' =&gt; 'Vertimas nerastas.'], ResponseCode::HTTP_NOT_FOUND);
}

private function createDefinition($translation)
{
    $definition = new Definition();
    $definer = new Definer(new DictionaryApiDefiner());
    try {
        $definition-&gt;definition = $definer-&gt;getDefinition($translation-&gt;translation);
        $definition-&gt;approved = true;
        $translation-&gt;definitions()-&gt;save($definition);
    } catch (\Exception $e) {
        Log::alert('Definition for word ' . $translation-&gt;translation . ' not found.');
    }
}

private function createExample($translation)
{
    $example = new Example();
    $exampler = new ExampleCreator(new YourDictionaryGouteParserExampler());
    try {
        $example-&gt;example = $exampler-&gt;getExample($translation-&gt;translation);
        $example-&gt;approved = true;
        $translation-&gt;examples()-&gt;save($example);
    } catch (\Exception $e) {
        Log::alert('Example for word ' . $translation-&gt;translation . ' not found.');
    }
}

private function createSynonym($translation)
{
    $creator = new SynonymCreator(new BigHugeLabsSynonymCreator());
    foreach ($creator-&gt;getSynonyms($translation-&gt;translation) as $s) {
        $synonym = new Synonym();
        $synonym-&gt;synonym = $s;
        $synonym-&gt;approved = true;
        $translation-&gt;synonyms()-&gt;save($synonym);
    }
}

private function translate($translated)
{
    $translator = new Translator(new GoogleTranslator());

    try {
        return $translator-&gt;translate($translated);
    } catch (\Exception $e) {
        Log::critical($e-&gt;getMessage());
    }

    $translator = new Translator(new YandexTranslator());
    return $translator-&gt;translate($translated);
}
</code></pre>