Completed
Push — master ( 6cc2de...ecdd96 )
by Adrien
02:40
created

Post::isLong()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLTests\Doctrine\Blog\Model;
6
7
use DateTime;
8
use Doctrine\ORM\Mapping as ORM;
9
use GraphQL\Doctrine\Annotation as API;
10
11
/**
12
 * A blog post with title and body
13
 *
14
 * @ORM\Entity
15
 */
16
class Post extends AbstractModel
17
{
18
    const STATUS_PRIVATE = 'private';
19
    const STATUS_PUBLIC = 'public';
20
21
    /**
22
     * @var string
23
     *
24
     * @ORM\Column(type="string", length=50, options={"default" = ""})
25
     */
26
    private $title = '';
27
28
    /**
29
     * @var string
30
     *
31
     * @ORM\Column(type="text")
32
     */
33
    private $body = '';
34
35
    /**
36
     * @var DateTime
37
     *
38
     * @ORM\Column(type="datetime")
39
     */
40
    private $publicationDate;
41
42
    /**
43
     * @var string
44
     *
45
     * @ORM\Column(type="string", options={"default" = Post::STATUS_PRIVATE})
46
     */
47
    private $status = self::STATUS_PRIVATE;
48
49
    /**
50
     * @var User
51
     *
52
     * @ORM\ManyToOne(targetEntity="GraphQLTests\Doctrine\Blog\Model\User", inversedBy="posts")
53
     */
54
    private $user;
55
56
    /**
57
     * Set title
58
     *
59
     * @param string $title
60
     */
61
    public function setTitle(string $title): void
62
    {
63
        $this->title = $title;
64
    }
65
66
    /**
67
     * Get title
68
     *
69
     * @return string
70
     */
71
    public function getTitle(): string
72
    {
73
        return $this->title;
74
    }
75
76
    /**
77
     * Set the body
78
     *
79
     * @param string $body
80
     */
81
    public function setBody(string $body): void
82
    {
83
        $this->body = $body;
84
    }
85
86
    /**
87
     * Returns the body
88
     *
89
     * @API\Field(name="content", description="The post content")
90
     *
91
     * @return string
92
     */
93
    public function getBody(): string
94
    {
95
        return $this->body;
96
    }
97
98
    /**
99
     * Set status
100
     *
101
     * @API\Input(type="GraphQLTests\Doctrine\Blog\Types\PostStatusType")
102
     *
103
     * @param string $status
104
     */
105
    public function setStatus(string $status = self::STATUS_PUBLIC): void
106
    {
107
        $this->status = $status;
108
    }
109
110
    /**
111
     * Get status
112
     *
113
     * @API\Field(type="GraphQLTests\Doctrine\Blog\Types\PostStatusType")
114
     *
115
     * @return string
116
     */
117
    public function getStatus(): string
118
    {
119
        return $this->status;
120
    }
121
122
    /**
123
     * Set author of post
124
     *
125
     * @param User $user
126
     */
127
    public function setUser(User $user): void
128
    {
129
        $this->user = $user;
130
    }
131
132
    /**
133
     * Get author of post
134
     *
135
     * @return User
136
     */
137
    public function getUser(): User
138
    {
139
        return $this->user;
140
    }
141
142
    /**
143
     * Set date of publication
144
     */
145
    public function setPublicationDate(DateTime $publicationDate): void
146
    {
147
        $this->publicationDate = $publicationDate;
148
    }
149
150
    /**
151
     * Get date of publication
152
     *
153
     * @return DateTime
154
     */
155
    public function getPublicationDate(): DateTime
156
    {
157
        return $this->publicationDate;
158
    }
159
160
    /**
161
     * @return string[]
162
     */
163
    public function getWords(): array
164
    {
165
        return explode(' ', $this->getBody());
166
    }
167
168
    /**
169
     * @param string[] $words
170
     *
171
     * @return bool
172
     */
173
    public function hasWords($words): bool
174
    {
175
        return count(array_diff($words, $this->getWords())) > 0;
176
    }
177
178
    public function isLong(int $wordLimit = 50): bool
179
    {
180
        return count($this->getWords()) > $wordLimit;
181
    }
182
183
    public function isAllowedEditing(User $user): bool
184
    {
185
        return $this->getUser() === $user;
186
    }
187
188
    /**
189
     * This should be silently ignored
190
     */
191
    public function setNothing(): void
192
    {
193
    }
194
}
195