View   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 22
c 1
b 0
f 0
dl 0
loc 113
ccs 20
cts 20
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A folder() 0 3 2
A space() 0 3 2
A list() 0 3 2
A team() 0 3 2
A getProtectedByAttribute() 0 3 1
A tasks() 0 9 2
A comments() 0 9 2
1
<?php
2
3
namespace Spinen\ClickUp;
4
5
use Carbon\Carbon;
6
use Spinen\ClickUp\Exceptions\InvalidRelationshipException;
7
use Spinen\ClickUp\Exceptions\ModelNotFoundException;
8
use Spinen\ClickUp\Exceptions\NoClientException;
9
use Spinen\ClickUp\Support\Model;
10
use Spinen\ClickUp\Support\Relations\ChildOf;
11
use Spinen\ClickUp\Support\Relations\HasMany;
12
13
/**
14
 * Class View
15
 *
16
 * @package Spinen\ClickUp
17
 *
18
 * @property array $columns
19
 * @property array $divide
20
 * @property array $filters
21
 * @property array $grouping
22
 * @property array $parent
23
 * @property array $settings
24
 * @property array $sorting
25
 * @property array $team_sidebar
26
 * @property boolean $protected
27
 * @property Carbon $date_created
28
 * @property Carbon $date_protected
29
 * @property float $orderindex
30
 * @property integer $creator
31
 * @property Member $protected_by
32
 * @property string $id
33
 * @property string $name
34
 * @property string $protected_note
35
 * @property string $type
36
 * @property string $visibility
37
 */
38
class View extends Model
39
{
40
    /**
41
     * The attributes that should be cast to native types.
42
     *
43
     * @var array
44
     */
45
    protected $casts = [
46
        'date_created'   => 'datetime:Uv',
47
        'date_protected' => 'integer',
48
        'id'             => 'string',
49
        'orderindex'     => 'float',
50
        'protected'      => 'boolean',
51
    ];
52
53
    /**
54
     * Path to API endpoint.
55
     *
56
     * @var string
57
     */
58
    protected $path = '/view';
59
60
    /**
61
     * @return HasMany
62
63
     * @throws InvalidRelationshipException
64
     * @throws ModelNotFoundException
65
     * @throws NoClientException
66
     */
67 2
    public function comments(): HasMany
68
    {
69 2
        if ($this->type !== 'conversation') {
70 1
            throw new InvalidRelationshipException(
71 1
                sprintf('The view is of type [%s], but must be of type [conversation] to have comments.', $this->type)
72
            );
73
        }
74
75 1
        return $this->hasMany(Comment::class);
76
    }
77
78
    /**
79
     * @return ChildOf
80
     * @throws InvalidRelationshipException
81
     * @throws ModelNotFoundException
82
     * @throws NoClientException
83
     */
84 2
    public function folder(): ?ChildOf
85
    {
86 2
        return is_a($this->parentModel, Folder::class) ? $this->childOf(Folder::class) : null;
87
    }
88
89
    /**
90
     * Accessor for ProtectedBy.
91
     *
92
     * @param array $protected_by
93
     *
94
     * @return Member
95
     * @throws NoClientException
96
     */
97 1
    public function getProtectedByAttribute($protected_by): Member
98
    {
99 1
        return $this->givenOne(Member::class, $protected_by);
100
    }
101
102
    /**
103
     * @return ChildOf
104
     * @throws InvalidRelationshipException
105
     * @throws ModelNotFoundException
106
     * @throws NoClientException
107
     */
108 2
    public function list(): ?ChildOf
109
    {
110 2
        return is_a($this->parentModel, TaskList::class) ? $this->childOf(TaskList::class) : null;
111
    }
112
113
    /**
114
     * @return ChildOf
115
     * @throws InvalidRelationshipException
116
     * @throws ModelNotFoundException
117
     * @throws NoClientException
118
     */
119 2
    public function space(): ?ChildOf
120
    {
121 2
        return is_a($this->parentModel, Space::class) ? $this->childOf(Space::class) : null;
122
    }
123
124
    /**
125
     * @return HasMany
126
127
     * @throws InvalidRelationshipException
128
     * @throws ModelNotFoundException
129
     * @throws NoClientException
130
     */
131 4
    public function tasks(): HasMany
132
    {
133 4
        if (in_array($this->type, ['conversation', 'doc', 'embed'])) {
134 3
            throw new InvalidRelationshipException(
135 3
                sprintf('The view is of type [%s], but must be of on of the task types to have tasks.', $this->type)
136
            );
137
        }
138
139 1
        return $this->hasMany(Task::class);
140
    }
141
142
    /**
143
     * @return ChildOf
144
     * @throws InvalidRelationshipException
145
     * @throws ModelNotFoundException
146
     * @throws NoClientException
147
     */
148 2
    public function team(): ?ChildOf
149
    {
150 2
        return is_a($this->parentModel, Team::class) ? $this->childOf(Team::class) : null;
151
    }
152
}
153