Completed
Push — master ( dca802...7eb0de )
by Ryan
07:03
created

StreamModel::getAssignments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Anomaly\Streams\Platform\Stream;
2
3
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
4
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypeQuery;
5
use Anomaly\Streams\Platform\Assignment\AssignmentCollection;
6
use Anomaly\Streams\Platform\Assignment\AssignmentModel;
7
use Anomaly\Streams\Platform\Assignment\AssignmentModelTranslation;
8
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
9
use Anomaly\Streams\Platform\Collection\CacheCollection;
10
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
11
use Anomaly\Streams\Platform\Entry\EntryModel;
12
use Anomaly\Streams\Platform\Field\Contract\FieldInterface;
13
use Anomaly\Streams\Platform\Field\FieldModel;
14
use Anomaly\Streams\Platform\Field\FieldModelTranslation;
15
use Anomaly\Streams\Platform\Model\EloquentCollection;
16
use Anomaly\Streams\Platform\Model\EloquentModel;
17
use Anomaly\Streams\Platform\Stream\Command\CompileStream;
18
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
19
use Robbo\Presenter\PresentableInterface;
20
use Robbo\Presenter\Robbo;
21
22
/**
23
 * Class StreamModel
24
 *
25
 * @link    http://anomaly.is/streams-platform
26
 * @author  AnomalyLabs, Inc. <[email protected]>
27
 * @author  Ryan Thompson <[email protected]>
28
 * @package Anomaly\Streams\Platform\Stream
29
 */
30
class StreamModel extends EloquentModel implements StreamInterface, PresentableInterface
31
{
32
33
    /**
34
     * The cache minutes.
35
     *
36
     * @var int
37
     */
38
    protected $cacheMinutes = 99999;
39
40
    /**
41
     * The foreign key for translations.
42
     *
43
     * @var string
44
     */
45
    protected $translationForeignKey = 'stream_id';
46
47
    /**
48
     * The translation model.
49
     *
50
     * @var string
51
     */
52
    protected $translationModel = 'Anomaly\Streams\Platform\Stream\StreamModelTranslation';
53
54
    /**
55
     * Translatable attributes.
56
     *
57
     * @var array
58
     */
59
    protected $translatedAttributes = [
60
        'name',
61
        'description'
62
    ];
63
64
    /**
65
     * The model's database table name.
66
     *
67
     * @var string
68
     */
69
    protected $table = 'streams_streams';
70
71
    /**
72
     * The streams store.
73
     *
74
     * @var StreamStore
75
     */
76
    protected static $store;
77
78
    /**
79
     * Boot the model.
80
     */
81
    protected static function boot()
82
    {
83
        self::$store = app('Anomaly\Streams\Platform\Stream\StreamStore');
84
85
        parent::boot();
86
    }
87
88
    /**
89
     * Make a Stream instance from the provided compile data.
90
     *
91
     * @param  array $data
92
     * @return StreamInterface
93
     */
94
    public function make(array $data)
95
    {
96
        $payload = $data;
97
98
        if ($stream = self::$store->get($data)) {
99
            return $stream;
100
        }
101
102
        $assignments = array();
103
104
        $streamModel        = new StreamModel();
105
        $streamTranslations = new EloquentCollection();
106
107
        $data['config'] = serialize(array_get($data, 'config', []));
108
109
        if ($translations = array_pull($data, 'translations')) {
110
            foreach ($translations as $attributes) {
111
112
                $translation = new StreamModelTranslation();
113
                $translation->setRawAttributes($attributes);
114
115
                $streamTranslations->push($translation);
116
            }
117
        }
118
119
        $streamModel->setRawAttributes($data);
120
121
        $streamModel->setRelation('translations', $streamTranslations);
122
123
        unset($this->translations);
124
125
        if (array_key_exists('assignments', $data)) {
126
127
            foreach ($data['assignments'] as $assignment) {
128
129
                if (isset($assignment['field'])) {
130
131
                    $assignment['field']['config'] = unserialize($assignment['field']['config']);
132
133
                    $fieldModel        = new FieldModel();
134
                    $fieldTranslations = new EloquentCollection();
135
136 View Code Duplication
                    if (isset($assignment['field']['translations'])) {
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...
137
                        foreach (array_pull($assignment['field'], 'translations') as $attributes) {
138
139
                            $translation = new FieldModelTranslation();
140
                            $translation->setRawAttributes($attributes);
141
142
                            $fieldTranslations->push($translation);
143
                        }
144
                    }
145
146
                    $assignment['field']['config'] = serialize($assignment['field']['config']);
147
148
                    $fieldModel->setRawAttributes($assignment['field']);
149
150
                    $fieldModel->setRelation('translations', $fieldTranslations);
151
152
                    unset($assignment['field']);
153
154
                    $assignmentModel        = new AssignmentModel();
155
                    $assignmentTranslations = new EloquentCollection();
156
157 View Code Duplication
                    if (isset($assignment['translations'])) {
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...
158
                        foreach (array_pull($assignment, 'translations') as $attributes) {
159
160
                            $translation = new AssignmentModelTranslation();
161
                            $translation->setRawAttributes($attributes);
162
163
                            $assignmentTranslations->push($translation);
164
                        }
165
                    }
166
167
                    $assignmentModel->setRawAttributes($assignment);
168
                    $assignmentModel->setRawAttributes($assignment);
169
170
                    $assignmentModel->setRelation('field', $fieldModel);
171
                    $assignmentModel->setRelation('stream', $streamModel);
172
                    $assignmentModel->setRelation('translations', $assignmentTranslations);
173
174
                    $assignments[] = $assignmentModel;
175
                }
176
            }
177
        }
178
179
        $assignmentsCollection = new AssignmentCollection($assignments);
180
181
        $streamModel->setRelation('assignments', $assignmentsCollection);
182
183
        $streamModel->assignments = $assignmentsCollection;
0 ignored issues
show
Documentation introduced by
The property assignments does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
184
185
        self::$store->put($payload, $streamModel);
186
187
        return $streamModel;
188
    }
189
190
    /**
191
     * Compile the entry models.
192
     *
193
     * @return mixed
194
     */
195
    public function compile()
196
    {
197
        $this->dispatch(new CompileStream($this));
198
    }
199
200
    /**
201
     * Flush the entry stream's cache.
202
     *
203
     * @return StreamInterface
204
     */
205
    public function flushCache()
206
    {
207
        (new CacheCollection())->setKey($this->getCacheCollectionKey())->flush();
208
        (new CacheCollection())->setKey((new FieldModel())->getCacheCollectionKey())->flush();
209
        (new CacheCollection())->setKey((new AssignmentModel())->getCacheCollectionKey())->flush();
210
211
        return $this;
212
    }
213
214
    /**
215
     * Because the stream record holds translatable data
216
     * we have a conflict. The streams table has translations
217
     * but not all streams are translatable. This helps avoid
218
     * the translatable conflict during specific procedures.
219
     *
220
     * @param  array $attributes
221
     * @return static
222
     */
223
    public static function create(array $attributes = [])
224
    {
225
        $model = parent::create($attributes);
226
227
        $model->saveTranslations();
228
229
        return;
230
    }
231
232
    /**
233
     * Get the ID.
234
     *
235
     * @return mixed
236
     */
237
    public function getId()
238
    {
239
        return $this->getKey();
240
    }
241
242
    /**
243
     * Get the namespace.
244
     *
245
     * @return mixed
246
     */
247
    public function getNamespace()
248
    {
249
        return $this->namespace;
0 ignored issues
show
Documentation introduced by
The property namespace does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
250
    }
251
252
    /**
253
     * Get the slug.
254
     *
255
     * @return mixed
256
     */
257
    public function getSlug()
258
    {
259
        return $this->slug;
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
260
    }
261
262
    /**
263
     * Get the prefix.
264
     *
265
     * @return mixed
266
     */
267
    public function getPrefix()
268
    {
269
        return $this->prefix;
0 ignored issues
show
Documentation introduced by
The property prefix does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
270
    }
271
272
    /**
273
     * Get the name.
274
     *
275
     * @return string
276
     */
277
    public function getName()
278
    {
279
        return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
280
    }
281
282
    /**
283
     * Get the description.
284
     *
285
     * @return string
286
     */
287
    public function getDescription()
288
    {
289
        return $this->description;
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
290
    }
291
292
    /**
293
     * Get the view options.
294
     *
295
     * @return array
296
     */
297
    public function getConfig()
298
    {
299
        return $this->config;
0 ignored issues
show
Documentation introduced by
The property config does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
300
    }
301
302
    /**
303
     * Get the locked flag.
304
     *
305
     * @return bool
306
     */
307
    public function isLocked()
308
    {
309
        return $this->locked;
0 ignored issues
show
Documentation introduced by
The property locked does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
310
    }
311
312
    /**
313
     * Get the hidden flag.
314
     *
315
     * @return bool
316
     */
317
    public function isHidden()
318
    {
319
        return $this->hidden;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->hidden; (array) is incompatible with the return type declared by the interface Anomaly\Streams\Platform...reamInterface::isHidden of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
320
    }
321
322
    /**
323
     * Get the sortable flag.
324
     *
325
     * @return bool
326
     */
327
    public function isSortable()
328
    {
329
        return $this->sortable;
0 ignored issues
show
Documentation introduced by
The property sortable does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
330
    }
331
332
    /**
333
     * Get the trashable flag.
334
     *
335
     * @return bool
336
     */
337
    public function isTrashable()
338
    {
339
        return $this->trashable;
0 ignored issues
show
Documentation introduced by
The property trashable does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
340
    }
341
342
    /**
343
     * Get the translatable flag.
344
     *
345
     * @return bool
346
     */
347
    public function isTranslatable()
348
    {
349
        return $this->translatable;
0 ignored issues
show
Documentation introduced by
The property translatable does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
350
    }
351
352
    /**
353
     * Get the title column.
354
     *
355
     * @return mixed
356
     */
357
    public function getTitleColumn()
358
    {
359
        return $this->title_column;
0 ignored issues
show
Documentation introduced by
The property title_column does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
360
    }
361
362
    /**
363
     * Get the title field.
364
     *
365
     * @return null|FieldInterface
366
     */
367
    public function getTitleField()
368
    {
369
        return $this->getField($this->getTitleColumn());
370
    }
371
372
    /**
373
     * Get the related assignments.
374
     *
375
     * @return AssignmentCollection
376
     */
377
    public function getAssignments()
378
    {
379
        return $this->assignments;
0 ignored issues
show
Documentation introduced by
The property assignments does not exist on object<Anomaly\Streams\P...orm\Stream\StreamModel>. 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...
380
    }
381
382
    /**
383
     * Get the field slugs for assigned fields.
384
     *
385
     * @param null $prefix
386
     * @return array
387
     */
388
    public function getAssignmentFieldSlugs($prefix = null)
389
    {
390
        $assignments = $this->getAssignments();
391
392
        return $assignments->fieldSlugs($prefix);
393
    }
394
395
    /**
396
     * Get the related date assignments.
397
     *
398
     * @return AssignmentCollection
399
     */
400
    public function getDateAssignments()
401
    {
402
        $assignments = $this->getAssignments();
403
404
        return $assignments->dates();
405
    }
406
407
    /**
408
     * Get the unique translatable assignments.
409
     *
410
     * @return AssignmentCollection
411
     */
412
    public function getUniqueAssignments()
413
    {
414
        $assignments = $this->getAssignments();
415
416
        return $assignments->indexed();
417
    }
418
419
    /**
420
     * Get the only required assignments.
421
     *
422
     * @return AssignmentCollection
423
     */
424
    public function getRequiredAssignments()
425
    {
426
        $assignments = $this->getAssignments();
427
428
        return $assignments->required();
429
    }
430
431
    /**
432
     * Get the related translatable assignments.
433
     *
434
     * @return AssignmentCollection
435
     */
436
    public function getTranslatableAssignments()
437
    {
438
        $assignments = $this->getAssignments();
439
440
        return $assignments->translatable();
441
    }
442
443
    /**
444
     * Get the related relationship assignments.
445
     *
446
     * @return AssignmentCollection
447
     */
448
    public function getRelationshipAssignments()
449
    {
450
        $assignments = $this->getAssignments();
451
452
        return $assignments->relations();
453
    }
454
455
    /**
456
     * Get an assignment by it's field's slug.
457
     *
458
     * @param  $fieldSlug
459
     * @return AssignmentInterface
460
     */
461
    public function getAssignment($fieldSlug)
462
    {
463
        return $this->getAssignments()->findByFieldSlug($fieldSlug);
464
    }
465
466
    /**
467
     * Return whether a stream
468
     * has a field assigned.
469
     *
470
     * @param $fieldSlug
471
     * @return bool
472
     */
473
    public function hasAssignment($fieldSlug)
474
    {
475
        return (bool)$this->getAssignment($fieldSlug);
476
    }
477
478
    /**
479
     * Get a stream field by it's slug.
480
     *
481
     * @param  $slug
482
     * @return mixed
483
     */
484
    public function getField($slug)
485
    {
486
        if (!$assignment = $this->getAssignment($slug)) {
487
            return null;
488
        }
489
490
        return $assignment->getField();
491
    }
492
493
    /**
494
     * Get a field's type by the field's slug.
495
     *
496
     * @param                $fieldSlug
497
     * @param EntryInterface $entry
498
     * @param null|string    $locale
499
     * @return FieldType
500
     */
501
    public function getFieldType($fieldSlug, EntryInterface $entry = null, $locale = null)
502
    {
503
        if (!$assignment = $this->getAssignment($fieldSlug)) {
504
            return null;
505
        }
506
507
        return $assignment->getFieldType($entry, $locale);
0 ignored issues
show
Documentation introduced by
$entry is of type null|object<Anomaly\Stre...ontract\EntryInterface>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to AssignmentInterface::getFieldType() has too many arguments starting with $locale.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
508
    }
509
510
    /**
511
     * Get a field's query utility by the field's slug.
512
     *
513
     * @param                $fieldSlug
514
     * @param EntryInterface $entry
515
     * @param null|string    $locale
516
     * @return FieldTypeQuery
517
     */
518
    public function getFieldTypeQuery($fieldSlug, EntryInterface $entry = null, $locale = null)
519
    {
520
        if (!$fieldType = $this->getFieldType($fieldSlug, $entry, $locale)) {
521
            return null;
522
        }
523
524
        return $fieldType->getQuery();
525
    }
526
527
    /**
528
     * Get the entry table name.
529
     *
530
     * @return string
531
     */
532
    public function getEntryTableName()
533
    {
534
        return $this->getPrefix() . $this->getSlug();
535
    }
536
537
    /**
538
     * Get the entry translations table name.
539
     *
540
     * @return string
541
     */
542
    public function getEntryTranslationsTableName()
543
    {
544
        return $this->getEntryTableName() . '_translations';
545
    }
546
547
    /**
548
     * Get the entry model.
549
     *
550
     * @return EntryModel
551
     */
552
    public function getEntryModel()
553
    {
554
        return app($this->getEntryModelName());
555
    }
556
557
    /**
558
     * Get the entry name.
559
     *
560
     * @return EntryModel
561
     */
562
    public function getEntryModelName()
563
    {
564
        $slug      = ucfirst(camel_case($this->getSlug()));
565
        $namespace = ucfirst(camel_case($this->getNamespace()));
566
567
        return "Anomaly\\Streams\\Platform\\Model\\{$namespace}\\{$namespace}{$slug}EntryModel";
568
    }
569
570
    /**
571
     * Get the foreign key.
572
     *
573
     * @return string
574
     */
575
    public function getForeignKey()
576
    {
577
        return str_singular($this->getSlug()) . '_id';
578
    }
579
580
    /**
581
     * Set the config attribute.
582
     *
583
     * @param $config
584
     */
585
    public function setConfigAttribute($config)
586
    {
587
        $this->attributes['config'] = serialize($config);
588
    }
589
590
    /**
591
     * Get the config attribute.
592
     *
593
     * @param  $viewOptions
594
     * @return mixed
595
     */
596
    public function getConfigAttribute($config)
597
    {
598
        return unserialize($config);
599
    }
600
601
    /**
602
     * Set the locked attribute.
603
     *
604
     * @param $locked
605
     */
606
    public function setLockedAttribute($locked)
607
    {
608
        $this->attributes['locked'] = filter_var($locked, FILTER_VALIDATE_BOOLEAN);
609
    }
610
611
    /**
612
     * Set the hidden attribute.
613
     *
614
     * @param $hidden
615
     */
616
    public function setHiddenAttribute($hidden)
617
    {
618
        $this->attributes['hidden'] = filter_var($hidden, FILTER_VALIDATE_BOOLEAN);
619
    }
620
621
    /**
622
     * Set the sortable attribute.
623
     *
624
     * @param $sortable
625
     */
626
    public function setSortableAttribute($sortable)
627
    {
628
        $this->attributes['sortable'] = filter_var($sortable, FILTER_VALIDATE_BOOLEAN);
629
    }
630
631
    /**
632
     * Set the trashable attribute.
633
     *
634
     * @param $trashable
635
     */
636
    public function setTrashableAttribute($trashable)
637
    {
638
        $this->attributes['trashable'] = filter_var($trashable, FILTER_VALIDATE_BOOLEAN);
639
    }
640
641
    /**
642
     * Set the translatable attribute.
643
     *
644
     * @param $translatable
645
     */
646
    public function setTranslatableAttribute($translatable)
647
    {
648
        $this->attributes['translatable'] = filter_var($translatable, FILTER_VALIDATE_BOOLEAN);
649
    }
650
651
    /**
652
     * Return a created presenter.
653
     *
654
     * @return \Robbo\Presenter\Presenter
655
     */
656
    public function getPresenter()
657
    {
658
        return new StreamPresenter($this);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Anomaly\Stre...StreamPresenter($this); (Anomaly\Streams\Platform\Stream\StreamPresenter) is incompatible with the return type declared by the interface Robbo\Presenter\PresentableInterface::getPresenter of type Robbo\Presenter\Robbo\Presenter\Presenter.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
659
    }
660
661
    /**
662
     * Return the assignments relation.
663
     *
664
     * @return mixed
665
     */
666
    public function assignments()
667
    {
668
        return $this->hasMany(
669
            'Anomaly\Streams\Platform\Assignment\AssignmentModel',
670
            'stream_id'
671
        )->orderBy('sort_order');
672
    }
673
}
674