Passed
Push — master ( 7e874c...f67e1e )
by Christian
14:45 queued 02:54
created

PromotionCodesLoader::generateCodeFixed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Promotion\Util;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Framework\Uuid\Uuid;
7
8
class PromotionCodesLoader
9
{
10
    /**
11
     * @var Connection
12
     */
13
    private $connection;
14
15
    public function __construct(Connection $connection)
16
    {
17
        $this->connection = $connection;
18
    }
19
20
    /**
21
     * @throws \Shopware\Core\Framework\Uuid\Exception\InvalidUuidException
22
     */
23
    public function loadIndividualCodes(string $promotionId): array
24
    {
25
        $qb = $this->connection->createQueryBuilder();
26
27
        $qb->select('code');
28
        $qb->from('promotion_individual_code');
29
        $qb->where($qb->expr()->eq('promotion_id', ':id'));
30
        $qb->setParameter(':id', Uuid::fromHexToBytes($promotionId));
31
32
        /** @var array|bool $result */
33
        $result = $qb->execute()->fetchAll(\PDO::FETCH_COLUMN);
34
35
        if ($result !== (array) $result) {
36
            return [];
37
        }
38
39
        return $result;
40
    }
41
42
    public function generateCodeFixed(): string
43
    {
44
        // ToDo NEXT-12515 - When code-pattern-handling will be implemented, use the new method
45
        return strtoupper(substr(str_replace(['+', '/', '='], '', base64_encode(random_bytes(8))), 0, 8));
46
    }
47
}
48