Skip to content

Commit e827640

Browse files
committed
Add more support to entity builder and authorisation setup command
1 parent 5df467b commit e827640

File tree

6 files changed

+91
-29
lines changed

6 files changed

+91
-29
lines changed

DevTools/Command/AuthorisationSetupCommand.php

+33-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DevTools\Command;
44

5+
use DevTools\ValueObjects\OAuthClient;
56
use DevTools\ValueObjects\OAuthTokenSet;
67
use GuzzleHttp\Client;
78
use GuzzleHttp\HandlerStack;
@@ -59,11 +60,10 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
5960
protected function execute(InputInterface $input, OutputInterface $output): int
6061
{
6162
$this->checkRedirectUriValue();
62-
$clientId = $this->askClientId();
63-
$clientSecret = $this->askClientSecret();
64-
$authorisationCode = $this->getAuthorisationCode($clientId);
65-
$tokenSet = $this->fetchTokens($clientId, $clientSecret, $authorisationCode);
66-
$this->writeTokenSetToFile($tokenSet);
63+
$client = $this->askClientDetails();
64+
$authorisationCode = $this->getAuthorisationCode($client);
65+
$tokenSet = $this->fetchTokens($client, $authorisationCode);
66+
$this->writeToFile($client, $tokenSet);
6767

6868
return 0;
6969
}
@@ -78,24 +78,29 @@ private function checkRedirectUriValue(): void
7878
}
7979
}
8080

81-
private function askClientId(): string
81+
private function askClientDetails(): OAuthClient
8282
{
83-
$question = new Question('What is the Client ID (Can be found in the Exact App Center)? ');
84-
return $this->questionHelper->ask($this->input, $this->output, $question);
85-
}
83+
$clientId = $this->questionHelper->ask(
84+
$this->input,
85+
$this->output,
86+
new Question('What is the Client ID (Can be found in the Exact App Center)? ')
87+
);
8688

87-
private function askClientSecret(): string
88-
{
89-
$question = new Question('What is the Client secret (Can be found in the Exact App Center)? ');
90-
return $this->questionHelper->ask($this->input, $this->output, $question);
89+
$clientSecret = $this->questionHelper->ask(
90+
$this->input,
91+
$this->output,
92+
new Question('What is the Client secret (Can be found in the Exact App Center)? ')
93+
);
94+
95+
return new OAuthClient($clientId, $clientSecret);
9196
}
9297

93-
private function getAuthorisationCode(string $clientId): string
98+
private function getAuthorisationCode(OAuthClient $client): string
9499
{
95100
$this->startHttpListening();
96101

97102
$url = self::$baseUrl . self::$authUrl . '?' . http_build_query([
98-
'client_id' => $clientId,
103+
'client_id' => $client->getId(),
99104
'redirect_uri' => self::$redirectUri,
100105
'response_type' => 'code'
101106
]);
@@ -115,7 +120,7 @@ private function getAuthorisationCode(string $clientId): string
115120
$i++;
116121
} else {
117122
$code = file_get_contents($filename);
118-
if ($code === null) {
123+
if ($code === false) {
119124
$this->output->writeln('Unable to read code from file, halting!');
120125
$this->stopHttpListening();
121126
exit(1);
@@ -143,7 +148,7 @@ private function stopHttpListening(): void
143148
shell_exec(sprintf('kill %d 2>&1', $this->pid));
144149
}
145150

146-
private function fetchTokens(string $clientId, string $clientSecret, string $authorisationCode): OAuthTokenSet
151+
private function fetchTokens(OAuthClient $client, string $authorisationCode): OAuthTokenSet
147152
{
148153
$this->output->writeln('Requesting access and refresh tokens.');
149154

@@ -152,8 +157,8 @@ private function fetchTokens(string $clientId, string $clientSecret, string $aut
152157
'form_params' => [
153158
'redirect_uri' => self::$redirectUri,
154159
'grant_type' => 'authorization_code',
155-
'client_id' => $clientId,
156-
'client_secret' => $clientSecret,
160+
'client_id' => $client->getId(),
161+
'client_secret' => $client->getSecret(),
157162
'code' => $authorisationCode
158163
]
159164
];
@@ -177,12 +182,18 @@ private function getFullDestinationPath(string $destination): string
177182
return getcwd() . DIRECTORY_SEPARATOR . $destination;
178183
}
179184

180-
private function writeTokenSetToFile(OAuthTokenSet $tokenSet): void
185+
private function writeToFile(OAuthClient $client, OAuthTokenSet $tokenSet): void
181186
{
182-
$path = $this->getFullDestinationPath($this->input->getOption('output'));
187+
$output = $this->input->getOption('output');
188+
189+
if (! is_string($output)) {
190+
throw new \InvalidArgumentException('Output parameter should be a string');
191+
}
192+
193+
$path = $this->getFullDestinationPath($output);
183194
$fileName = $path . DIRECTORY_SEPARATOR . 'oauth.json';
184195

185-
file_put_contents($fileName, json_encode($tokenSet, JSON_PRETTY_PRINT));
196+
file_put_contents($fileName, json_encode(array_merge($client->jsonSerialize(), $tokenSet->jsonSerialize()), JSON_PRETTY_PRINT));
186197

187198
$this->output->writeln('Access and refresh tokens are available in: ' . $fileName);
188199
}

DevTools/TwigExtension.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public function getFilters(): array
2424
new TwigFilter('derivePropertyName', function (Property $property) : string {
2525
return $this->derivePropertyName($property);
2626
}),
27+
new TwigFilter('deriveEndpointUri', function (Endpoint $endpoint) : string {
28+
return $this->deriveEndpointUri($endpoint);
29+
}),
2730
];
2831
}
2932

@@ -76,12 +79,17 @@ public function derivePropertyName(Property $property): string
7679
return lcfirst($property->getName());
7780
}
7881

82+
public function deriveEndpointUri(Endpoint $endpoint): string
83+
{
84+
return ltrim($endpoint->getUri(), '/api/v1/{division}');
85+
}
86+
7987
private function propertyIsApiEntity(Property $property): bool
8088
{
8189
return strpos($property->getType(), 'Exact.Web.Api.Models.') === 0;
8290
}
8391

84-
private function mapPropertyTypeToApiEntityClassName(Property $property)
92+
private function mapPropertyTypeToApiEntityClassName(Property $property): string
8593
{
8694
$propertyType = ltrim($property->getType(), 'Exact.Web.Api.Models.');
8795
$propertyType = str_replace('.', '\\', $propertyType);

DevTools/ValueObjects/OAuthClient.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DevTools\ValueObjects;
4+
5+
6+
class OAuthClient implements \JsonSerializable
7+
{
8+
/** @var string */
9+
private $id;
10+
/** @var string */
11+
private $secret;
12+
13+
public function __construct(string $id, string $secret)
14+
{
15+
$this->id = $id;
16+
$this->secret = $secret;
17+
}
18+
19+
public function getId(): string
20+
{
21+
return $this->id;
22+
}
23+
24+
public function getSecret(): string
25+
{
26+
return $this->secret;
27+
}
28+
29+
public function jsonSerialize(): array
30+
{
31+
return [
32+
'client_id' => $this->id,
33+
'client_secret' => $this->secret
34+
];
35+
}
36+
}

composer.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@
3434
"rector/rector": "^0.5.8"
3535
},
3636
"scripts": {
37-
"phpcs": [
38-
"./vendor/bin/phpcs --standard=PSR2 DevTools template application.php"
39-
],
40-
"phpcbf": [
41-
"./vendor/bin/phpcbf --standard=PSR2 DevTools template application.php"
42-
]
37+
"phpcs": ["./vendor/bin/phpcs --standard=PSR2 DevTools template application.php"],
38+
"phpcbf": ["./vendor/bin/phpcbf --standard=PSR2 DevTools template application.php"],
39+
"phpstan": "./vendor/bin/phpstan analyse",
40+
"build-entities": ["./exact-online-api-client-dev-tools build-entities --destination entities"]
4341
},
4442
"bin": [
4543
"exact-online-api-client-dev-tools"

phpstan.neon.dist

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- DevTools

template/entity.php.template

+5
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ class {{ endpoint|className }} extends Entity
2222
return $this->{{ property|derivePropertyName }};
2323
}
2424
{% endfor %}
25+
26+
public static function getApiEndpoint(): string
27+
{
28+
return '{{ endpoint|deriveEndpointUri }}';
29+
}
2530
}

0 commit comments

Comments
 (0)