Completed
Push — master ( efae37...460051 )
by Brandon
19s
created

Location::createLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
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 9
ccs 0
cts 6
cp 0
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
crap 2
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
8
class Location extends Model
9
{
10
    /**
11
     * The attributes that are mass assignable.
12
     *
13
     * @var array
14
     */
15
    protected $fillable = [
16
        'name', 'site_id'
17
    ];
18
    
19
    public $timestamps = false;
20
    
21
    /**
22
     * Get the site for the location
23
     */
24
    public function site()
25
    {
26
        return $this->belongsTo('App\Site', 'site_id');
27
    }
28
    
29
    /**
30
     * Get devices for the location
31
     */
32
    public function devices()
33
    {
34
        return $this->hasMany('App\Device');
35
    }
36
    
37
    /**
38
     * Get all the locations with the supplied site id
39
     *
40
     * @param int $site_id
41
     * @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
42
     */
43
    public function getLocationsBasedOnSite($site_id)
44
    {
45
        return self::where('site_id', $site_id)->get();
46
    }
47
    
48
    /**
49
     * Get the locations that are related to the supplied site id and sort the locations
50
     * starting with the supplied location id
51
     *
52
     * @param int $site_id
53
     * @param int $location_id
54
     * @return Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
55
     */
56 View Code Duplication
    public function orderedSiteLocationsBy($site_id, $location_id)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
57
    {
58
        $locations = self::query()->select(['id', 'name'])
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
59
                                    ->where('site_id', '=', $site_id)
60
                                    ->orderByRaw(DB::raw("(id = " . $location_id . ") DESC"))
61
                                    ->get();
62
        return $locations;
63
    }
64
    
65
    /**
66
     * Create a new location and return its id
67
     *
68
     * @param string $name
69
     * @param int $site_id
70
     * @return int $id
71
     */
72
    public function createLocation($name, $site_id)
73
    {
74
        $location = new Location;
75
        $location->name = $name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Location>. 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...
76
        $location->site_id = $site_id;
0 ignored issues
show
Documentation introduced by
The property site_id does not exist on object<App\Location>. 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...
77
        $location->save();
78
        
79
        return $location->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Location>. 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...
80
    }
81
}
82