Completed
Pull Request — master (#12)
by Philippe
12:17
created

Line::removeValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Collection;
8
use Illuminate\Support\Facades\DB;
9
10
/**
11
 * Class Line
12
 *
13
 * @package Thinktomorrow\Squanto\Domain
14
 */
15
class Line extends Model
16
{
17
    use BaseTranslatable, Translatable;
18
19
    public $table = 'squanto_lines';
20
    public $translatedAttributes = ['value'];
21
22
    /**
23
     * @param $key
24
     * @return Line
25
     */
26 27
    public static function make($key)
27
    {
28 27
        $linekey = new LineKey($key);
29
30 27
        $line = new self;
31 27
        $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...
32 27
        $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...
33 27
        $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...
34
35 27
        $line->save();
36
37 27
        return $line;
38
    }
39
40
    /**
41
     * @param $key
42
     * @return Line
43
     */
44 3
    public static function findOrCreateByKey($key)
45
    {
46 3
        if ($line = self::findByKey($key)) {
47
            return $line;
48
        }
49
50 3
        return self::make($key);
51
    }
52
53
    /**
54
     * Save a translated value
55
     *
56
     * @param $locale
57
     * @param $value
58
     * @return $this
59
     */
60 25
    public function saveValue($locale, $value)
61
    {
62 25
        $this->saveTranslation($locale, 'value', $value);
63
64 25
        return $this;
65
    }
66
67
    /**
68
     * Save a translated value
69
     *
70
     * @param $locale
71
     * @return $this
72
     */
73
    public function removeValue($locale)
74
    {
75
        $this->removeTranslation($locale);
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param null $locale
82
     * @param bool $fallback
83
     * @return string
84
     */
85 12
    public function getValue($locale = null, $fallback = true)
86
    {
87 12
        return $this->getTranslationFor('value', $locale, $fallback);
88
    }
89
90
    public static function findValue($key, $locale)
91
    {
92
        if (!$line = self::findByKey($key)) {
93
            return null;
94
        }
95
96
        return $line->getValue($locale, false);
97
    }
98
99
    /**
100
     * @param $key
101
     * @return mixed
102
     */
103 20
    public static function findByKey($key)
104
    {
105 20
        return self::where('key', $key)->first();
106
    }
107
108 6
    public function saveSuggestedType()
109
    {
110
        // Based on first value found we will suggest a type
111 6
        if (!$value = $this->getValue()) {
112
            return;
113
        }
114
115 6
        $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...
116 6
        $this->save();
117 6
    }
118
119 5
    public function editInEditor()
120
    {
121 5
        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...
122
    }
123
124
    public function editInTextarea()
125
    {
126
        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...
127
    }
128
129
    public function editInTextinput()
130
    {
131
        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...
132
    }
133
134 5
    public function areParagraphsAllowed()
135
    {
136 5
        return (false !== strpos($this->allowed_html, '<p>'));
0 ignored issues
show
Documentation introduced by
The property allowed_html 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...
137
    }
138
139
    /**
140
     * Get key - value pairs for all lines per locale
141
     *
142
     * @param $locale
143
     * @return mixed
144
     */
145 9
    public static function getValuesByLocale($locale)
146
    {
147 9
        return self::getValuesByLocaleAndPage($locale);
148
    }
149
150 14
    public static function getValuesByLocaleAndPage($locale, $pagekey = null)
151
    {
152 14
        $locale = $locale?: app()->getLocale();
153
154
        // Since the dimsav translatable model trait injects its behaviour and overwrites our results
155
        // with the current locale, we will need to fetch results straight from the db instead.
156 14
        $lines = DB::table('squanto_lines')
157 14
            ->join('squanto_line_translations', 'squanto_lines.id', '=', 'squanto_line_translations.line_id')
158 14
            ->select(['squanto_lines.*','squanto_line_translations.locale','squanto_line_translations.value'])
159 14
            ->where('squanto_line_translations.locale', $locale);
160
161 14
        if ($pagekey) {
162
            $lines = $lines
163 5
                ->join('squanto_pages', 'squanto_lines.page_id', '=', 'squanto_pages.id')
164 5
                ->where('squanto_pages.key', $pagekey);
165
        }
166
167 14
        $lines = $lines->get();
168
169
        // Assert we have a collection
170 14
        $lines = $lines instanceof Collection ? $lines : collect($lines);
171
172 14
        return $lines->pluck('value', 'key')->toArray();
173
    }
174
}
175