Passed
Push — bugfix/supesc-612-crefopay-use... ( d838fe )
by Roman
09:03
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
            $quoteTransfer->getCustomerReference(),
53
            $this->crefoPayConfig->getCrefoPayOrderIdLength(),
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
        $normalizedSku = $this->normalizeBase(
65
            $itemTransfer->getSku(),
66
            $this->crefoPayConfig->getCrefoPayBasketItemIdMaxLength(),
67
        );
68
69
        if ($normalizedSku === $itemTransfer->getSku()) {
70
            return $itemTransfer->getSku();
71
        }
72
73
        return $this->generateUniqueId(
74
            $itemTransfer->getSku(),
75
            $this->crefoPayConfig->getCrefoPayBasketItemIdMaxLength(),
76
        );
77
    }
78
79
    /**
80
     * @param string|null $base
81
     * @param int $maxLength
82
     *
83
     * @return string
84
     */
85
    protected function generateUniqueId(?string $base, int $maxLength): string
86
    {
87
        $baseMaxLength = $maxLength - static::RANDOM_STRING_MIN_LENGTH - 1;
88
        $base = $this->normalizeBase($base, $baseMaxLength);
89
        $randomStringLength = $maxLength - strlen($base) - 1;
90
91
        return sprintf(
92
            '%s-%s',
93
            $this->utilTextService->generateRandomString($randomStringLength),
94
            $base,
95
        );
96
    }
97
98
    /**
99
     * @param string|null $base
100
     * @param int $maxLength
101
     *
102
     * @return string
103
     */
104
    protected function normalizeBase(?string $base, int $maxLength): string
105
    {
106
        if ($base === null) {
107
            return '';
108
        }
109
110
        if ($maxLength >= strlen($base)) {
111
            return $base;
112
        }
113
114
        return substr($base, 0, $maxLength);
115
    }
116
}
117