Completed
Push — master ( 446e6e...f3d591 )
by Ben
08:37
created

Line::getValuesByLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Thinktomorrow\Squanto\Domain;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Dimsav\Translatable\Translatable as BaseTranslatable;
7
use Illuminate\Support\Facades\DB;
8
9
/**
10
 * Class Line
11
 *
12
 * @package Thinktomorrow\Squanto\Domain
13
 */
14
class Line extends Model
15
{
16
    use BaseTranslatable, Translatable;
17
18
    public $table = 'squanto_lines';
19 36
    public $translatedAttributes = ['value'];
20
21 36
    /**
22
     * @param $key
23 36
     * @return Line
24 36
     */
25 36
    public static function make($key)
26 36
    {
27
        $linekey = new LineKey($key);
28 36
29
        $line = new self;
30 36
        $line->key = $linekey->get();
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<Thinktomorrow\Squanto\Domain\Line>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
31
        $line->label = $linekey->getAsLabel();
0 ignored issues
show
Documentation introduced by
The property label does not exist on object<Thinktomorrow\Squanto\Domain\Line>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
32
        $line->page_id = Page::findOrCreateByKey($linekey->getPageKey())->id;
0 ignored issues
show
Documentation introduced by
The property page_id does not exist on object<Thinktomorrow\Squanto\Domain\Line>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
33
34
        $line->save();
35
36
        return $line;
37 12
    }
38
39 12
    /**
40 3
     * @param $key
41
     * @return Line
42
     */
43 12
    public static function findOrCreateByKey($key)
44
    {
45
        if ($line = self::findByKey($key)) {
46
            return $line;
47
        }
48
49
        return self::make($key);
50
    }
51
52
    /**
53 30
     * Save a translated value
54
     *
55 30
     * @param $locale
56
     * @param $value
57 30
     * @return $this
58
     */
59
    public function saveValue($locale, $value)
60
    {
61
        $this->saveTranslation($locale, 'value', $value);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Save a translated value
68
     *
69
     * @param $locale
70
     * @return $this
71
     */
72
    public function removeValue($locale)
73
    {
74
        $this->removeTranslation($locale);
75
76
        return $this;
77
    }
78 24
79
    /**
80 24
     * @param null $locale
81
     * @param bool $fallback
82
     * @return string
83
     */
84
    public function getValue($locale = null, $fallback = true)
85
    {
86
        return $this->getTranslationFor('value', $locale, $fallback);
87
    }
88
89
    public static function findValue($key, $locale)
90
    {
91
        if (!$line = self::findByKey($key)) {
92
            return null;
93
        }
94
95
        return $line->getValue($locale, false);
96 39
    }
97
98 39
    /**
99
     * @param $key
100
     * @return mixed
101 12
     */
102
    public static function findByKey($key)
103
    {
104 12
        return self::where('key', $key)->first();
105
    }
106
107
    public function saveSuggestedType()
108 12
    {
109 12
        // Based on first value found we will suggest a type
110 12
        if (!$value = $this->getValue()) {
111
            return;
112
        }
113
114
        $this->type = (new LineType($value))->suggest();
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<Thinktomorrow\Squanto\Domain\Line>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
115
        $this->save();
116
    }
117
118
    public function editInEditor()
119
    {
120
        return $this->type == LineType::EDITOR;
0 ignored issues
show
Documentation introduced by
The property type 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...
121
    }
122
123
    public function editInTextarea()
124
    {
125
        return $this->type == LineType::TEXTAREA;
0 ignored issues
show
Documentation introduced by
The property type 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...
126
    }
127
128
    public function editInTextinput()
129
    {
130
        return (!$this->type || $this->type == LineType::TEXT);
0 ignored issues
show
Documentation introduced by
The property type 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...
131
    }
132
133
    /**
134
     * Get key - value pairs for all lines per locale
135
     *
136
     * @param $locale
137
     * @return mixed
138
     */
139
    public static function getValuesByLocale($locale)
140
    {
141
        // Since the dimsav translatable model trait injects its behaviour and overwrites our results
142
        // with the current locale, we will need to fetch results straight from the db instead.
143
        $lines = DB::table('squanto_lines')->join('squanto_line_translations','squanto_lines.id','=','squanto_line_translations.line_id')
144
            ->select(['squanto_lines.*','squanto_line_translations.locale','squanto_line_translations.value'])
145
            ->where('squanto_line_translations.locale',$locale)
146
            ->get();
147
148
        return $lines->pluck('value', 'key')->toArray();
149
    }
150
}
151