Completed
Push — master ( 52052f...b5e4b5 )
by Ben
04:12
created

Line   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 63.16%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 119
ccs 24
cts 38
cp 0.6316
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A editInEditor() 0 4 1
A editInTextarea() 0 4 1
A make() 0 13 1
A findOrCreateByKey() 0 8 2
A saveValue() 0 6 1
A removeValue() 0 6 1
A getValue() 0 4 1
A findValue() 0 8 2
A findByKey() 0 4 1
A saveSuggestedType() 0 10 2
A editInTextinput() 0 4 2
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 36
20
    /**
21 36
     * @param $key
22
     * @return Line
23 36
     */
24 36
    public static function make($key)
25 36
    {
26 36
        $linekey = new LineKey($key);
27
28 36
        $line = new self;
29
        $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
        $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
        $line->save();
34
35
        return $line;
36
    }
37 12
38
    /**
39 12
     * @param $key
40 3
     * @return Line
41
     */
42
    public static function findOrCreateByKey($key)
43 12
    {
44
        if ($line = self::findByKey($key)) {
45
            return $line;
46
        }
47
48
        return self::make($key);
49
    }
50
51
    /**
52
     * Save a translated value
53 30
     *
54
     * @param $locale
55 30
     * @param $value
56
     * @return $this
57 30
     */
58
    public function saveValue($locale, $value)
59
    {
60
        $this->saveTranslation($locale, 'value', $value);
61
62
        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 24
    /**
79
     * @param null $locale
80 24
     * @param bool $fallback
81
     * @return string
82
     */
83
    public function getValue($locale = null, $fallback = true)
84
    {
85
        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 39
97
    /**
98 39
     * @param $key
99
     * @return mixed
100
     */
101 12
    public static function findByKey($key)
102
    {
103
        return self::where('key', $key)->first();
104 12
    }
105
106
    public function saveSuggestedType()
107
    {
108 12
        // Based on first value found we will suggest a type
109 12
        if (!$value = $this->getValue()) {
110 12
            return;
111
        }
112
113
        $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
        $this->save();
115
    }
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