Completed
Push — master ( 778409...c54dd1 )
by Vincent
03:02
created

ErrorFactory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 58
dl 0
loc 195
ccs 53
cts 53
cp 1
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fake() 0 10 1
A fakeDetails() 0 5 1
A fakeSource() 0 7 1
A setSource() 0 5 1
A fakeStatus() 0 12 1
C toArray() 0 29 9
A fakeTitle() 0 5 1
A fakeCode() 0 5 1
A set() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\Factory;
6
7
use VGirol\JsonApiFaker\Members;
8
use VGirol\JsonApiFaker\Messages;
9
10
/**
11
 * A factory for error object
12
 */
13
class ErrorFactory extends BaseFactory
14
{
15
    use HasIdentifier;
16
    use HasLinks;
17
    use HasMeta;
18
19
    /**
20
     * The "status" member
21
     *
22
     * @var string
23
     */
24
    public $status;
25
26
    /**
27
     * The "code" member
28
     *
29
     * @var string
30
     */
31
    public $code;
32
33
    /**
34
     * The "title" member
35
     *
36
     * @var string
37
     */
38
    public $title;
39
40
    /**
41
     * The "details" member
42
     *
43
     * @var string
44
     */
45
    public $details;
46
47
    /**
48
     * The "source" member
49
     *
50
     * @var array<string,mixed>
51
     */
52
    public $source;
53
54
    /**
55
     * Set one of the following member : status, code, title, details
56
     *
57
     * @param string $key
58
     * @param string $value
59
     *
60
     * @return static
61
     * @throws \Exception
62
     */
63 9
    public function set(string $key, string $value)
64
    {
65 9
        if (!property_exists($this, $key)) {
66 1
            throw new \Exception(sprintf(Messages::ERROR_OBJECT_INEXISTANT_KEY, $key));
67
        }
68
69 8
        $this->{$key} = $value;
70
71 8
        return $this;
72
    }
73
74
    /**
75
     * Set the "source" member
76
     *
77
     * @param array<string,mixed> $source
78
     *
79
     * @return static
80
     */
81 4
    public function setSource(array $source)
82
    {
83 4
        $this->source = $source;
84
85 4
        return $this;
86
    }
87
88
    /**
89
     * @inheritDoc
90
     * @return array<string,mixed>
91
     */
92 4
    public function toArray(): array
93
    {
94 4
        $resource = [];
95 4
        if (isset($this->id)) {
96 4
            $resource[Members::ID] = $this->id;
97
        }
98 4
        if (isset($this->links)) {
99 3
            $resource[Members::LINKS] = $this->links;
100
        }
101 4
        if (isset($this->status)) {
102 4
            $resource[Members::STATUS] = $this->status;
103
        }
104 4
        if (isset($this->code)) {
105 3
            $resource[Members::CODE] = $this->code;
106
        }
107 4
        if (isset($this->title)) {
108 3
            $resource[Members::TITLE] = $this->title;
109
        }
110 4
        if (isset($this->details)) {
111 3
            $resource[Members::DETAILS] = $this->details;
112
        }
113 4
        if (isset($this->source)) {
114 3
            $resource[Members::SOURCE] = $this->source;
115
        }
116 4
        if (isset($this->meta)) {
117 4
            $resource[Members::META] = $this->meta;
118
        }
119
120 4
        return $resource;
121
    }
122
123
    /**
124
     * Fill the error object with fake values
125
     * ("id", "links", "meta", "status", "code", "title", "details" and "source").
126
     *
127
     * @return static
128
     */
129 2
    public function fake()
130
    {
131 2
        return $this->fakeIdentifier()
132 2
            ->fakeLinks(['about' => ['url']])
133 2
            ->fakeMeta()
134 2
            ->fakeStatus()
135 2
            ->fakeCode()
136 2
            ->fakeTitle()
137 2
            ->fakeDetails()
138 2
            ->fakeSource();
139
    }
140
141
    /**
142
     * Fill the "status" member with fake value
143
     *
144
     * @return static
145
     */
146 2
    private function fakeStatus()
147
    {
148
        $status = [
149 2
            '200', '201', '202', '203', '204', '205', '206', '207', '208', '226',
150
            '300', '301', '302', '303', '304', '305', '307', '308',
151
            '400', '401', '402', '403', '404', '405', '406', '407', '408', '409',
152
            '410', '411', '412', '413', '414', '415', '416', '417', '422', '423', '424', '426', '428', '429', '431',
153
            '500', '501', '502', '503', '504', '505', '506', '507', '508', '510', '511'
154
        ];
155 2
        $faker = \Faker\Factory::create();
156
157 2
        return $this->set('status', strval($faker->randomElement($status)));
158
    }
159
160
    /**
161
     * Fill the "code" member with fake value
162
     *
163
     * @return static
164
     */
165 2
    private function fakeCode()
166
    {
167 2
        $faker = \Faker\Factory::create();
168
169 2
        return $this->set('code', strval($faker->numberBetween(1, 100)));
170
    }
171
172
    /**
173
     * Fill the "title" member with fake value
174
     *
175
     * @return static
176
     */
177 2
    private function fakeTitle()
178
    {
179 2
        $faker = \Faker\Factory::create();
180
181 2
        return $this->set('title', $faker->sentence);
182
    }
183
184
    /**
185
     * Fill the "details" member with fake value
186
     *
187
     * @return static
188
     */
189 2
    private function fakeDetails()
190
    {
191 2
        $faker = \Faker\Factory::create();
192
193 2
        return $this->set('details', $faker->paragraph);
194
    }
195
196
    /**
197
     * Fill the "source" member with fake value
198
     *
199
     * @return static
200
     */
201 2
    private function fakeSource()
202
    {
203 2
        $faker = \Faker\Factory::create();
204
205 2
        return $this->setSource(
206
            [
207 2
                'parameter' => $faker->word
208
            ]
209
        );
210
    }
211
}
212