Failed Conditions
Pull Request — master (#63)
by Adrien
14:14
created

User::getManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLTests\Doctrine\AttributeBlog\Model;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping as ORM;
10
use GraphQL\Doctrine\Annotation as API;
11
use GraphQLTests\Doctrine\Blog\Repository\UserRepository;
12
13
/** A blog author or visitor. */
14
#[ORM\Entity(repositoryClass: UserRepository::class)]
15
final class User extends AbstractModel
16
{
17
    #[ORM\Column(name: 'custom_column_name', type: 'string', length: 50, options: ['default' => ''])]
18
    private string $name = '';
19
20
    #[ORM\Column(type: 'string', length: 50, nullable: true)]
21
    private ?string $email = null;
22
23
    #[ORM\Column(name: 'password', type: 'string', length: 255)]
24
    #[API\Exclude]
25
    private string $password;
26
27
    #[ORM\Column(type: 'boolean', options: ['default' => false])]
28
    private bool $isAdministrator = false;
29
30
    /** @var Collection<Post> */
31
    #[ORM\OneToMany(mappedBy: 'user', targetEntity: Post::class)]
32
    private Collection $posts;
33
34
    /** @var Collection<Post> */
35
    #[ORM\ManyToMany(targetEntity: Post::class)]
36
    private Collection $favoritePosts;
0 ignored issues
show
introduced by
The private property $favoritePosts is not used, and could be removed.
Loading history...
37
38
    #[ORM\ManyToOne(targetEntity: self::class)]
39
    private ?User $manager = null;
40
41
    /**
42
     * Constructor.
43
     */
44
    public function __construct(?int $id = null)
45
    {
46
        // This is a bad idea in real world, but we are just testing stuff here
47
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
48
            $this->id = $id;
49
        }
50
51
        $this->posts = new ArrayCollection();
52
    }
53
54
    /**
55
     * Set name.
56
     */
57
    public function setName(string $name): void
58
    {
59
        $this->name = $name;
60
    }
61
62
    /**
63
     * Get the user real name.
64
     */
65
    public function getName(): string
66
    {
67
        return $this->name;
68
    }
69
70
    /**
71
     * Set a valid email or null.
72
     */
73
    public function setEmail(?string $email): void
74
    {
75
        $this->email = $email;
76
    }
77
78
    /**
79
     * Get the validated email or null.
80
     */
81
    public function getEmail(): ?string
82
    {
83
        return $this->email;
84
    }
85
86
    /**
87
     * Encrypt and change the user password.
88
     */
89
    public function setPassword(string $password): void
90
    {
91
        $this->password = password_hash($password, PASSWORD_DEFAULT) ?: '';
92
    }
93
94
    /**
95
     * Returns the hashed password.
96
     *
97
     * @API\Exclude
98
     */
99
    public function getPassword(): string
100
    {
101
        return $this->password;
102
    }
103
104
    /**
105
     * Set whether the user is an administrator.
106
     *
107
     * @API\Exclude
108
     */
109
    public function setIsAdministrator(bool $isAdministrator): void
110
    {
111
        $this->isAdministrator = $isAdministrator;
112
    }
113
114
    /**
115
     * Get whether the user is an administrator.
116
     */
117
    public function isAdministrator(): bool
118
    {
119
        return $this->isAdministrator;
120
    }
121
122
    /**
123
     * Returns all posts of the specified status.
124
     *
125
     * @param null|string $status the status of posts as defined in \GraphQLTests\Doctrine\AttributeBlog\Model\Post
126
     */
127
    #[API\Field(args: [new API\Argument(name: 'status', type: '?GraphQLTests\Doctrine\Blog\Types\PostStatusType')])]
128
    public function getPosts(?string $status = Post::STATUS_PUBLIC): Collection
129
    {
130
        // Return unfiltered collection
131
        if ($status === null) {
132
            return $this->posts;
133
        }
134
135
        return $this->posts->filter(fn (Post $post) => $post->getStatus() === $status);
136
    }
137
138
    #[API\Field(type: 'GraphQLTests\Doctrine\AttributeBlog\Model\Post[]', args: [
139
        new API\Argument(name: 'ids', type: 'id[]'),
140
    ])]
141
    public function getPostsWithIds(array $ids): Collection
142
    {
143
        return $this->posts->filter(fn (Post $post) => in_array($post->getId(), $ids, true));
144
    }
145
146
    public function setManager(?self $manager): void
147
    {
148
        $this->manager = $manager;
149
    }
150
151
    public function getManager(): ?self
152
    {
153
        return $this->manager;
154
    }
155
}
156