Completed
Push — master ( b52359...45d2b6 )
by Ben
14:40 queued 09:32
created

Line::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1.037
1
<?php
2
3
namespace Thinktomorrow\Squanto\Domain;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Dimsav\Translatable\Translatable as BaseTranslatable;
7
8
/**
9
 * Class Line
10
 *
11
 * @package Thinktomorrow\Squanto\Domain
12
 */
13
class Line extends Model
14
{
15
    use BaseTranslatable, Translatable;
16
17
    public $table = 'squanto_lines';
18
    public $translatedAttributes = ['value'];
19 12
20
    /**
21 12
     * @param $key
22
     * @return Line
23 12
     */
24 36
    public static function make($key)
25 12
    {
26 36
        $linekey = new LineKey($key);
27
28 36
        $line = new self;
29 24
        $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...
30 36
        $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...
31 24
        $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...
32
33 24
        $line->save();
34
35 24
        return $line;
36
    }
37 4
38
    /**
39 4
     * @param $key
40 1
     * @return Line
41
     */
42 8
    public static function findOrCreateByKey($key)
43 4
    {
44 8
        if ($line = self::findByKey($key)) {
45 2
            return $line;
46
        }
47
48 8
        return self::make($key);
49
    }
50
51
    /**
52
     * Save a translated value
53 10
     *
54
     * @param $locale
55 10
     * @param $value
56
     * @return $this
57 10
     */
58 20
    public function saveValue($locale, $value)
59
    {
60 20
        $this->saveTranslation($locale, 'value', $value);
61
62 20
        return $this;
63
    }
64
65
    /**
66
     * Save a translated value
67
     *
68
     * @param $locale
69
     * @return $this
70
     */
71
    public function removeValue($locale)
72
    {
73
        $this->removeTranslation($locale);
74
75
        return $this;
76
    }
77
78 8
    /**
79
     * @param null $locale
80 8
     * @param bool $fallback
81
     * @return string
82
     */
83 16
    public function getValue($locale = null, $fallback = true)
84
    {
85 16
        return $this->getTranslationFor('value', $locale, $fallback);
86
    }
87
88
    public static function findValue($key, $locale)
89
    {
90
        if (!$line = self::findByKey($key)) {
91
            return null;
92
        }
93
94
        return $line->getValue($locale, false);
95
    }
96 13
97
    /**
98 13
     * @param $key
99
     * @return mixed
100
     */
101 30
    public static function findByKey($key)
102
    {
103 26
        return self::where('key', $key)->first();
104 4
    }
105
106 8
    public function saveSuggestedType()
107
    {
108 4
        // Based on first value found we will suggest a type
109 12
        if (!$value = $this->getValue()) {
110 4
            return;
111
        }
112
113 8
        $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...
114 8
        $this->save();
115 8
    }
116
117
    public function editInEditor()
118
    {
119
        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...
120
    }
121
122
    public function editInTextarea()
123
    {
124
        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...
125
    }
126
127
    public function editInTextinput()
128
    {
129
        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...
130
    }
131
}
132