Skip to content

Commit effbd16

Browse files
committed
[UC-3] Add possibility to set configuration values to resource in admin panel
1 parent 0fadfa2 commit effbd16

10 files changed

Lines changed: 98 additions & 6 deletions

File tree

src/Api/AbstractClient.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ protected function getApiEndpointUrl(
7171
string $endpoint,
7272
string $query = null,
7373
): string {
74+
if (null === $resource->getUserComUrl()) {
75+
throw new \InvalidArgumentException('User.com API key is missing.');
76+
}
77+
7478
$url = sprintf(
7579
'%s/%s/%s/',
7680
trim($resource->getUserComUrl(), '/'),
@@ -98,6 +102,10 @@ protected function buildOptions(
98102
protected function authorizeRequest(
99103
UserComApiAwareInterface $resource,
100104
): string {
105+
if (null === $resource->getUserComApiKey()) {
106+
throw new \InvalidArgumentException('User.com API key is missing.');
107+
}
108+
101109
return sprintf(
102110
'Token %s',
103111
$resource->getUserComApiKey(),
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitBag\SyliusUserComPlugin\Form\Extension;
6+
7+
use Sylius\Bundle\ChannelBundle\Form\Type\ChannelType;
8+
use Symfony\Component\Form\AbstractTypeExtension;
9+
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
10+
use Symfony\Component\Form\Extension\Core\Type\TextType;
11+
use Symfony\Component\Form\FormBuilderInterface;
12+
use Symfony\Component\Validator\Constraints\NotBlank;
13+
use Symfony\Component\Validator\Constraints\Url;
14+
15+
final class UserComApiAwareTypeExtension extends AbstractTypeExtension
16+
{
17+
public function buildForm(FormBuilderInterface $builder, array $options)
18+
{
19+
$builder
20+
->add(
21+
'userComApiKey',
22+
PasswordType::class,
23+
[
24+
'label' => 'bitbag_sylius_user_com_plugin.ui.user_com_api_key',
25+
'required' => false,
26+
'constraints' => [
27+
new NotBlank(['groups' => ['sylius']]),
28+
],
29+
],
30+
)
31+
->add(
32+
'userComUrl',
33+
TextType::class,
34+
[
35+
'label' => 'bitbag_sylius_user_com_plugin.ui.user_com_url',
36+
'required' => false,
37+
'constraints' => [
38+
new NotBlank(['groups' => ['sylius']]),
39+
new Url(['groups' => ['sylius']]),
40+
],
41+
],
42+
);
43+
}
44+
45+
public static function getExtendedTypes(): iterable
46+
{
47+
return [
48+
ChannelType::class,
49+
];
50+
}
51+
}

src/Trait/UserComApiAwareInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
interface UserComApiAwareInterface
1515
{
16-
public function getUserComUrl(): string;
16+
public function getUserComUrl(): ?string;
1717

1818
public function setUserComUrl(string $userComUrl): void;
1919

20-
public function getUserComApiKey(): string;
20+
public function getUserComApiKey(): ?string;
2121

2222
public function setUserComApiKey(string $userComApiKey): void;
2323
}

src/Trait/UserComApiAwareTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
trait UserComApiAwareTrait
1515
{
16-
private string $userComUrl;
16+
private ?string $userComUrl;
1717

18-
private string $userComApiKey;
18+
private ?string $userComApiKey;
1919

20-
public function getUserComUrl(): string
20+
public function getUserComUrl(): ?string
2121
{
2222
return $this->userComUrl;
2323
}
@@ -27,7 +27,7 @@ public function setUserComUrl(string $userComUrl): void
2727
$this->userComUrl = $userComUrl;
2828
}
2929

30-
public function getUserComApiKey(): string
30+
public function getUserComApiKey(): ?string
3131
{
3232
return $this->userComApiKey;
3333
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="ui attached segment">
2+
{{ form_row(form.userComApiKey) }}
3+
{{ form_row(form.userComUrl) }}
4+
</div>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sylius_ui:
2+
events:
3+
sylius.admin.channel.form.first_column_content:
4+
blocks:
5+
user_com_configuration:
6+
template: '@BitBagSyliusUserComPlugin/UserApiResource/_userComCredentials.html.twig'
7+
priority: 25

tests/Application/config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
33
parameters:
44
locale: en_US
5+
6+
imports:
7+
- { resource: ./services/** }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
4+
>
5+
<services>
6+
<defaults public="true"/>
7+
<service id="bit_bag.sylius_user_com_plugin.form.extension.user_com_api_aware_type_extension" class="BitBag\SyliusUserComPlugin\Form\Extension\UserComApiAwareTypeExtension">
8+
<tag name="form.type_extension" extended-type="Sylius\Bundle\ChannelBundle\Form\Type\ChannelType" priority="200" />
9+
</service>
10+
</services>
11+
</container>

translations/messages.en.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bitbag_sylius_user_com_plugin:
2+
ui:
3+
user_com_api_key: 'User.com API Key'
4+
user_com_url: 'User.com Instance URL'

translations/messages.pl.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bitbag_sylius_user_com_plugin:
2+
ui:
3+
user_com_api_key: 'Klucz API User.com'
4+
user_com_url: 'Adres URL instancjiUser.com'

0 commit comments

Comments
 (0)