Skip to content

Commit 2e3760c

Browse files
committed
[Refactor] Reduce API class constructor arguments
1 parent 0bd5b07 commit 2e3760c

File tree

6 files changed

+194
-126
lines changed

6 files changed

+194
-126
lines changed

src/Api/Api.php

Lines changed: 38 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,24 @@ class Api
6060
private $name;
6161

6262
/**
63-
* @var EncodingList
64-
*/
65-
private $encodings;
66-
67-
/**
68-
* @var DecodingList
69-
*/
70-
private $decodings;
71-
72-
/**
73-
* @var bool
63+
* @var Url
7464
*/
75-
private $useEloquent;
65+
private $url;
7666

7767
/**
78-
* @var Url
68+
* @var Config
7969
*/
80-
private $url;
70+
private $config;
8171

8272
/**
83-
* @var Jobs
73+
* @var EncodingList|null
8474
*/
85-
private $jobs;
75+
private $encodings;
8676

8777
/**
88-
* @var string|null
78+
* @var DecodingList|null
8979
*/
90-
private $supportedExt;
80+
private $decodings;
9181

9282
/**
9383
* @var ContainerInterface|null
@@ -104,71 +94,27 @@ class Api
10494
*/
10595
private $responses;
10696

107-
/**
108-
* @var array
109-
*/
110-
private $providers;
111-
112-
/**
113-
* @var string|null
114-
*/
115-
private $connection;
116-
117-
/**
118-
* @var bool
119-
*/
120-
private $transactions;
121-
122-
/**
123-
* @var string|null
124-
*/
125-
private $modelNamespace;
126-
12797
/**
12898
* Api constructor.
12999
*
130100
* @param Factory $factory
131101
* @param AggregateResolver $resolver
132-
* @param $apiName
133-
* @param EncodingList $encodings
134-
* @param DecodingList $decodings
102+
* @param string $name
135103
* @param Url $url
136-
* @param Jobs $jobs
137-
* @param bool $useEloquent
138-
* @param string|null $supportedExt
139-
* @param array $providers
140-
* @param string|null $connection
141-
* @param bool $transactions
142-
* @param string|null $modelNamespace
104+
* @param Config $config
143105
*/
144106
public function __construct(
145107
Factory $factory,
146108
AggregateResolver $resolver,
147-
$apiName,
148-
EncodingList $encodings,
149-
DecodingList $decodings,
109+
string $name,
150110
Url $url,
151-
Jobs $jobs,
152-
$useEloquent = true,
153-
$supportedExt = null,
154-
array $providers = [],
155-
string $connection = null,
156-
bool $transactions = true,
157-
string $modelNamespace = null
111+
Config $config
158112
) {
159113
$this->factory = $factory;
160114
$this->resolver = $resolver;
161-
$this->name = $apiName;
162-
$this->encodings = $encodings;
163-
$this->decodings = $decodings;
115+
$this->name = $name;
164116
$this->url = $url;
165-
$this->jobs = $jobs;
166-
$this->useEloquent = $useEloquent;
167-
$this->supportedExt = $supportedExt;
168-
$this->providers = $providers;
169-
$this->connection = $connection;
170-
$this->transactions = $transactions;
171-
$this->modelNamespace = $modelNamespace;
117+
$this->config = $config;
172118
}
173119

174120
/**
@@ -223,7 +169,7 @@ public function getName()
223169
*/
224170
public function isEloquent()
225171
{
226-
return $this->useEloquent;
172+
return $this->config->useEloquent();
227173
}
228174

229175
/**
@@ -239,7 +185,7 @@ public function getUrl()
239185
*/
240186
public function getJobs()
241187
{
242-
return $this->jobs;
188+
return Jobs::fromArray($this->config->jobs());
243189
}
244190

245191
/**
@@ -271,27 +217,38 @@ public function getStore()
271217
*/
272218
public function getSupportedExtensions()
273219
{
274-
if (!$this->supportedExt) {
275-
return null;
220+
if ($ext = $this->config->supportedExt()) {
221+
return $this->factory->createSupportedExtensions($ext);
276222
}
277223

278-
return $this->factory->createSupportedExtensions($this->supportedExt);
224+
return null;
279225
}
280226

281227
/**
282228
* @return EncodingList
283229
*/
284230
public function getEncodings(): EncodingList
285231
{
286-
return $this->encodings;
232+
if ($this->encodings) {
233+
return $this->encodings;
234+
}
235+
236+
return $this->encodings = EncodingList::fromArray(
237+
$this->config->encoding(),
238+
$this->url->toString()
239+
);
287240
}
288241

289242
/**
290243
* @return DecodingList
291244
*/
292245
public function getDecodings(): DecodingList
293246
{
294-
return $this->decodings;
247+
if ($this->decodings) {
248+
return $this->decodings;
249+
}
250+
251+
return $this->decodings = DecodingList::fromArray($this->config->decoding());
295252
}
296253

297254
/**
@@ -303,8 +260,8 @@ public function getDefaultCodec(): Codec
303260
{
304261
return $this->factory->createCodec(
305262
$this->getContainer(),
306-
$this->encodings->find(MediaTypeInterface::JSON_API_MEDIA_TYPE) ?: Encoding::jsonApi(),
307-
$this->decodings->find(MediaTypeInterface::JSON_API_MEDIA_TYPE)
263+
$this->getEncodings()->find(MediaTypeInterface::JSON_API_MEDIA_TYPE) ?: Encoding::jsonApi(),
264+
$this->getDecodings()->find(MediaTypeInterface::JSON_API_MEDIA_TYPE)
308265
);
309266
}
310267

@@ -329,7 +286,7 @@ public function getResponses()
329286
*/
330287
public function getConnection(): ?string
331288
{
332-
return $this->connection;
289+
return $this->config->dbConnection();
333290
}
334291

335292
/**
@@ -339,7 +296,7 @@ public function getConnection(): ?string
339296
*/
340297
public function hasTransactions(): bool
341298
{
342-
return $this->transactions;
299+
return $this->config->dbTransactions();
343300
}
344301

345302
/**
@@ -356,7 +313,7 @@ public function exceptions(): ExceptionParserInterface
356313
*/
357314
public function getModelNamespace(): ?string
358315
{
359-
return $this->modelNamespace;
316+
return $this->config->modelNamespace();
360317
}
361318

362319
/**
@@ -438,7 +395,7 @@ public function providers(): ResourceProviders
438395
{
439396
return new ResourceProviders(
440397
$this->factory,
441-
$this->providers
398+
$this->config->providers()
442399
);
443400
}
444401

src/Api/Config.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace CloudCreativity\LaravelJsonApi\Api;
4+
5+
use Illuminate\Support\Arr;
6+
7+
final class Config
8+
{
9+
10+
/**
11+
* @var array
12+
*/
13+
private $config;
14+
15+
/**
16+
* Config constructor.
17+
*
18+
* @param array $config
19+
*/
20+
public function __construct(array $config)
21+
{
22+
$this->config = $config;
23+
}
24+
25+
/**
26+
* Get all config.
27+
*
28+
* @return array
29+
*/
30+
public function all(): array
31+
{
32+
return $this->config;
33+
}
34+
35+
/**
36+
* Get the database connection for controller transactions.
37+
*
38+
* @return string|null
39+
* @deprecated
40+
*/
41+
public function dbConnection(): ?string
42+
{
43+
return Arr::get($this->config, 'controllers.connection');
44+
}
45+
46+
/**
47+
* Should database transactions be used by controllers?
48+
*
49+
* @return bool
50+
*/
51+
public function dbTransactions(): bool
52+
{
53+
return Arr::get($this->config, 'controllers.transactions', true);
54+
}
55+
56+
/**
57+
* Get the decoding media types configuration.
58+
*
59+
* @return array
60+
*/
61+
public function decoding(): array
62+
{
63+
return $this->config['decoding'];
64+
}
65+
66+
/**
67+
* Get the encoding media types configuration.
68+
*
69+
* @return array
70+
*/
71+
public function encoding(): array
72+
{
73+
return $this->config['encoding'] ?? [];
74+
}
75+
76+
/**
77+
* Get the asynchronous job configuration.
78+
*
79+
* @return array
80+
*/
81+
public function jobs(): array
82+
{
83+
return $this->config['jobs'] ?? [];
84+
}
85+
86+
/**
87+
* Get the default namespace for the application's models.
88+
*
89+
* @return string|null
90+
*/
91+
public function modelNamespace(): ?string
92+
{
93+
return $this->config['model-namespace'] ?? null;
94+
}
95+
96+
/**
97+
* Get resource providers.
98+
*
99+
* @return array
100+
*/
101+
public function providers(): array
102+
{
103+
return $this->config['providers'] ?? [];
104+
}
105+
106+
/**
107+
* Get the supported extensions.
108+
*
109+
* @return string|null
110+
* @deprecated
111+
*/
112+
public function supportedExt(): ?string
113+
{
114+
return $this->config['supported-ext'] ?? null;
115+
}
116+
117+
/**
118+
* @return array
119+
*/
120+
public function url(): array
121+
{
122+
return $this->config['url'] ?? [];
123+
}
124+
125+
/**
126+
* Are the application's models predominantly Eloquent models?
127+
*
128+
* @return bool
129+
*/
130+
public function useEloquent(): bool
131+
{
132+
return $this->config['use-eloquent'] ?? true;
133+
}
134+
}

0 commit comments

Comments
 (0)