Skip to content

Commit 0d809c2

Browse files
authored
Merge pull request #12 from WeGetFinancing/GET-2539
GET-2539 | refactor(sdk)
2 parents eb5c578 + 3ef5c20 commit 0d809c2

22 files changed

+548
-704
lines changed

Dockerfile

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
FROM php:8.0-fpm-alpine
2-
3-
RUN apk add --no-cache \
4-
libzip-dev \
5-
zip \
6-
&& docker-php-ext-install zip
7-
8-
RUN apk --no-cache add pcre-dev ${PHPIZE_DEPS} \
9-
&& pecl install xdebug \
10-
&& docker-php-ext-enable xdebug \
11-
&& apk del pcre-dev ${PHPIZE_DEPS}
12-
13-
RUN set -xe \
14-
&& apk add --update \
15-
icu \
16-
&& apk add --no-cache --virtual .php-deps \
17-
make \
18-
&& apk add --no-cache --virtual .build-deps \
19-
$PHPIZE_DEPS \
20-
zlib-dev \
21-
icu-dev \
22-
g++ \
23-
&& docker-php-ext-configure intl \
24-
&& docker-php-ext-install \
25-
intl \
26-
&& docker-php-ext-enable intl \
27-
&& { find /usr/local/lib -type f -print0 | xargs -0r strip --strip-all -p 2>/dev/null || true; } \
28-
&& apk del .build-deps \
29-
&& rm -rf /tmp/* /usr/local/lib/php/doc/* /var/cache/apk/*
1+
FROM php:8.2-fpm
2+
3+
RUN apt-get -y update \
4+
&& apt-get -y install curl
5+
6+
RUN curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.deb.sh' | bash
7+
8+
RUN apt-get -y install \
9+
libzip-dev \
10+
zip \
11+
libfreetype6-dev \
12+
libjpeg62-turbo-dev \
13+
libpng-dev \
14+
libicu-dev \
15+
libxml2-dev \
16+
libxslt-dev \
17+
libpq-dev \
18+
symfony-cli \
19+
&& apt-get -y clean
20+
21+
RUN docker-php-ext-install zip
22+
23+
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
24+
&& docker-php-ext-install -j$(nproc) gd
25+
26+
RUN docker-php-ext-configure intl \
27+
&& docker-php-ext-install intl
28+
29+
RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
30+
&& docker-php-ext-install pgsql pdo_pgsql pdo
31+
32+
RUN docker-php-ext-install soap
33+
34+
RUN docker-php-ext-install xsl
35+
36+
RUN docker-php-ext-install sockets
37+
38+
RUN docker-php-ext-install bcmath
3039

3140
# Install composer
3241
COPY --from=composer/composer:2 /usr/bin/composer /usr/bin/composer

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
"minimum-stability": "stable",
55
"prefer-stable": true,
66
"license": "LGPL-3.0-only",
7-
"version": "2.4.1",
7+
"version": "4.0.0",
88
"authors": [
99
{
1010
"name": "Riccardo De Leo",
1111
"email": "riccardo.deleo@empaytech.com"
1212
}
1313
],
1414
"require": {
15-
"php": ">=8.0",
15+
"php": ">=8.2",
1616
"ext-json": "*",
1717
"guzzlehttp/guzzle": "^7.0",
18-
"symfony/validator": "5.4.8",
18+
"symfony/validator": "7.0.*",
1919
"doctrine/annotations": "^1.14.3",
20-
"symfony/serializer": "v4.4.47",
21-
"symfony/cache": "v5.4.9"
20+
"symfony/serializer": "7.*.*",
21+
"symfony/cache": "7.*"
2222
},
2323
"require-dev": {
2424
"phpmd/phpmd": "^2",

src/Entity/AbstractEntity.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public function __construct(
3838
public static function getValidator(): ValidatorInterface
3939
{
4040
return Validation::createValidatorBuilder()
41-
->enableAnnotationMapping()
42-
->addDefaultDoctrineAnnotationReader()
41+
->enableAttributeMapping()
4342
->getValidator();
4443
}
4544

@@ -50,18 +49,9 @@ public static function getValidator(): ValidatorInterface
5049
*/
5150
public function initFromArray(array $data): self
5251
{
53-
try {
54-
foreach ($data as $key => $value) {
55-
$propertyName = $this->camelCaseToSnakeCase->denormalize($key);
56-
$this->{$propertyName} = $value;
57-
}
58-
} catch (TypeError $exception) {
59-
throw new EntityValidationException(
60-
EntityValidationException::INVALID_ENTITY_DATA_MESSAGE,
61-
EntityValidationException::TYPE_ERROR_INIT_ENTITY_ABSTRACT_CODE,
62-
null,
63-
[ 'field' => 'unknown', 'message' => $exception->getMessage() ]
64-
);
52+
foreach ($data as $key => $value) {
53+
$propertyName = $this->camelCaseToSnakeCase->denormalize($key);
54+
$this->{$propertyName} = $value;
6555
}
6656

6757
$this->isValid();
@@ -94,4 +84,34 @@ public function isValid(): bool
9484
$messages
9585
);
9686
}
87+
88+
/**
89+
* @param array $errors
90+
* @return $this
91+
* @throws EntityValidationException
92+
*/
93+
protected function compositeIsValid(array $errors): static
94+
{
95+
try {
96+
$this->isValid();
97+
} catch (EntityValidationException $exception) {
98+
throw new EntityValidationException(
99+
EntityValidationException::INVALID_ENTITY_DATA_MESSAGE,
100+
EntityValidationException::VALIDATION_VIOLATION_INIT_ENTITY_ABSTRACT_CODE,
101+
null,
102+
array_merge($exception->getViolations(), $errors)
103+
);
104+
}
105+
106+
if (false === empty($errors)) {
107+
throw new EntityValidationException(
108+
EntityValidationException::INVALID_ENTITY_DATA_MESSAGE,
109+
EntityValidationException::VALIDATION_VIOLATION_INIT_ENTITY_ABSTRACT_CODE,
110+
null,
111+
$errors
112+
);
113+
}
114+
115+
return $this;
116+
}
97117
}

src/Entity/AuthEntity.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,36 @@
1010

1111
class AuthEntity extends AbstractEntity
1212
{
13-
/**
14-
* @Assert\Length(
15-
* min = 2,
16-
* minMessage = "The value of username is too short. It should have {{ limit }} characters or more."
17-
* )
18-
* @Assert\NotBlank(message = "The value of username should not be blank.")
19-
*/
20-
protected string $username;
13+
#[Assert\Type(type: "string", message: "The value {{ value }} is not a valid {{ type }}.")]
14+
#[Assert\NotBlank(message: "The value of username should not be blank.", allowNull: true)]
15+
#[Assert\NotNull(message: "The value of username should not be null.")]
16+
#[Assert\Length(
17+
min: 2,
18+
minMessage: "The value of username is too short. It should have {{ limit }} characters or more."
19+
)]
20+
protected mixed $username;
2121

22-
/**
23-
* @Assert\Length(
24-
* min = 2,
25-
* minMessage = "The value of password is too short. It should have {{ limit }} characters or more."
26-
* )
27-
* @Assert\NotBlank(message = "The value of password should not be blank.")
28-
*/
29-
protected string $password;
22+
#[Assert\Type(type: "string", message: "The value {{ value }} is not a valid {{ type }}.")]
23+
#[Assert\NotBlank(message: "The value of password should not be blank.", allowNull: true)]
24+
#[Assert\NotNull(message: "The value of password should not be null.")]
25+
#[Assert\Length(
26+
min: 2,
27+
minMessage: "The value of password is too short. It should have {{ limit }} characters or more."
28+
)]
29+
protected mixed $password;
3030

31-
/**
32-
* @Assert\Length(
33-
* min = 2,
34-
* minMessage = "The value of merchant id is too short. It should have {{ limit }} characters or more."
35-
* )
36-
* @Assert\NotBlank(message = "The value of merchant id should not be blank.")
37-
*/
38-
protected string $merchantId;
31+
#[Assert\Type(type: "string", message: "The value {{ value }} is not a valid {{ type }}.")]
32+
#[Assert\NotBlank(message: "The value of merchant id should not be blank.", allowNull: true)]
33+
#[Assert\NotNull(message: "The value of merchant id should not be null.")]
34+
#[Assert\Length(
35+
min: 2,
36+
minMessage: "The value of merchant id is too short. It should have {{ limit }} characters or more."
37+
)]
38+
protected mixed $merchantId;
3939

40-
protected bool $prod = false;
40+
#[Assert\NotNull(message: "The value of prod should not be null.")]
41+
#[Assert\Type(type: "bool", message: "The value {{ value }} is not a valid {{ type }}.")]
42+
protected mixed $prod = false;
4143

4244
/**
4345
* @SuppressWarnings(PHPMD.StaticAccess)

src/Entity/MoneyEntity.php

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ class MoneyEntity extends AbstractEntity
1717

1818
public const DECIMAL_SEPARATOR = '.';
1919

20-
/**
21-
* @Assert\Type(
22-
* type = "numeric",
23-
* message = "value is not a valid {{ type }}."
24-
* )
25-
* @Assert\PositiveOrZero(message = "value should be either positive or zero if allowed.")
26-
* @Assert\NotBlank(message = "value should not be blank.")
27-
*/
28-
public string $value;
20+
#[Assert\Type(type: "numeric", message: "The value {{ value }} is not a valid {{ type }}.")]
21+
#[Assert\PositiveOrZero(message: "The value should be either positive or zero if allowed.")]
22+
#[Assert\NotBlank(message: "The value should not be blank.", allowNull: true)]
23+
#[Assert\NotNull(message: "The value should not be null.")]
24+
public mixed $value = null;
2925

30-
protected string $name = "";
26+
#[Assert\Type(type: "string", message: "The value {{ value }} is not a valid {{ type }}.")]
27+
#[Assert\NotNull(message: "The value of name should not be null.")]
28+
protected mixed $name = "unknown";
3129

3230
protected bool $isZeroAllowed = false;
3331

@@ -43,45 +41,14 @@ public function __construct(
4341
NameConverterInterface $camelCaseToSnakeCase,
4442
array $data = null
4543
) {
46-
if (
47-
(false === is_null($data) && true === array_key_exists('value', $data)) &&
48-
(true === is_int($data['value']) || true === is_float($data['value']))
49-
) {
50-
$data['value'] = (string)$data['value'];
44+
if (false === is_null($data) && true === array_key_exists('value', $data)) {
45+
$data['value'] = (true === is_int($data['value']) || true === is_float($data['value']))
46+
? (string)$data['value']
47+
: $data['value'];
5148
}
5249
parent::__construct($validator, $camelCaseToSnakeCase, $data);
5350
}
5451

55-
/**
56-
* @SuppressWarnings(PHPMD.StaticAccess)
57-
*
58-
* @param array<string, mixed> $data
59-
* @throws EntityValidationException
60-
* @return self
61-
*/
62-
public function initFromArray(array $data): self
63-
{
64-
try {
65-
foreach ($data as $key => $value) {
66-
$propertyName = $this->camelCaseToSnakeCase->denormalize($key);
67-
$this->{$propertyName} = $value;
68-
}
69-
} catch (TypeError $exception) {
70-
throw new EntityValidationException(
71-
EntityValidationException::INVALID_ENTITY_DATA_MESSAGE,
72-
EntityValidationException::VALIDATION_VIOLATION_INIT_MONEY_ENTITY_CODE,
73-
null,
74-
[[
75-
'field' => true === empty($this->name) ? 'unknown' : $this->name,
76-
'message' => $this->getFormattedName() . $exception->getMessage(),
77-
]]
78-
);
79-
}
80-
81-
$this->isValid();
82-
return $this;
83-
}
84-
8552
/**
8653
* @throws EntityValidationException
8754
*/
@@ -95,7 +62,8 @@ public function isValid(): bool
9562
foreach ($violations as $violation) {
9663
$messages[] = [
9764
'field' => true === empty($this->name) ? 'unknown' : $this->name,
98-
'message' => $this->getFormattedName() . $violation->getMessage(),
65+
'message' => "The money entity named " . $this->getFormattedName() . " generated an error, " .
66+
$violation->getMessage(),
9967
];
10068
}
10169
}
@@ -106,7 +74,7 @@ public function isValid(): bool
10674
) {
10775
$messages[] = [
10876
'field' => true === empty($this->name) ? 'unknown' : $this->name,
109-
'message' => $this->getFormattedName() .
77+
'message' => "The money entity named " . $this->getFormattedName() . " generated an error, " .
11078
EntityValidationException::VALIDATION_VIOLATION_INIT_MONEY_ENTITY_MESSAGE,
11179
];
11280
}
@@ -141,6 +109,9 @@ public static function make(array $data = null): MoneyEntity
141109

142110
public function getValue(): string
143111
{
112+
if (false === is_numeric($this->value)) {
113+
return "0.00";
114+
}
144115
$numberParts = explode(self::DECIMAL_SEPARATOR, $this->value);
145116
$value = $numberParts[0];
146117
$value .= self::DECIMAL_SEPARATOR;
@@ -158,8 +129,6 @@ public function getValue(): string
158129

159130
private function getFormattedName(): string
160131
{
161-
return true === empty($this->name)
162-
? $this->name
163-
: $this->name . " ";
132+
return $this->name;
164133
}
165134
}

0 commit comments

Comments
 (0)