Passed
Push — master ( ef0fdb...1d76f0 )
by
unknown
03:02
created

CrefoPayUniqueIdGenerator::normalizeBase()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Service\CrefoPay\Generator;
9
10
use Generated\Shared\Transfer\ItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ItemTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SprykerEco\Service\CrefoPay\CrefoPayConfig;
13
use SprykerEco\Service\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface;
14
15
class CrefoPayUniqueIdGenerator implements CrefoPayUniqueIdGeneratorInterface
16
{
17
    /**
18
     * @var int
19
     */
20
    protected const RANDOM_STRING_MIN_LENGTH = 3;
21
22
    /**
23
     * @var \SprykerEco\Service\CrefoPay\CrefoPayConfig
24
     */
25
    protected CrefoPayConfig $crefoPayConfig;
26
27
    /**
28
     * @var \SprykerEco\Service\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface
29
     */
30
    protected CrefoPayToUtilTextServiceInterface $utilTextService;
31
32
    /**
33
     * @param \SprykerEco\Service\CrefoPay\CrefoPayConfig $crefoPayConfig
34
     * @param \SprykerEco\Service\CrefoPay\Dependency\Service\CrefoPayToUtilTextServiceInterface $utilTextService
35
     */
36
    public function __construct(
37
        CrefoPayConfig $crefoPayConfig,
38
        CrefoPayToUtilTextServiceInterface $utilTextService
39
    ) {
40
        $this->crefoPayConfig = $crefoPayConfig;
41
        $this->utilTextService = $utilTextService;
42
    }
43
44
    /**
45
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
46
     *
47
     * @return string
48
     */
49
    public function generateCrefoPayOrderId(QuoteTransfer $quoteTransfer): string
50
    {
51
        return $this->generateUniqueId(
52
            $this->crefoPayConfig->getCrefoPayOrderIdLength(),
53
            $quoteTransfer->getCustomerReference(),
54
        );
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
59
     *
60
     * @return string
61
     */
62
    public function generateBasketItemId(ItemTransfer $itemTransfer): string
63
    {
64
        $basketItemIdMaxLength = $this->crefoPayConfig->getCrefoPayBasketItemIdMaxLength();
65
        $normalizedSku = $this->normalizeBase(
66
            $basketItemIdMaxLength,
67
            $itemTransfer->getSku(),
68
        );
69
70
        if ($normalizedSku === $itemTransfer->getSku()) {
71
            return $itemTransfer->getSku();
72
        }
73
74
        return $this->generateUniqueId(
75
            $basketItemIdMaxLength,
76
            $itemTransfer->getSku(),
77
        );
78
    }
79
80
    /**
81
     * @param int $maxLength
82
     * @param string|null $base
83
     *
84
     * @return string
85
     */
86
    protected function generateUniqueId(int $maxLength, ?string $base = null): string
87
    {
88
        $baseMaxLength = $maxLength - static::RANDOM_STRING_MIN_LENGTH - 1;
89
        $base = $this->normalizeBase($baseMaxLength, $base);
90
        $randomStringLength = $maxLength - strlen($base) - 1;
91
92
        return sprintf(
93
            '%s-%s',
94
            $this->utilTextService->generateRandomString($randomStringLength),
95
            $base,
96
        );
97
    }
98
99
    /**
100
     * @param int $maxLength
101
     * @param string|null $base
102
     *
103
     * @return string
104
     */
105
    protected function normalizeBase(int $maxLength, ?string $base = null): string
106
    {
107
        if ($base === null) {
108
            return '';
109
        }
110
111
        if ($maxLength >= strlen($base)) {
112
            return $base;
113
        }
114
115
        return substr($base, 0, $maxLength);
116
    }
117
}
118