Completed
Push — master ( f3d591...31e394 )
by Ben
02:18
created

TranslationController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 7.55 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 0
cbo 5
dl 8
loc 106
ccs 0
cts 67
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A edit() 0 10 1
A update() 0 10 1
B saveValueTranslations() 8 23 4
B groupLinesByKey() 0 30 5
A getFirstSegmentOfKey() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Thinktomorrow\Squanto\Manager\Controllers;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\Squanto\Domain\Line;
7
use Thinktomorrow\Squanto\Domain\Page;
8
9
class TranslationController extends Controller
10
{
11
    public function index()
12
    {
13
        $pages = Page::sequence()->get();
0 ignored issues
show
Bug introduced by
The method sequence() does not exist on Thinktomorrow\Squanto\Domain\Page. Did you maybe mean scopeSequence()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
14
15
        return view('squanto::index',compact('pages'));
16
    }
17
18
    public function edit($id)
19
    {
20
        $available_locales = config('squanto.locales');
21
22
        $page = Page::find($id);
23
24
        $groupedLines = $this->groupLinesByKey($page);
25
26
        return view('squanto::edit', compact('page','available_locales','groupedLines'));
27
    }
28
29
    public function update(Request $request, $page_id)
30
    {
31
        $page = Page::find($page_id);
32
33
        $this->saveValueTranslations($request->get('trans'));
34
35
        // TODO: Resave our cached translation
36
37
        return redirect()->route('back.squanto.edit',$page->id)->with('messages.success', $page->label .' translations have been updated');
38
    }
39
40
    private function saveValueTranslations(array $translations)
41
    {
42
        collect($translations)->map(function($translation,$locale){
43
            collect($translation)->map(function($value,$id) use($locale){
44
45
                $value = cleanupHTML($value);
46
                $line = Line::find($id);
47
48
                // If line value is not meant to contain tags, we should strip them
49
                if(!$line->editInEditor()) $value = cleanupString($value);
50
51 View Code Duplication
                if(is_null($value) || "" === $value)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
                {
53
                    $line->removeValue($locale);
54
                }
55
                else
56
                {
57
                    $line->saveValue($locale,$value);
58
                }
59
60
            });
61
        });
62
    }
63
64
    /**
65
     * @param $page
66
     * @return \Illuminate\Support\Collection
67
     */
68
    private function groupLinesByKey($page)
69
    {
70
        $groupedLines = collect(['general' => []]);
71
        $groups = [];
72
73
        foreach ($page->lines as $line)
74
        {
75
            $keysegment = $this->getFirstSegmentOfKey($line);
76
77
            if (!isset($groups[$keysegment]))
78
            {
79
                $groups[$keysegment] = [];
80
            }
81
            $groups[$keysegment][] = $line;
82
        }
83
84
        // If firstkey occurs more than once, we will group it
85
        foreach ($groups as $group => $lines)
86
        {
87
            if (count($lines) < 2)
88
            {
89
                $groupedLines['general'] = array_merge($groupedLines['general'], $lines);
90
            } else
91
            {
92
                $groupedLines[$group] = $lines;
93
            }
94
        }
95
96
        return $groupedLines;
97
    }
98
99
    /**
100
     * Get suggestion for a label based on the key
101
     * e.g. foo.bar.title return bar
102
     * @return string
103
     */
104
    private function getFirstSegmentOfKey(Line $line)
105
    {
106
        // Remove first part since that part equals the page
107
        $key = substr($line->key, strpos($line->key, '.')+1);
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<Thinktomorrow\Squanto\Domain\Line>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108
109
        $length = strpos($key, '.')?: strlen($key);
110
        $key = substr($key,0,$length);
111
112
        return $key;
113
    }
114
}