Document::countOpeners()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 3
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\Documents;
29
30
use Angelov\Storgman\Members\Member;
31
use Angelov\Storgman\Documents\Tags\Tag;
32
use Illuminate\Database\Eloquent\Model;
33
34
class Document extends Model
35
{
36
    protected $table = "documents";
37
38
    protected $tagList = [];
39
    protected $openedByList = [];
40
41
    public function getId()
42
    {
43
        return $this->getAttribute('id');
44
    }
45
46
    public function getTitle()
47
    {
48
        return $this->getAttribute('title');
49
    }
50
51
    public function setTitle($title)
52
    {
53
        $this->setAttribute('title', $title);
54
    }
55
56
    public function getDescription()
57
    {
58
        return $this->getAttribute('description');
59
    }
60
61
    public function setDescription($description)
62
    {
63
        $this->setAttribute('description', $description);
64
    }
65
66
    public function getUrl()
67
    {
68
        return $this->getAttribute('url');
69
    }
70
71
    public function setUrl($url)
72
    {
73
        $this->setAttribute('url', $url);
74
    }
75
76
    public function submitter()
77
    {
78
        return $this->belongsTo(Member::class, 'submitted_by');
79
    }
80
81
    /**
82
     * @return Member
83
     */
84
    public function getSubmitter()
85
    {
86
        return $this->submitter;
0 ignored issues
show
Documentation introduced by
The property submitter does not exist on object<Angelov\Storgman\Documents\Document>. 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
    /**
90
     * @param Member $member
91
     */
92
    public function setSubmitter(Member $member)
93
    {
94
        $this->submitter()->associate($member);
95
    }
96
97
    public function openedBy()
98
    {
99
        return $this->belongsToMany(Member::class, 'document_openings')->withTimestamps();
100
    }
101
102
    public function addOpener(Member $member)
103
    {
104
        $this->openedByList[] = $member;
105
    }
106
107
    /**
108
     * @return Member[]
109
     */
110
    public function getOpeners()
111
    {
112
        return $this->openedBy->all();
0 ignored issues
show
Bug introduced by
The property openedBy does not seem to exist. Did you mean openedByList?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
113
    }
114
115
    public function countOpenings()
116
    {
117
        return $this->openedBy->count();
0 ignored issues
show
Bug introduced by
The property openedBy does not seem to exist. Did you mean openedByList?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
118
    }
119
120
    public function countOpeners()
121
    {
122
        $openers = $this->getOpeners();
123
        $counted = [];
124
125
        foreach ($openers as $opener) {
126
            if (! in_array($opener->getId(), $counted)) {
127
                $counted[] = $opener->getId();
128
            }
129
        }
130
131
        return count($counted);
132
    }
133
134
    public function tags()
135
    {
136
        return $this->belongsToMany(Tag::class);
137
    }
138
139
    /**
140
     * @param Tag $tag
141
     */
142
    public function addTag(Tag $tag)
143
    {
144
        $this->tagList[] = $tag;
145
    }
146
147
    /**
148
     * @return Tag[]
149
     */
150
    public function getTags()
151
    {
152
        return $this->tags;
0 ignored issues
show
Documentation introduced by
The property tags does not exist on object<Angelov\Storgman\Documents\Document>. 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...
153
    }
154
155
    /**
156
     * @return \DateTime
157
     */
158
    public function getCreatedAt()
159
    {
160
        return $this->getAttribute('created_at');
161
    }
162
163
    /**
164
     * @return \DateTime
165
     */
166
    public function getUpdatedAt()
167
    {
168
        return $this->getAttribute('updated_at');
169
    }
170
171
    /**
172
     * By default, all members have access to the document
173
     */
174
    public function setVisibleToAllMembers()
175
    {
176
        $this->setAttribute('board_only', false);
177
    }
178
179
    /**
180
     * @return bool
181
     */
182
    public function isVisibleToAllMembers()
183
    {
184
        return (! $this->getAttribute('board_only'));
185
    }
186
187
    public function setVisibleToBoardOnly()
188
    {
189
        $this->setAttribute('board_only', true);
190
    }
191
192
    public function save(array $options = [])
193
    {
194
        parent::save($options);
195
196
        foreach ($this->tagList as $tag) {
197
            $this->tags()->attach($tag);
198
        }
199
200
        foreach ($this->openedByList as $opener) {
201
            $this->openedBy()->attach($opener);
202
        }
203
    }
204
}
205