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

PromotionCodesLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 13
c 1
b 1
f 0
dl 0
loc 38
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadIndividualCodes() 0 17 2
A __construct() 0 3 1
A generateCodeFixed() 0 4 1
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