Attachment::getOwner()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Meetings\Attachments;
29
30
use Angelov\Storgman\Meetings\Meeting;
31
use Angelov\Storgman\Members\Member;
32
use Illuminate\Database\Eloquent\Model;
33
34
class Attachment extends Model
35
{
36
    protected $table = "meeting_attachments";
37
38
    public function getId()
39
    {
40
        return $this->getAttribute('id');
41
    }
42
43
    public function getFilename()
44
    {
45
        return $this->getAttribute('filename');
46
    }
47
48
    public function setFilename($filename)
49
    {
50
        $this->setAttribute("filename", $filename);
51
    }
52
53
    public function getStorageFilename()
54
    {
55
        return $this->getAttribute('storage_filename');
56
    }
57
58
    public function setStorageFilename($filename)
59
    {
60
        $this->setAttribute('storage_filename', $filename);
61
    }
62
63
    public function getSize()
64
    {
65
        $value = $this->getAttribute('size');
66
        $size = new FileSize($value, FileSize::UNIT_KILOBYTE);
67
68
        return $size;
69
    }
70
71
    public function setSize($size)
72
    {
73
        $this->setAttribute('size', $size);
74
    }
75
76
    public function meeting()
77
    {
78
        return $this->belongsTo(Meeting::class, 'meeting_id');
79
    }
80
81
    /**
82
     * @return Meeting
83
     */
84
    public function getMeeting()
85
    {
86
        return $this->meeting;
0 ignored issues
show
Documentation introduced by
The property meeting does not exist on object<Angelov\Storgman\...Attachments\Attachment>. 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...
87
    }
88
89
    public function setMeeting(Meeting $meeting = null)
90
    {
91
        $this->meeting()->associate($meeting);
0 ignored issues
show
Bug introduced by
It seems like $meeting defined by parameter $meeting on line 89 can be null; however, Illuminate\Database\Eloq...\BelongsTo::associate() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
92
    }
93
94
    public function owner()
95
    {
96
        return $this->belongsTo(Member::class, 'owner_id');
97
    }
98
99
    /**
100
     * @return Member
101
     */
102
    public function getOwner()
103
    {
104
        return $this->owner;
0 ignored issues
show
Documentation introduced by
The property owner does not exist on object<Angelov\Storgman\...Attachments\Attachment>. 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...
105
    }
106
107
    public function setOwner(Member $owner)
108
    {
109
        $this->owner()->associate($owner);
110
    }
111
112
    /**
113
     * @return \DateTime
114
     */
115
    public function getCreatedAt()
116
    {
117
        return $this->getAttribute('created_at');
118
    }
119
}
120